1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\WebhookListeners\Db;
use OCP\AppFramework\Db\Entity;
use OCP\Security\ICrypto;
use OCP\Server;
/**
* @method void setUserId(?string $userId)
* @method ?string getAppId()
* @method ?string getUserId()
* @method string getHttpMethod()
* @method string getUri()
* @method ?array getHeaders()
* @method ?string getAuthData()
* @method void setAuthData(?string $data)
* @method string getAuthMethod()
* @psalm-suppress PropertyNotSetInConstructor
*/
class WebhookListener extends Entity implements \JsonSerializable {
/**
* @var ?string id of the app_api application who added the webhook listener
*/
protected $appId = null;
/**
* @var ?string id of the user who added the webhook listener
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $userId = null;
/**
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $httpMethod;
/**
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $uri;
/**
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $event;
/**
* @var array
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $eventFilter;
/**
* @var ?string
* If not empty, id of the user that needs to be connected for the webhook to trigger
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $userIdFilter;
/**
* @var ?array
*/
protected $headers = null;
/**
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $authMethod;
/**
* @var ?string
*/
protected $authData = null;
private ICrypto $crypto;
public function __construct(
?ICrypto $crypto = null,
) {
if ($crypto === null) {
$crypto = Server::get(ICrypto::class);
}
$this->crypto = $crypto;
$this->addType('appId', 'string');
$this->addType('userId', 'string');
$this->addType('httpMethod', 'string');
$this->addType('uri', 'string');
$this->addType('event', 'string');
$this->addType('eventFilter', 'json');
$this->addType('userIdFilter', 'string');
$this->addType('headers', 'json');
$this->addType('authMethod', 'string');
$this->addType('authData', 'string');
}
public function getAuthMethodEnum(): AuthMethod {
return AuthMethod::from($this->getAuthMethod());
}
public function getAuthDataClear(): array {
$authData = $this->getAuthData();
if ($authData === null) {
return [];
}
return json_decode($this->crypto->decrypt($authData), associative:true, flags:JSON_THROW_ON_ERROR);
}
public function setAuthDataClear(
#[\SensitiveParameter]
?array $data,
): void {
if ($data === null) {
if ($this->getAuthMethodEnum() === AuthMethod::Header) {
throw new \UnexpectedValueException('Header auth method needs an associative array of headers as auth data');
}
$this->setAuthData(null);
return;
}
$this->setAuthData($this->crypto->encrypt(json_encode($data)));
}
public function jsonSerialize(): array {
$fields = array_keys($this->getFieldTypes());
return array_combine(
$fields,
array_map(
fn ($field) => $this->getter($field),
$fields
)
);
}
public function getAppId(): ?string {
return $this->appId;
}
}
|