1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Boris Guéry <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Bgy\OAuth2; |
7
|
|
|
|
8
|
|
|
use Bgy\OAuth2\GrantType\GrantDecision; |
9
|
|
|
use Bgy\OAuth2\GrantType\GrantError; |
10
|
|
|
use Bgy\OAuth2\GrantType\GrantType; |
11
|
|
|
|
12
|
|
|
class AuthorizationServer |
13
|
|
|
{ |
14
|
|
|
private $configuration; |
15
|
|
|
private $clientAuthenticator; |
16
|
|
|
|
17
|
|
|
public function __construct(AuthorizationServerConfiguration $configuration) |
18
|
|
|
{ |
19
|
|
|
$this->configuration = $configuration; |
20
|
|
|
$this->clientAuthenticator = new ClientAuthenticator( |
21
|
|
|
$this->configuration->getClientStorage() |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param TokenRequestAttempt $tokenRequestAttempt |
27
|
|
|
* @return FailedTokenRequestAttemptResult|SuccessfulTokenRequestAttemptResult |
28
|
|
|
*/ |
29
|
|
|
public function requestAccessToken(TokenRequestAttempt $tokenRequestAttempt) |
30
|
|
|
{ |
31
|
|
|
if (null === $this->getGrantTypeByIdentifier($tokenRequestAttempt->getGrantType())) { |
32
|
|
|
|
33
|
|
|
return $this->buildTokenAttemptResult($tokenRequestAttempt, GrantDecision::denied(GrantError::invalidGrant('Unknown grant type'))); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->configuration->alwaysRequireAClient()) { |
37
|
|
|
|
38
|
|
|
if (false === $tokenRequestAttempt->getInputData()->getClientId()) { |
39
|
|
|
|
40
|
|
|
return $this->buildTokenAttemptResult($tokenRequestAttempt, GrantDecision::denied(GrantError::invalidRequest('missing client_id'))); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (false === $this->clientAuthenticator->isClientValid( |
44
|
|
|
$tokenRequestAttempt->getInputData()->getClientId(), |
45
|
|
|
$tokenRequestAttempt->getInputData()->getClientSecret() |
46
|
|
|
)) { |
47
|
|
|
|
48
|
|
|
return $this->buildTokenAttemptResult($tokenRequestAttempt, GrantDecision::denied(GrantError::accessDenied('invalid client credentials'))); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$client = $this->configuration->getClientStorage() |
52
|
|
|
->findById($tokenRequestAttempt->getInputData()->getClientId()) |
53
|
|
|
; |
54
|
|
|
|
55
|
|
|
if (!in_array($tokenRequestAttempt->getGrantType(), $client->getAllowedGrantTypes())) { |
56
|
|
|
|
57
|
|
|
return $this->buildTokenAttemptResult($tokenRequestAttempt, GrantDecision::denied( |
58
|
|
|
GrantError::invalidGrant( |
59
|
|
|
sprintf( |
60
|
|
|
'This client doesn\'t support the following grant type: "%s"', |
61
|
|
|
$tokenRequestAttempt->getGrantType() |
62
|
|
|
) |
63
|
|
|
) |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->buildTokenAttemptResult($tokenRequestAttempt, $this->getGrantTypeByIdentifier($tokenRequestAttempt->getGrantType()) |
68
|
|
|
->grant($tokenRequestAttempt) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->buildTokenAttemptResult($tokenRequestAttempt, GrantDecision::denied(GrantError::serverError('unknown error'))); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function buildTokenAttemptResult(TokenRequestAttempt $tokenRequestAttempt, GrantDecision $grantDecision) |
76
|
|
|
{ |
77
|
|
|
if ($grantDecision->isAllowed()) { |
78
|
|
|
|
79
|
|
|
$token = $this->configuration->getTokenGenerator()->generate( |
80
|
|
|
['length' => $this->configuration->getAccessTokenLength()] |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$accessToken = new AccessToken( |
84
|
|
|
$token, |
85
|
|
|
$this->configuration->getAccessTokenTTL(), |
86
|
|
|
$tokenRequestAttempt->getInputData()->getClientId(), |
87
|
|
|
$grantDecision->getResourceOwnerId(), |
88
|
|
|
[] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$this->configuration->getAccessTokenStorage()->save($accessToken); |
92
|
|
|
|
93
|
|
|
$refreshToken = null; |
94
|
|
|
if ($this->configuration->alwaysGenerateARefreshToken()) { |
95
|
|
|
$token = $this->configuration->getTokenGenerator()->generate( |
96
|
|
|
['length' => $this->configuration->getAccessTokenLength()] |
97
|
|
|
); |
98
|
|
|
$refreshToken = new RefreshToken($token); |
99
|
|
|
|
100
|
|
|
$this->configuration->getRefreshTokenStorage()->save($refreshToken); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$result = new SuccessfulTokenRequestAttemptResult($grantDecision, $accessToken, $refreshToken); |
104
|
|
|
|
105
|
|
|
} else { |
106
|
|
|
|
107
|
|
|
$result = new FailedTokenRequestAttemptResult($grantDecision); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $identifier |
115
|
|
|
* @return GrantType |
116
|
|
|
*/ |
117
|
|
|
private function getGrantTypeByIdentifier($identifier) |
118
|
|
|
{ |
119
|
|
|
if (!isset($this->configuration->getGrantTypeExtensions()[$identifier])) { |
120
|
|
|
|
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->configuration->getGrantTypeExtensions()[$identifier]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|