1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\components\server\grants\traits; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
6
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use rhertogh\Yii2Oauth2Server\events\Oauth2AccessTokenIssuanceEvent; |
9
|
|
|
use rhertogh\Yii2Oauth2Server\events\Oauth2AuthCodeIssuanceEvent; |
10
|
|
|
use rhertogh\Yii2Oauth2Server\events\Oauth2RefreshTokenIssuanceEvent; |
11
|
|
|
use rhertogh\Yii2Oauth2Server\helpers\UrlHelper; |
12
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface; |
13
|
|
|
use rhertogh\Yii2Oauth2Server\Oauth2Module; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\InvalidConfigException; |
16
|
|
|
|
17
|
|
|
trait Oauth2GrantTrait |
18
|
|
|
{ |
19
|
|
|
/** @var Oauth2Module */ |
20
|
|
|
public $module; |
21
|
|
|
|
22
|
|
|
protected function issueAuthCode(\DateInterval $authCodeTTL, ClientEntityInterface $client, $userIdentifier, $redirectUri, array $scopes = []) |
23
|
|
|
{ |
24
|
|
|
/** @var Oauth2AuthCodeIssuanceEvent $event */ |
25
|
|
|
$event = Yii::createObject([ |
26
|
|
|
'class' => Oauth2AuthCodeIssuanceEvent::class, |
27
|
|
|
'grant' => $this, |
28
|
|
|
'authCodeTTL' => $authCodeTTL, |
29
|
|
|
'client' => $client, |
30
|
|
|
'userIdentifier' => $userIdentifier, |
31
|
|
|
'redirectUri' => $redirectUri, |
32
|
|
|
'scopes' => $scopes, |
33
|
|
|
]); |
34
|
|
|
|
35
|
|
|
$this->module->trigger(Oauth2Module::EVENT_BEFORE_AUTH_CODE_ISSUANCE, $event); |
36
|
|
|
|
37
|
|
|
if (!$event->authCode) { |
38
|
|
|
$event->authCode = parent::issueAuthCode($authCodeTTL, $client, $userIdentifier, $redirectUri, $scopes); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->module->trigger(Oauth2Module::EVENT_AFTER_AUTH_CODE_ISSUANCE, $event); |
42
|
|
|
|
43
|
|
|
return $event->authCode; |
44
|
|
|
} |
45
|
|
|
|
46
|
8 |
|
protected function issueAccessToken(\DateInterval $accessTokenTTL, ClientEntityInterface $client, $userIdentifier, array $scopes = []) |
47
|
|
|
{ |
48
|
|
|
/** @var Oauth2AccessTokenIssuanceEvent $event */ |
49
|
8 |
|
$event = Yii::createObject([ |
50
|
8 |
|
'class' => Oauth2AccessTokenIssuanceEvent::class, |
51
|
8 |
|
'grant' => $this, |
52
|
8 |
|
'accessTokenTTL' => $accessTokenTTL, |
53
|
8 |
|
'client' => $client, |
54
|
8 |
|
'userIdentifier' => $userIdentifier, |
55
|
8 |
|
'scopes' => $scopes, |
56
|
8 |
|
]); |
57
|
|
|
|
58
|
8 |
|
$this->module->trigger(Oauth2Module::EVENT_BEFORE_ACCESS_TOKEN_ISSUANCE, $event); |
59
|
|
|
|
60
|
8 |
|
if (!$event->accessToken) { |
61
|
8 |
|
$event->accessToken = parent::issueAccessToken($accessTokenTTL, $client, $userIdentifier, $scopes); // TODO: Change the autogenerated stub |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
$this->module->trigger(Oauth2Module::EVENT_AFTER_ACCESS_TOKEN_ISSUANCE, $event); |
65
|
|
|
|
66
|
8 |
|
return $event->accessToken; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
70
|
|
|
{ |
71
|
|
|
/** @var Oauth2RefreshTokenIssuanceEvent $event */ |
72
|
4 |
|
$event = Yii::createObject([ |
73
|
4 |
|
'class' => Oauth2RefreshTokenIssuanceEvent::class, |
74
|
4 |
|
'grant' => $this, |
75
|
4 |
|
'accessToken' => $accessToken, |
76
|
4 |
|
]); |
77
|
|
|
|
78
|
4 |
|
$this->module->trigger(Oauth2Module::EVENT_BEFORE_REFRESH_TOKEN_ISSUANCE, $event); |
79
|
|
|
|
80
|
4 |
|
if (!$event->refreshToken) { |
81
|
4 |
|
$event->refreshToken = parent::issueRefreshToken($accessToken); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
$this->module->trigger(Oauth2Module::EVENT_AFTER_REFRESH_TOKEN_ISSUANCE, $event); |
85
|
|
|
|
86
|
4 |
|
return $event->refreshToken; |
87
|
|
|
} |
88
|
|
|
|
89
|
60 |
|
protected function validateRedirectUri( |
90
|
|
|
string $redirectUri, |
91
|
|
|
ClientEntityInterface $client, |
92
|
|
|
ServerRequestInterface $request |
93
|
|
|
) { |
94
|
60 |
|
if (!($client instanceof Oauth2ClientInterface)) { |
95
|
|
|
throw new InvalidConfigException(get_class($client) . ' must implement ' . Oauth2ClientInterface::class); |
96
|
|
|
} |
97
|
|
|
|
98
|
60 |
|
if ($client->isVariableRedirectUriQueryAllowed()) { |
99
|
24 |
|
$redirectUri = UrlHelper::stripQueryAndFragment($redirectUri); |
100
|
|
|
} |
101
|
|
|
|
102
|
60 |
|
parent::validateRedirectUri($redirectUri, $client, $request); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|