1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\components\server\grants; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
6
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
7
|
|
|
use League\OAuth2\Server\Grant\PasswordGrant; |
8
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2PasswordGrantInterface; |
11
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\user\Oauth2PasswordGrantUserComponentInterface; |
12
|
|
|
use Yii; |
13
|
|
|
|
14
|
|
|
class Oauth2PasswordGrant extends PasswordGrant implements Oauth2PasswordGrantInterface |
15
|
|
|
{ |
16
|
|
|
protected $validatedUser = null; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @inheritDoc |
20
|
|
|
*/ |
21
|
|
|
protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client) |
22
|
|
|
{ |
23
|
|
|
$this->validatedUser = parent::validateUser($request, $client); |
24
|
|
|
if (Yii::$app->user instanceof Oauth2PasswordGrantUserComponentInterface) { |
25
|
|
|
if (!Yii::$app->user->beforeOauth2PasswordGrantLogin($this->validatedUser, $client, $this)) { |
26
|
|
|
Yii::info('Login rejected by `beforeOauthPasswordGrantLogin()`.', 'oauth2'); |
27
|
|
|
$this->validatedUser = null; |
28
|
|
|
throw OAuthServerException::invalidCredentials(); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->validatedUser; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function respondToAccessTokenRequest( |
39
|
|
|
ServerRequestInterface $request, |
40
|
|
|
ResponseTypeInterface $responseType, |
41
|
|
|
\DateInterval $accessTokenTTL |
42
|
|
|
) |
43
|
|
|
{ |
|
|
|
|
44
|
|
|
$responseType = parent::respondToAccessTokenRequest($request, $responseType, $accessTokenTTL); |
45
|
|
|
if (Yii::$app->user instanceof Oauth2PasswordGrantUserComponentInterface) { |
46
|
|
|
Yii::$app->user->afterOauth2PasswordGrantLogin($this->validatedUser, $this); |
47
|
|
|
} |
48
|
|
|
return $responseType; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|