1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Alexandre |
5
|
|
|
* Date: 11/03/2018 |
6
|
|
|
* Time: 17:56 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OAuth2\AuthorizationGrantTypes; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use OAuth2\Storages\AccessTokenStorageInterface; |
13
|
|
|
use OAuth2\Storages\RefreshTokenStorageInterface; |
14
|
|
|
|
15
|
|
|
abstract class AbstractGrantType implements GrantTypeInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var AccessTokenStorageInterface |
19
|
|
|
*/ |
20
|
|
|
protected $accessTokenStorage; |
21
|
|
|
/** |
22
|
|
|
* @var RefreshTokenStorageInterface |
23
|
|
|
*/ |
24
|
|
|
protected $refreshTokenStorage; |
25
|
|
|
|
26
|
|
|
public function __construct(AccessTokenStorageInterface $accessTokenStorage, |
27
|
|
|
RefreshTokenStorageInterface $refreshTokenStorage) |
28
|
|
|
{ |
29
|
|
|
$this->accessTokenStorage = $accessTokenStorage; |
30
|
|
|
$this->refreshTokenStorage = $refreshTokenStorage; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function issueTokens(array $scopes, string $clientIdentifier, ?string $resourceOwnerIdentifier = null, |
34
|
|
|
?string $authorizationCode = null) |
35
|
|
|
{ |
36
|
|
|
return array_merge( |
37
|
|
|
$this->issueAccessToken($scopes, $clientIdentifier, $resourceOwnerIdentifier, $authorizationCode), |
38
|
|
|
$this->issueRefreshToken($scopes, $clientIdentifier, $resourceOwnerIdentifier, $authorizationCode) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function issueAccessToken(array $scopes, string $clientIdentifier, |
43
|
|
|
?string $resourceOwnerIdentifier = null, ?string $authorizationCode = null): array |
44
|
|
|
{ |
45
|
|
|
$accessToken = $this->accessTokenStorage->generate($scopes, $clientIdentifier, |
46
|
|
|
$resourceOwnerIdentifier, $authorizationCode); |
47
|
|
|
|
48
|
|
|
return [ |
49
|
|
|
'access_token' => $accessToken->getToken(), |
50
|
|
|
'token_type' => $accessToken->getType(), |
|
|
|
|
51
|
|
|
'expires_in' => $this->accessTokenStorage->getLifetime() |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function issueRefreshToken(array $scopes, string $clientIdentifier, |
56
|
|
|
?string $resourceOwnerIdentifier = null, ?string $authorizationCode = null) |
57
|
|
|
{ |
58
|
|
|
$accessToken = $this->refreshTokenStorage->generate($scopes, $clientIdentifier, $resourceOwnerIdentifier, $authorizationCode); |
59
|
|
|
return [ |
60
|
|
|
'refresh_token' => $accessToken->getToken() |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
} |