1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: GCC-MED |
5
|
|
|
* Date: 12/03/2018 |
6
|
|
|
* Time: 15:16 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OAuth2\Flows; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use OAuth2\Endpoints\AuthorizationEndpoint; |
13
|
|
|
use OAuth2\Endpoints\TokenEndpoint; |
14
|
|
|
use OAuth2\Exceptions\OAuthException; |
15
|
|
|
use OAuth2\GrantTypes\AbstractGrantType; |
16
|
|
|
use OAuth2\GrantTypes\GrantTypeInterface; |
17
|
|
|
use OAuth2\ResponseTypes\ResponseTypeInterface; |
18
|
|
|
use OAuth2\ScopePolicy\Policies\ScopePolicyInterface; |
19
|
|
|
use OAuth2\ScopePolicy\ScopePolicyManager; |
20
|
|
|
use OAuth2\Storages\AccessTokenStorageInterface; |
21
|
|
|
use OAuth2\Storages\RefreshTokenStorageInterface; |
22
|
|
|
use OAuth2\Storages\ResourceOwnerStorageInterface; |
23
|
|
|
|
24
|
|
|
class ResourceOwnerPasswordCredentialsFlow extends AbstractGrantType implements FlowInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ResourceOwnerStorageInterface |
28
|
|
|
*/ |
29
|
|
|
private $resourceOwnerStorage; |
30
|
|
|
/** |
31
|
|
|
* @var ScopePolicyManager |
32
|
|
|
*/ |
33
|
|
|
private $scopePolicyManager; |
34
|
|
|
|
35
|
|
|
public function __construct(ScopePolicyManager $scopePolicyManager, |
36
|
|
|
ResourceOwnerStorageInterface $resourceOwnerStorage, |
37
|
|
|
AccessTokenStorageInterface $accessTokenStorage, |
38
|
|
|
RefreshTokenStorageInterface $refreshTokenStorage) |
39
|
|
|
{ |
40
|
|
|
parent::__construct($accessTokenStorage, $refreshTokenStorage); |
41
|
|
|
$this->resourceOwnerStorage = $resourceOwnerStorage; |
42
|
|
|
$this->scopePolicyManager = $scopePolicyManager; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return ResponseTypeInterface[] |
47
|
|
|
*/ |
48
|
|
|
function getResponseTypes(): array |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
return []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return GrantTypeInterface[] |
55
|
|
|
*/ |
56
|
|
|
function getGrantTypes(): array |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
return ['password']; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if (empty($requestData['username'])) { |
64
|
|
|
throw new OAuthException('invalid_request', |
65
|
|
|
'The request is missing the required parameter username.', |
66
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.3'); |
67
|
|
|
} |
68
|
|
|
if (empty($requestData['password'])) { |
69
|
|
|
throw new OAuthException('invalid_request', |
70
|
|
|
'The request is missing the required parameter password.', |
71
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.3'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$scopes = $this->scopePolicyManager->getScopes($tokenEndpoint->getClient(), $requestData['scope'] ?? null); |
75
|
|
|
$this->scopePolicyManager->verifyScopes($tokenEndpoint->getClient(), $scopes); |
76
|
|
|
|
77
|
|
|
$resourceOwnerIdentifier = $this->resourceOwnerStorage->validateCredentials($requestData['username'], $requestData['password']); |
78
|
|
|
if (!$resourceOwnerIdentifier) { |
79
|
|
|
throw new OAuthException('invalid_grant', |
80
|
|
|
'The provider authorization grant is invalid. Resource owner credentials invalid.', |
81
|
|
|
'https://tools.ietf.org/html/rfc7636#section-4.3'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->issueTokens(implode(' ', $scopes), $tokenEndpoint->getClient()->getIdentifier(), $resourceOwnerIdentifier); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
throw new \BadMethodCallException(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
function getDefaultResponseMode(): string |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
throw new \BadMethodCallException(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
function getUnsupportedResponseModes(): array |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
throw new \BadMethodCallException(); |
100
|
|
|
} |
101
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.