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 League\OAuth2\Server\Entities\ClientEntityInterface; |
15
|
|
|
use League\OAuth2\Server\Entities\UserEntityInterface; |
16
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
17
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
18
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface; |
19
|
|
|
use League\OAuth2\Server\RequestEvent; |
20
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Password grant class. |
25
|
|
|
*/ |
26
|
|
|
class PasswordGrant extends AbstractGrant |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param UserRepositoryInterface $userRepository |
30
|
|
|
* @param RefreshTokenRepositoryInterface $refreshTokenRepository |
31
|
|
|
*/ |
32
|
5 |
|
public function __construct( |
33
|
|
|
UserRepositoryInterface $userRepository, |
34
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository |
35
|
|
|
) { |
36
|
5 |
|
$this->setUserRepository($userRepository); |
37
|
5 |
|
$this->setRefreshTokenRepository($refreshTokenRepository); |
38
|
|
|
|
39
|
5 |
|
$this->refreshTokenTTL = new \DateInterval('P1M'); |
40
|
5 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
4 |
|
public function respondToAccessTokenRequest( |
46
|
|
|
ServerRequestInterface $request, |
47
|
|
|
ResponseTypeInterface $responseType, |
48
|
|
|
\DateInterval $accessTokenTTL |
49
|
|
|
) { |
50
|
|
|
// Validate request |
51
|
4 |
|
$client = $this->validateClient($request); |
52
|
4 |
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope)); |
53
|
4 |
|
$user = $this->validateUser($request, $client); |
54
|
|
|
|
55
|
|
|
// Finalize the requested scopes |
56
|
1 |
|
$finalizedScopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $user->getIdentifier()); |
57
|
|
|
|
58
|
|
|
// Issue and persist new tokens |
59
|
1 |
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $finalizedScopes); |
60
|
1 |
|
$refreshToken = $this->issueRefreshToken($accessToken); |
61
|
|
|
|
62
|
|
|
// Send events to emitter |
63
|
1 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
64
|
1 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); |
65
|
|
|
|
66
|
|
|
// Inject tokens into response |
67
|
1 |
|
$responseType->setAccessToken($accessToken); |
|
|
|
|
68
|
1 |
|
$responseType->setRefreshToken($refreshToken); |
|
|
|
|
69
|
|
|
|
70
|
1 |
|
return $responseType; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ServerRequestInterface $request |
75
|
|
|
* @param ClientEntityInterface $client |
76
|
|
|
* |
77
|
|
|
* @throws OAuthServerException |
78
|
|
|
* |
79
|
|
|
* @return UserEntityInterface |
80
|
|
|
*/ |
81
|
4 |
|
protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client) |
82
|
|
|
{ |
83
|
4 |
|
$username = $this->getRequestParameter('username', $request); |
84
|
4 |
|
if (is_null($username)) { |
85
|
1 |
|
throw OAuthServerException::invalidRequest('username'); |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
$password = $this->getRequestParameter('password', $request); |
89
|
3 |
|
if (is_null($password)) { |
90
|
1 |
|
throw OAuthServerException::invalidRequest('password'); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
$user = $this->userRepository->getUserEntityByUserCredentials( |
94
|
2 |
|
$username, |
95
|
2 |
|
$password, |
96
|
2 |
|
$this->getIdentifier(), |
97
|
2 |
|
$client |
98
|
|
|
); |
99
|
2 |
|
if ($user instanceof UserEntityInterface === false) { |
100
|
1 |
|
$this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request)); |
101
|
|
|
|
102
|
1 |
|
throw OAuthServerException::invalidCredentials(); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
return $user; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
5 |
|
public function getIdentifier() |
112
|
|
|
{ |
113
|
5 |
|
return 'password'; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: