|
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 rhertogh\Yii2Oauth2Server\events\Oauth2AccessTokenIssuanceEvent; |
|
8
|
|
|
use rhertogh\Yii2Oauth2Server\events\Oauth2AuthCodeIssuanceEvent; |
|
9
|
|
|
use rhertogh\Yii2Oauth2Server\events\Oauth2RefreshTokenIssuanceEvent; |
|
10
|
|
|
use rhertogh\Yii2Oauth2Server\Oauth2Module; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
|
|
13
|
|
|
trait Oauth2GrantTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Oauth2Module */ |
|
16
|
|
|
public $module; |
|
17
|
|
|
|
|
18
|
|
|
protected function issueAuthCode(\DateInterval $authCodeTTL, ClientEntityInterface $client, $userIdentifier, $redirectUri, array $scopes = []) |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Oauth2AuthCodeIssuanceEvent $event */ |
|
21
|
|
|
$event = Yii::createObject([ |
|
22
|
|
|
'class' => Oauth2AuthCodeIssuanceEvent::class, |
|
23
|
|
|
'grant' => $this, |
|
24
|
|
|
'authCodeTTL' => $authCodeTTL, |
|
25
|
|
|
'client' => $client, |
|
26
|
|
|
'userIdentifier' => $userIdentifier, |
|
27
|
|
|
'redirectUri' => $redirectUri, |
|
28
|
|
|
'scopes' => $scopes, |
|
29
|
|
|
]); |
|
30
|
|
|
|
|
31
|
|
|
$this->module->trigger(Oauth2Module::EVENT_BEFORE_AUTH_CODE_ISSUANCE, $event); |
|
32
|
|
|
|
|
33
|
|
|
if (!$event->authCode) { |
|
34
|
|
|
$event->authCode = parent::issueAuthCode($authCodeTTL, $client, $userIdentifier, $redirectUri, $scopes); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->module->trigger(Oauth2Module::EVENT_AFTER_AUTH_CODE_ISSUANCE, $event); |
|
38
|
|
|
|
|
39
|
|
|
return $event->authCode; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
8 |
|
protected function issueAccessToken(\DateInterval $accessTokenTTL, ClientEntityInterface $client, $userIdentifier, array $scopes = []) |
|
43
|
|
|
{ |
|
44
|
|
|
/** @var Oauth2AccessTokenIssuanceEvent $event */ |
|
45
|
8 |
|
$event = Yii::createObject([ |
|
46
|
|
|
'class' => Oauth2AccessTokenIssuanceEvent::class, |
|
47
|
|
|
'grant' => $this, |
|
48
|
|
|
'accessTokenTTL' => $accessTokenTTL, |
|
49
|
|
|
'client' => $client, |
|
50
|
|
|
'userIdentifier' => $userIdentifier, |
|
51
|
|
|
'scopes' => $scopes, |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
8 |
|
$this->module->trigger(Oauth2Module::EVENT_BEFORE_ACCESS_TOKEN_ISSUANCE, $event); |
|
55
|
|
|
|
|
56
|
8 |
|
if (!$event->accessToken) { |
|
57
|
8 |
|
$event->accessToken = parent::issueAccessToken($accessTokenTTL, $client, $userIdentifier, $scopes); // TODO: Change the autogenerated stub |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
8 |
|
$this->module->trigger(Oauth2Module::EVENT_AFTER_ACCESS_TOKEN_ISSUANCE, $event); |
|
61
|
|
|
|
|
62
|
8 |
|
return $event->accessToken; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var Oauth2RefreshTokenIssuanceEvent $event */ |
|
68
|
4 |
|
$event = Yii::createObject([ |
|
69
|
|
|
'class' => Oauth2RefreshTokenIssuanceEvent::class, |
|
70
|
|
|
'grant' => $this, |
|
71
|
|
|
'accessToken' => $accessToken, |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
4 |
|
$this->module->trigger(Oauth2Module::EVENT_BEFORE_REFRESH_TOKEN_ISSUANCE, $event); |
|
75
|
|
|
|
|
76
|
4 |
|
if (!$event->refreshToken) { |
|
77
|
4 |
|
$event->refreshToken = parent::issueRefreshToken($accessToken); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
$this->module->trigger(Oauth2Module::EVENT_AFTER_REFRESH_TOKEN_ISSUANCE, $event); |
|
81
|
|
|
|
|
82
|
4 |
|
return $event->refreshToken; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|