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
|
|
|
namespace League\OAuth2\Server\Grant; |
12
|
|
|
|
13
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
14
|
|
|
use League\OAuth2\Server\Entities\UserEntityInterface; |
15
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
16
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
17
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface; |
18
|
|
|
use League\OAuth2\Server\RequestEvent; |
19
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
20
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Password grant class. |
24
|
|
|
*/ |
25
|
|
|
class PasswordGrant extends AbstractGrant |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository |
29
|
|
|
* @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
UserRepositoryInterface $userRepository, |
33
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository |
34
|
|
|
) { |
35
|
|
|
$this->setUserRepository($userRepository); |
36
|
|
|
$this->setRefreshTokenRepository($refreshTokenRepository); |
37
|
|
|
|
38
|
|
|
$this->refreshTokenTTL = new \DateInterval('P1M'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function respondToAccessTokenRequest( |
45
|
|
|
ServerRequestInterface $request, |
46
|
|
|
ResponseTypeInterface $responseType, |
47
|
|
|
\DateInterval $accessTokenTTL |
48
|
|
|
) { |
49
|
|
|
// Validate request |
50
|
|
|
$client = $this->validateClient($request); |
51
|
|
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request)); |
52
|
|
|
$user = $this->validateUser($request, $client); |
53
|
|
|
|
54
|
|
|
// Finalize the requested scopes |
55
|
|
|
$scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $user->getIdentifier()); |
56
|
|
|
|
57
|
|
|
// Issue and persist new tokens |
58
|
|
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $scopes); |
59
|
|
|
$refreshToken = $this->issueRefreshToken($accessToken); |
60
|
|
|
|
61
|
|
|
// Inject tokens into response |
62
|
|
|
$responseType->setAccessToken($accessToken); |
63
|
|
|
$responseType->setRefreshToken($refreshToken); |
64
|
|
|
|
65
|
|
|
return $responseType; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
70
|
|
|
* @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
71
|
|
|
* |
72
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException |
73
|
|
|
* |
74
|
|
|
* @return \League\OAuth2\Server\Entities\UserEntityInterface |
75
|
|
|
*/ |
76
|
|
|
protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client) |
77
|
|
|
{ |
78
|
|
|
$username = $this->getRequestParameter('username', $request); |
79
|
|
|
if (is_null($username)) { |
80
|
|
|
throw OAuthServerException::invalidRequest('username', '`%s` parameter is missing'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$password = $this->getRequestParameter('password', $request); |
84
|
|
|
if (is_null($password)) { |
85
|
|
|
throw OAuthServerException::invalidRequest('password', '`%s` parameter is missing'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$user = $this->userRepository->getUserEntityByUserCredentials( |
89
|
|
|
$username, |
90
|
|
|
$password, |
91
|
|
|
$this->getIdentifier(), |
92
|
|
|
$client |
93
|
|
|
); |
94
|
|
|
if (!$user instanceof UserEntityInterface) { |
95
|
|
|
$this->getEmitter()->emit(new RequestEvent('user.authentication.failed', $request)); |
96
|
|
|
|
97
|
|
|
throw OAuthServerException::invalidCredentials(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $user; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function getIdentifier() |
107
|
|
|
{ |
108
|
|
|
return 'password'; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|