1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Risan\OAuth1\Config; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
use Risan\OAuth1\Request\UriParserInterface; |
8
|
|
|
|
9
|
|
|
class UriConfig implements UriConfigInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The UriParserInterface implementation. |
13
|
|
|
* |
14
|
|
|
* @return \Risan\OAuth1\Request\UriParserInterface |
15
|
|
|
*/ |
16
|
|
|
protected $parser; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The base URI. |
20
|
|
|
* |
21
|
|
|
* @var \Psr\Http\Message\UriInterface|null |
22
|
|
|
*/ |
23
|
|
|
protected $base; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The URI for obtaining temporary credentials. Also known as request token |
27
|
|
|
* URI. |
28
|
|
|
* |
29
|
|
|
* @var \Psr\Http\Message\UriInterface |
30
|
|
|
*/ |
31
|
|
|
protected $temporaryCredentials; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The URI for asking user to authorize the request. |
35
|
|
|
* |
36
|
|
|
* @var \Psr\Http\Message\UriInterface |
37
|
|
|
*/ |
38
|
|
|
protected $authorization; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The URI for obtaining token credentials. Also known as access token |
42
|
|
|
* URI. |
43
|
|
|
* |
44
|
|
|
* @var \Psr\Http\Message\UriInterface |
45
|
|
|
*/ |
46
|
|
|
protected $tokenCredentials; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The callback URI. |
50
|
|
|
* |
51
|
|
|
* @var \Psr\Http\Message\UriInterface|null |
52
|
|
|
*/ |
53
|
|
|
protected $callback; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create UriConfig instance. |
57
|
|
|
* |
58
|
|
|
* @param array $uris |
59
|
|
|
* @param \Risan\OAuth1\Request\UriParserInterface $parser |
60
|
|
|
*/ |
61
|
31 |
|
public function __construct(array $uris, UriParserInterface $parser) |
62
|
|
|
{ |
63
|
31 |
|
$this->parser = $parser; |
64
|
31 |
|
$this->setFromArray($uris); |
65
|
31 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
1 |
|
public function getParser() |
71
|
|
|
{ |
72
|
1 |
|
return $this->parser; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set URIs from an array. |
77
|
|
|
* |
78
|
|
|
* @param array $uris |
79
|
|
|
* |
80
|
|
|
* @return \Risan\OAuth1\Config\UriConfig |
81
|
|
|
*/ |
82
|
31 |
|
public function setFromArray(array $uris) |
83
|
|
|
{ |
84
|
31 |
|
$this->validateUris($uris); |
85
|
|
|
|
86
|
31 |
|
$this->temporaryCredentials = $this->parser->toPsrUri($uris['temporary_credentials_uri']); |
87
|
31 |
|
$this->authorization = $this->parser->toPsrUri($uris['authorization_uri']); |
88
|
31 |
|
$this->tokenCredentials = $this->parser->toPsrUri($uris['token_credentials_uri']); |
89
|
|
|
|
90
|
31 |
|
if (isset($uris['base_uri'])) { |
91
|
31 |
|
$this->setBase($this->parser->toPsrUri($uris['base_uri'])); |
92
|
|
|
} |
93
|
|
|
|
94
|
31 |
|
if (isset($uris['callback_uri'])) { |
95
|
31 |
|
$this->callback = $this->parser->toPsrUri($uris['callback_uri']); |
96
|
|
|
} |
97
|
|
|
|
98
|
31 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Validate the given URI array. |
103
|
|
|
* |
104
|
|
|
* @param array $uris |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
* |
108
|
|
|
* @throws \InvalidArgumentException |
109
|
|
|
*/ |
110
|
31 |
|
public function validateUris(array $uris) |
111
|
|
|
{ |
112
|
|
|
$requiredParams = [ |
113
|
31 |
|
'temporary_credentials_uri', |
114
|
|
|
'authorization_uri', |
115
|
|
|
'token_credentials_uri', |
116
|
|
|
]; |
117
|
|
|
|
118
|
31 |
|
foreach ($requiredParams as $param) { |
119
|
31 |
|
if (! isset($uris[$param])) { |
120
|
31 |
|
throw new InvalidArgumentException("Missing URI configuration: {$param}."); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
31 |
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set the base URI. |
129
|
|
|
* |
130
|
|
|
* @param \Psr\Http\Message\UriInterface $uri |
131
|
|
|
* |
132
|
|
|
* @return \Risan\OAuth1\Config\UriConfig |
133
|
|
|
* |
134
|
|
|
* @throws \InvalidArgumentException |
135
|
|
|
*/ |
136
|
31 |
|
public function setBase(UriInterface $uri) |
137
|
|
|
{ |
138
|
31 |
|
if (! $this->parser->isAbsolute($uri)) { |
139
|
1 |
|
throw new InvalidArgumentException('The base URI must be absolute.'); |
140
|
|
|
} |
141
|
|
|
|
142
|
31 |
|
$this->base = $uri; |
143
|
|
|
|
144
|
31 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
12 |
|
public function base() |
151
|
|
|
{ |
152
|
12 |
|
return $this->hasBase() ? $this->base : null; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
14 |
|
public function hasBase() |
159
|
|
|
{ |
160
|
14 |
|
return null !== $this->base; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
4 |
|
public function forTemporaryCredentials() |
167
|
|
|
{ |
168
|
4 |
|
return $this->build($this->temporaryCredentials); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
4 |
|
public function forAuthorization() |
175
|
|
|
{ |
176
|
4 |
|
return $this->build($this->authorization); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
4 |
|
public function forTokenCredentials() |
183
|
|
|
{ |
184
|
4 |
|
return $this->build($this->tokenCredentials); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
4 |
|
public function callback() |
191
|
|
|
{ |
192
|
4 |
|
return $this->hasCallback() ? $this->build($this->callback) : null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
7 |
|
public function hasCallback() |
199
|
|
|
{ |
200
|
7 |
|
return null !== $this->callback; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
12 |
|
public function build($uri) |
207
|
|
|
{ |
208
|
12 |
|
$uri = $this->parser->toPsrUri($uri); |
209
|
|
|
|
210
|
12 |
|
if ($this->shouldBeResolvedToAbsoluteUri($uri)) { |
211
|
10 |
|
$uri = $this->parser->resolve($this->base(), $uri); |
212
|
|
|
} |
213
|
|
|
|
214
|
12 |
|
return $this->parser->isMissingScheme($uri) ? $uri->withScheme('http') : $uri; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Check if the given URI should be resolved to absolute URI. |
219
|
|
|
* |
220
|
|
|
* @param \Psr\Http\Message\UriInterface $uri |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
13 |
|
public function shouldBeResolvedToAbsoluteUri(UriInterface $uri) |
225
|
|
|
{ |
226
|
13 |
|
return ! $this->parser->isAbsolute($uri) && $this->hasBase(); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|