Passed
Push — master ( 5dabbc...d41c5c )
by Rutger
02:55
created

Oauth2GrantTrait::issueAuthCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 9.8333
cc 2
nc 2
nop 5
crap 2
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 1
    protected function issueAuthCode(\DateInterval $authCodeTTL, ClientEntityInterface $client, $userIdentifier, $redirectUri, array $scopes = [])
23
    {
24
        /** @var Oauth2AuthCodeIssuanceEvent $event */
25 1
        $event = Yii::createObject([
26 1
            'class' => Oauth2AuthCodeIssuanceEvent::class,
27 1
            'grant' => $this,
28 1
            'authCodeTTL' => $authCodeTTL,
29 1
            'client' => $client,
30 1
            'userIdentifier' => $userIdentifier,
31 1
            'redirectUri' => $redirectUri,
32 1
            'scopes' => $scopes,
33 1
        ]);
34
35 1
        $this->module->trigger(Oauth2Module::EVENT_BEFORE_AUTH_CODE_ISSUANCE, $event);
36
37 1
        if (!$event->authCode) {
38 1
            $event->authCode = parent::issueAuthCode($authCodeTTL, $client, $userIdentifier, $redirectUri, $scopes);
39
        }
40
41 1
        $this->module->trigger(Oauth2Module::EVENT_AFTER_AUTH_CODE_ISSUANCE, $event);
42
43 1
        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
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
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 61
    protected function validateRedirectUri(
90
        string $redirectUri,
91
        ClientEntityInterface $client,
92
        ServerRequestInterface $request
93
    ) {
94 61
        if (!($client instanceof Oauth2ClientInterface)) {
95 1
            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