1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Parroauth2\Client; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Configuration of the client |
7
|
|
|
* |
8
|
|
|
* Note: It's not recommended to modify the options once the client is created |
9
|
|
|
* |
10
|
|
|
* @see Client |
11
|
|
|
*/ |
12
|
|
|
class ClientConfig |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
* @readonly |
17
|
|
|
*/ |
18
|
|
|
private $clientId; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
private $secret = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $openid = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
private $scopes = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $options = []; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ClientConfig constructor. |
43
|
|
|
* |
44
|
|
|
* @param string $clientId |
45
|
|
|
*/ |
46
|
144 |
|
public function __construct(string $clientId) |
47
|
|
|
{ |
48
|
144 |
|
$this->clientId = $clientId; |
49
|
144 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the client id |
53
|
|
|
* Must match with the client id configuration on the provider side |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
* |
57
|
|
|
* @psalm-mutation-free |
58
|
|
|
*/ |
59
|
68 |
|
public function clientId(): string |
60
|
|
|
{ |
61
|
68 |
|
return $this->clientId; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the client secret |
66
|
|
|
* This value may be null for a public client |
67
|
|
|
* |
68
|
|
|
* @return string|null |
69
|
|
|
* |
70
|
|
|
* @psalm-mutation-free |
71
|
|
|
*/ |
72
|
43 |
|
public function secret(): ?string |
73
|
|
|
{ |
74
|
43 |
|
return $this->secret; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the client secret |
79
|
|
|
* |
80
|
|
|
* @param string $secret |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
98 |
|
public function setSecret(string $secret): ClientConfig |
85
|
|
|
{ |
86
|
98 |
|
$this->secret = $secret; |
87
|
|
|
|
88
|
98 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Does openid connect is enabled for the client ? |
93
|
|
|
* Note: This configuration has no effect on provided which do not supports openid connect |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
* |
97
|
|
|
* @psalm-mutation-free |
98
|
|
|
*/ |
99
|
144 |
|
public function openid(): bool |
100
|
|
|
{ |
101
|
144 |
|
return $this->openid; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Enable (or disable) openid for the client |
106
|
|
|
* |
107
|
|
|
* @param bool $flag |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
91 |
|
public function enableOpenId(bool $flag = true): ClientConfig |
112
|
|
|
{ |
113
|
91 |
|
$this->openid = $flag; |
114
|
|
|
|
115
|
91 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the configured scopes |
120
|
|
|
* |
121
|
|
|
* @return string[] |
122
|
|
|
* |
123
|
|
|
* @psalm-mutation-free |
124
|
|
|
*/ |
125
|
14 |
|
public function scopes(): array |
126
|
|
|
{ |
127
|
14 |
|
return $this->scopes; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Configure the client scopes |
132
|
|
|
* Adding openid scope is not required |
133
|
|
|
* |
134
|
|
|
* @param string[] $scopes |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
70 |
|
public function setScopes(array $scopes): ClientConfig |
139
|
|
|
{ |
140
|
70 |
|
$this->scopes = $scopes; |
141
|
|
|
|
142
|
70 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get a client option |
147
|
|
|
* |
148
|
|
|
* @param string $name The option name |
149
|
|
|
* @param mixed $default The default value, if not set on the config |
150
|
|
|
* |
151
|
|
|
* @return mixed |
152
|
|
|
* |
153
|
|
|
* @psalm-mutation-free |
154
|
|
|
*/ |
155
|
45 |
|
public function option(string $name, $default = null) |
156
|
|
|
{ |
157
|
45 |
|
return $this->options[$name] ?? $default; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set a client option |
162
|
|
|
* |
163
|
|
|
* @param string $name The option name |
164
|
|
|
* @param mixed $value The option value |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
6 |
|
public function setOption(string $name, $value): ClientConfig |
169
|
|
|
{ |
170
|
6 |
|
$this->options[$name] = $value; |
171
|
|
|
|
172
|
6 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|