Passed
Push — master ( 60854d...29b3a1 )
by Rutger
03:10
created

respondToAccessTokenRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 11
cp 0
rs 9.7666
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\server\grants;
4
5
use DateInterval;
6
use League\OAuth2\Server\Entities\ClientEntityInterface;
7
use League\OAuth2\Server\Entities\UserEntityInterface;
8
use League\OAuth2\Server\Exception\OAuthServerException;
9
use League\OAuth2\Server\Grant\AbstractGrant;
10
use League\OAuth2\Server\RequestEvent;
11
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use rhertogh\Yii2Oauth2Server\components\server\grants\traits\Oauth2GrantTrait;
14
use rhertogh\Yii2Oauth2Server\exceptions\Oauth2ServerException;
15
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2AccessTokenRepositoryInterface;
16
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2UserRepositoryInterface;
17
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2PersonalAccessTokenGrantInterface;
18
19
class Oauth2PersonalAccessTokenGrant extends AbstractGrant implements Oauth2PersonalAccessTokenGrantInterface
20
{
21
    use Oauth2GrantTrait;
22
23
    /**
24
     * @var Oauth2UserRepositoryInterface
25
     */
26
    protected $userRepository;
27
28
    /**
29
     * @param Oauth2AccessTokenRepositoryInterface     $accessTokenRepository
30
     * @param DateInterval                    $authCodeTTL
31
     *
32
     * @throws \Exception
33
     */
34
    public function __construct(
35
        Oauth2UserRepositoryInterface        $userRepository,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$userRepository"; 8 found
Loading history...
36
        Oauth2AccessTokenRepositoryInterface $accessTokenRepository,
37
        DateInterval                         $authCodeTTL
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$authCodeTTL"; 25 found
Loading history...
38
    ) {
39
        $this->setUserRepository($userRepository);
40
        $this->setAccessTokenRepository($accessTokenRepository);
41
        $this->authCodeTTL = $authCodeTTL;
0 ignored issues
show
Bug Best Practice introduced by
The property authCodeTTL does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
    }
43
44
    public function getIdentifier()
45
    {
46
        return 'personal_access_token';
47
    }
48
49
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseTypeInterface $responseType, DateInterval $accessTokenTTL)
50
    {
51
        $client = $this->validateClient($request);
52
        $user = $this->validateUser($request);
53
        $scopes = $this->validateScopes($this->getRequestParameter('scope', $request));
54
55
        $scopes = $this->scopeRepository->finalizeScopes(
56
            $scopes,
57
            $this->getIdentifier(),
58
            $client,
59
            $user->getIdentifier(),
60
        );
61
62
        $accessToken = $this->issueAccessToken(
63
            $accessTokenTTL,
64
            $client,
65
            $user->getIdentifier(),
66
            $scopes
67
        );
68
69
        $responseType->setAccessToken($accessToken);
70
71
        return $responseType;
72
    }
73
74
    /**
75
     * Validate the client.
76
     *
77
     * @param ServerRequestInterface $request
78
     *
79
     * @throws OAuthServerException
80
     *
81
     * @return ClientEntityInterface
82
     */
83
    protected function validateClient(ServerRequestInterface $request)
84
    {
85
        [$clientId, $clientSecret] = $this->getClientCredentials($request);
86
87
        if ($this->clientRepository->validateClient($clientId, $clientSecret, $this->getIdentifier()) === false) {
88
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
89
            throw OAuthServerException::invalidClient($request);
90
        }
91
92
        return $this->getClientEntityOrFail($clientId, $request);
93
    }
94
95
    /**
96
     * @param ServerRequestInterface $request
97
     *
98
     * @return UserEntityInterface
99
     *@throws OAuthServerException
0 ignored issues
show
Coding Style introduced by
Expected 1 space after asterisk; 0 found
Loading history...
100
     *
101
     */
102
    protected function validateUser(ServerRequestInterface $request)
103
    {
104
        $userIdentifier = $this->getRequestParameter('user_id', $request);
105
106
        if (empty($userIdentifier)) {
107
            throw OAuthServerException::invalidRequest('user_id');
108
        }
109
110
        $user = $this->userRepository->getUserEntityByIdentifier($userIdentifier);
111
112
        if (empty($user)) {
113
            throw Oauth2ServerException::accessDenied();
114
        }
115
116
        return $user;
117
    }
118
119
120
}
121