Skip to content

Commit 18fb67c

Browse files
authored
Merge pull request #27 from thiakil/new-token-supplier
New token supplier
2 parents 8741be9 + 50de8b9 commit 18fb67c

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

src/OAuth2Handler.php

+52-19
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace kamermans\OAuth2;
44

55
use GuzzleHttp\Exception\BadResponseException;
6-
use kamermans\OAuth2\GrantType\GrantTypeInterface;
7-
use kamermans\OAuth2\Utils\Helper;
86

97
/**
108
* OAuth2 plugin.
@@ -16,49 +14,66 @@ class OAuth2Handler
1614
/**
1715
* The grant type implementation used to acquire access tokens.
1816
*
19-
* @var GrantTypeInterface
17+
* @var GrantType\GrantTypeInterface
2018
*/
2119
protected $grantType;
2220

2321
/**
2422
* The grant type implementation used to refresh access tokens.
2523
*
26-
* @var GrantTypeInterface
24+
* @var GrantType\GrantTypeInterface
2725
*/
2826
protected $refreshTokenGrantType;
2927

3028
/**
3129
* The service in charge of including client credentials into requests.
3230
* to get an access token.
3331
*
34-
* @var AccessTokenSigner
32+
* @var Signer\ClientCredentials\SignerInterface
3533
*/
3634
protected $clientCredentialsSigner;
3735

3836
/**
3937
* The service in charge of including the access token into requests.
4038
*
41-
* @var AccessTokenSigner
39+
* @var Signer\AccessToken\SignerInterface
4240
*/
4341
protected $accessTokenSigner;
4442

4543
/**
4644
* The object including access token.
4745
*
48-
* @var TokenInterface
46+
* @var Token\TokenInterface
4947
*/
5048
protected $rawToken;
5149

5250
/**
5351
* The service in charge of persisting access token.
5452
*
55-
* @var TokenPersistenceInterface
53+
* @var Persistence\TokenPersistenceInterface
5654
*/
5755
protected $tokenPersistence;
5856

5957
/**
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
6277
*/
6378
public function __construct(
6479
GrantType\GrantTypeInterface $grantType,
@@ -81,10 +96,13 @@ public function __construct(
8196

8297
$this->tokenPersistence = new Persistence\NullTokenPersistence();
8398
$this->tokenFactory = new Token\RawTokenFactory();
99+
$this->newTokenSupplier = function(){ return new Token\RawToken(); };
84100
}
85101

86102
/**
87103
* @param Signer\ClientCredentials\SignerInterface $signer
104+
*
105+
* @return self
88106
*/
89107
public function setClientCredentialsSigner(Signer\ClientCredentials\SignerInterface $signer)
90108
{
@@ -94,7 +112,9 @@ public function setClientCredentialsSigner(Signer\ClientCredentials\SignerInterf
94112
}
95113

96114
/**
97-
* @param AccessToken\SignerInterface $signer
115+
* @param Signer\AccessToken\SignerInterface $signer
116+
*
117+
* @return self
98118
*/
99119
public function setAccessTokenSigner(Signer\AccessToken\SignerInterface $signer)
100120
{
@@ -105,6 +125,8 @@ public function setAccessTokenSigner(Signer\AccessToken\SignerInterface $signer)
105125

106126
/**
107127
* @param Persistence\TokenPersistenceInterface $tokenPersistence
128+
*
129+
* @return self
108130
*/
109131
public function setTokenPersistence(Persistence\TokenPersistenceInterface $tokenPersistence)
110132
{
@@ -115,6 +137,8 @@ public function setTokenPersistence(Persistence\TokenPersistenceInterface $token
115137

116138
/**
117139
* @param callable $tokenFactory
140+
*
141+
* @return self
118142
*/
119143
public function setTokenFactory(callable $tokenFactory)
120144
{
@@ -123,12 +147,23 @@ public function setTokenFactory(callable $tokenFactory)
123147
return $this;
124148
}
125149

150+
/**
151+
* @param callable $tokenSupplier the new token supplier
152+
*
153+
* @return self
154+
*/
155+
public function setNewTokenSupplier(callable $tokenSupplier) {
156+
$this->newTokenSupplier = $tokenSupplier;
126157

158+
return $this;
159+
}
127160

128161
/**
129162
* Manually set the access token.
130163
*
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
132167
*/
133168
public function setAccessToken($token)
134169
{
@@ -161,13 +196,13 @@ public function deleteAccessToken()
161196
*
162197
* @return string|null A valid access token or null if unable to get one
163198
*
164-
* @throws AccessTokenRequestException while trying to run `requestNewAccessToken` method
199+
* @throws Exception\AccessTokenRequestException while trying to run `requestNewAccessToken` method
165200
*/
166201
public function getAccessToken()
167202
{
168203
// If token is not set try to get it from the persistent storage.
169204
if ($this->rawToken === null) {
170-
$this->rawToken = $this->tokenPersistence->restoreToken(new Token\RawToken());
205+
$this->rawToken = $this->tokenPersistence->restoreToken(call_user_func($this->newTokenSupplier));
171206
}
172207

173208
// If token is not set or expired then try to acquire a new one...
@@ -189,7 +224,7 @@ public function getAccessToken()
189224
/**
190225
* Gets the current Token object
191226
*
192-
* @return Token\RawToken|null
227+
* @return Token\TokenInterface|null
193228
*/
194229
public function getRawToken()
195230
{
@@ -210,7 +245,7 @@ protected function signRequest($request)
210245
/**
211246
* Helper method for (callable)tokenFactory
212247
*
213-
* @return TokenInterface
248+
* @return Token\TokenInterface
214249
*/
215250
protected function tokenFactory()
216251
{
@@ -220,9 +255,7 @@ protected function tokenFactory()
220255
/**
221256
* Acquire a new access token from the server.
222257
*
223-
* @return TokenInterface|null
224-
*
225-
* @throws AccessTokenRequestException
258+
* @throws Exception\AccessTokenRequestException
226259
*/
227260
protected function requestNewAccessToken()
228261
{

src/OAuth2Middleware.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ public function __invoke(callable $handler)
3737
}
3838

3939
/**
40-
* Request error event handler.
41-
*
42-
* Handles unauthorized errors by acquiring a new access token and
43-
* retrying the request.
44-
*
45-
* @param ErrorEvent $event Event received
46-
*/
40+
* Request error event handler.
41+
*
42+
* Handles unauthorized errors by acquiring a new access token and
43+
* retrying the request.
44+
*
45+
* @param \Psr\Http\Message\RequestInterface $request
46+
* @param array $options
47+
* @param callable $handler
48+
*
49+
* @return callable
50+
*/
4751
private function onFulfilled(RequestInterface $request, array $options, $handler)
4852
{
4953
return function ($response) use ($request, $options, $handler) {

0 commit comments

Comments
 (0)