1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* OAuth 2.0 Password grant. |
4
|
|
|
* |
5
|
|
|
* @author Alex Bilbie <[email protected]> |
6
|
|
|
* @copyright Copyright (c) Alex Bilbie |
7
|
|
|
* @license http://mit-license.org/ |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\OAuth2\Server\Grant; |
13
|
|
|
|
14
|
|
|
use DateInterval; |
15
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
16
|
|
|
use League\OAuth2\Server\Entities\UserEntityInterface; |
17
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
18
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
19
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface; |
20
|
|
|
use League\OAuth2\Server\RequestEvent; |
21
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Password grant class. |
26
|
|
|
*/ |
27
|
|
|
class PasswordGrant extends AbstractGrant |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param UserRepositoryInterface $userRepository |
31
|
|
|
* @param RefreshTokenRepositoryInterface $refreshTokenRepository |
32
|
|
|
*/ |
33
|
6 |
|
public function __construct( |
34
|
|
|
UserRepositoryInterface $userRepository, |
35
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository |
36
|
|
|
) { |
37
|
6 |
|
$this->setUserRepository($userRepository); |
38
|
6 |
|
$this->setRefreshTokenRepository($refreshTokenRepository); |
39
|
|
|
|
40
|
6 |
|
$this->refreshTokenTTL = new DateInterval('P1M'); |
41
|
6 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
5 |
|
public function respondToAccessTokenRequest( |
47
|
|
|
ServerRequestInterface $request, |
48
|
|
|
ResponseTypeInterface $responseType, |
49
|
|
|
DateInterval $accessTokenTTL |
50
|
|
|
) { |
51
|
|
|
// Validate request |
52
|
5 |
|
$client = $this->validateClient($request); |
53
|
4 |
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope)); |
54
|
4 |
|
$user = $this->validateUser($request, $client); |
55
|
|
|
|
56
|
|
|
// Finalize the requested scopes |
57
|
2 |
|
$finalizedScopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $user->getIdentifier()); |
58
|
|
|
|
59
|
2 |
|
$privateClaims = []; |
60
|
2 |
|
if ($this->claimRepository) { |
61
|
1 |
|
$privateClaims = $this->claimRepository->getClaims($this->getIdentifier(), $client, $user->getIdentifier()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Issue and persist new access token |
65
|
2 |
|
$accessToken = $this->issueAccessToken( |
66
|
2 |
|
$accessTokenTTL, |
67
|
|
|
$client, |
68
|
2 |
|
$user->getIdentifier(), |
69
|
|
|
$finalizedScopes, |
70
|
|
|
$privateClaims |
71
|
|
|
); |
72
|
2 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
73
|
2 |
|
$responseType->setAccessToken($accessToken); |
74
|
|
|
|
75
|
|
|
// Issue and persist new refresh token if given |
76
|
2 |
|
$refreshToken = $this->issueRefreshToken($accessToken); |
77
|
|
|
|
78
|
2 |
|
if ($refreshToken !== null) { |
79
|
1 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); |
80
|
1 |
|
$responseType->setRefreshToken($refreshToken); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
return $responseType; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param ServerRequestInterface $request |
88
|
|
|
* @param ClientEntityInterface $client |
89
|
|
|
* |
90
|
|
|
* @throws OAuthServerException |
91
|
|
|
* |
92
|
|
|
* @return UserEntityInterface |
93
|
|
|
*/ |
94
|
4 |
|
protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client) |
95
|
|
|
{ |
96
|
4 |
|
$username = $this->getRequestParameter('username', $request); |
97
|
|
|
|
98
|
4 |
|
if (\is_null($username)) { |
99
|
|
|
throw OAuthServerException::invalidRequest('username'); |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
$password = $this->getRequestParameter('password', $request); |
103
|
|
|
|
104
|
4 |
|
if (\is_null($password)) { |
105
|
1 |
|
throw OAuthServerException::invalidRequest('password'); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
$user = $this->userRepository->getUserEntityByUserCredentials( |
109
|
3 |
|
$username, |
110
|
|
|
$password, |
111
|
3 |
|
$this->getIdentifier(), |
112
|
|
|
$client |
113
|
|
|
); |
114
|
|
|
|
115
|
3 |
|
if ($user instanceof UserEntityInterface === false) { |
116
|
1 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request)); |
117
|
|
|
|
118
|
1 |
|
throw OAuthServerException::invalidGrant(); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
return $user; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
5 |
|
public function getIdentifier() |
128
|
|
|
{ |
129
|
5 |
|
return 'password'; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|