3
3
namespace kamermans \OAuth2 ;
4
4
5
5
use GuzzleHttp \Exception \BadResponseException ;
6
- use kamermans \OAuth2 \GrantType \GrantTypeInterface ;
7
- use kamermans \OAuth2 \Utils \Helper ;
8
6
9
7
/**
10
8
* OAuth2 plugin.
@@ -16,49 +14,66 @@ class OAuth2Handler
16
14
/**
17
15
* The grant type implementation used to acquire access tokens.
18
16
*
19
- * @var GrantTypeInterface
17
+ * @var GrantType\ GrantTypeInterface
20
18
*/
21
19
protected $ grantType ;
22
20
23
21
/**
24
22
* The grant type implementation used to refresh access tokens.
25
23
*
26
- * @var GrantTypeInterface
24
+ * @var GrantType\ GrantTypeInterface
27
25
*/
28
26
protected $ refreshTokenGrantType ;
29
27
30
28
/**
31
29
* The service in charge of including client credentials into requests.
32
30
* to get an access token.
33
31
*
34
- * @var AccessTokenSigner
32
+ * @var Signer\ClientCredentials\SignerInterface
35
33
*/
36
34
protected $ clientCredentialsSigner ;
37
35
38
36
/**
39
37
* The service in charge of including the access token into requests.
40
38
*
41
- * @var AccessTokenSigner
39
+ * @var Signer\AccessToken\SignerInterface
42
40
*/
43
41
protected $ accessTokenSigner ;
44
42
45
43
/**
46
44
* The object including access token.
47
45
*
48
- * @var TokenInterface
46
+ * @var Token\ TokenInterface
49
47
*/
50
48
protected $ rawToken ;
51
49
52
50
/**
53
51
* The service in charge of persisting access token.
54
52
*
55
- * @var TokenPersistenceInterface
53
+ * @var Persistence\ TokenPersistenceInterface
56
54
*/
57
55
protected $ tokenPersistence ;
58
56
59
57
/**
60
- * @param GrantTypeInterface $grantType
61
- * @param GrantTypeInterface $refreshTokenGrantType
58
+ * Callable used to instantiate a blank TokenInterface instance.
59
+ * Called with no arguments, must return a newly constructed class implementing TokenInterface
60
+ *
61
+ * @var callable
62
+ */
63
+ protected $ newTokenSupplier ;
64
+
65
+ /**
66
+ * Factory responsible for parsing server token response
67
+ *
68
+ * @var callable
69
+ */
70
+ protected $ tokenFactory ;
71
+
72
+ /**
73
+ * @param GrantType\GrantTypeInterface $grantType
74
+ * @param GrantType\GrantTypeInterface|null $refreshTokenGrantType
75
+ * @param Signer\ClientCredentials\SignerInterface|null $clientCredentialsSigner
76
+ * @param Signer\AccessToken\SignerInterface|null $accessTokenSigner
62
77
*/
63
78
public function __construct (
64
79
GrantType \GrantTypeInterface $ grantType ,
@@ -81,10 +96,13 @@ public function __construct(
81
96
82
97
$ this ->tokenPersistence = new Persistence \NullTokenPersistence ();
83
98
$ this ->tokenFactory = new Token \RawTokenFactory ();
99
+ $ this ->newTokenSupplier = function (){ return new Token \RawToken (); };
84
100
}
85
101
86
102
/**
87
103
* @param Signer\ClientCredentials\SignerInterface $signer
104
+ *
105
+ * @return self
88
106
*/
89
107
public function setClientCredentialsSigner (Signer \ClientCredentials \SignerInterface $ signer )
90
108
{
@@ -94,7 +112,9 @@ public function setClientCredentialsSigner(Signer\ClientCredentials\SignerInterf
94
112
}
95
113
96
114
/**
97
- * @param AccessToken\SignerInterface $signer
115
+ * @param Signer\AccessToken\SignerInterface $signer
116
+ *
117
+ * @return self
98
118
*/
99
119
public function setAccessTokenSigner (Signer \AccessToken \SignerInterface $ signer )
100
120
{
@@ -105,6 +125,8 @@ public function setAccessTokenSigner(Signer\AccessToken\SignerInterface $signer)
105
125
106
126
/**
107
127
* @param Persistence\TokenPersistenceInterface $tokenPersistence
128
+ *
129
+ * @return self
108
130
*/
109
131
public function setTokenPersistence (Persistence \TokenPersistenceInterface $ tokenPersistence )
110
132
{
@@ -115,6 +137,8 @@ public function setTokenPersistence(Persistence\TokenPersistenceInterface $token
115
137
116
138
/**
117
139
* @param callable $tokenFactory
140
+ *
141
+ * @return self
118
142
*/
119
143
public function setTokenFactory (callable $ tokenFactory )
120
144
{
@@ -123,12 +147,23 @@ public function setTokenFactory(callable $tokenFactory)
123
147
return $ this ;
124
148
}
125
149
150
+ /**
151
+ * @param callable $tokenSupplier the new token supplier
152
+ *
153
+ * @return self
154
+ */
155
+ public function setNewTokenSupplier (callable $ tokenSupplier ) {
156
+ $ this ->newTokenSupplier = $ tokenSupplier ;
126
157
158
+ return $ this ;
159
+ }
127
160
128
161
/**
129
162
* Manually set the access token.
130
163
*
131
- * @param string|array|TokenInterface $token An array of token data, an access token string, or a TokenInterface object
164
+ * @param string|array|Token\TokenInterface $token An array of token data, an access token string, or a TokenInterface object
165
+ *
166
+ * @return self
132
167
*/
133
168
public function setAccessToken ($ token )
134
169
{
@@ -161,13 +196,13 @@ public function deleteAccessToken()
161
196
*
162
197
* @return string|null A valid access token or null if unable to get one
163
198
*
164
- * @throws AccessTokenRequestException while trying to run `requestNewAccessToken` method
199
+ * @throws Exception\ AccessTokenRequestException while trying to run `requestNewAccessToken` method
165
200
*/
166
201
public function getAccessToken ()
167
202
{
168
203
// If token is not set try to get it from the persistent storage.
169
204
if ($ this ->rawToken === null ) {
170
- $ this ->rawToken = $ this ->tokenPersistence ->restoreToken (new Token \ RawToken ( ));
205
+ $ this ->rawToken = $ this ->tokenPersistence ->restoreToken (call_user_func ( $ this -> newTokenSupplier ));
171
206
}
172
207
173
208
// If token is not set or expired then try to acquire a new one...
@@ -189,7 +224,7 @@ public function getAccessToken()
189
224
/**
190
225
* Gets the current Token object
191
226
*
192
- * @return Token\RawToken |null
227
+ * @return Token\TokenInterface |null
193
228
*/
194
229
public function getRawToken ()
195
230
{
@@ -210,7 +245,7 @@ protected function signRequest($request)
210
245
/**
211
246
* Helper method for (callable)tokenFactory
212
247
*
213
- * @return TokenInterface
248
+ * @return Token\ TokenInterface
214
249
*/
215
250
protected function tokenFactory ()
216
251
{
@@ -220,9 +255,7 @@ protected function tokenFactory()
220
255
/**
221
256
* Acquire a new access token from the server.
222
257
*
223
- * @return TokenInterface|null
224
- *
225
- * @throws AccessTokenRequestException
258
+ * @throws Exception\AccessTokenRequestException
226
259
*/
227
260
protected function requestNewAccessToken ()
228
261
{
0 commit comments