Passed
Push — master ( e9591a...c0a685 )
by Rutger
13:20
created

Oauth2PersonalAccessTokenGrant   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 13.51%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 33
c 2
b 0
f 0
dl 0
loc 95
ccs 5
cts 37
cp 0.1351
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validateClient() 0 10 2
A validateUser() 0 15 3
A getIdentifier() 0 3 1
A __construct() 0 6 1
A respondToAccessTokenRequest() 0 23 1
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
use rhertogh\Yii2Oauth2Server\Oauth2Module;
19
20
class Oauth2PersonalAccessTokenGrant extends AbstractGrant implements Oauth2PersonalAccessTokenGrantInterface
21
{
22
    use Oauth2GrantTrait;
23
24
    /**
25
     * @var Oauth2UserRepositoryInterface
26
     */
27
    protected $userRepository;
28
29
    /**
30
     * @param Oauth2AccessTokenRepositoryInterface     $accessTokenRepository
31
     *
32
     * @throws \Exception
33
     */
34 3
    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
    ) {
38 3
        $this->setUserRepository($userRepository);
39 3
        $this->setAccessTokenRepository($accessTokenRepository);
40
    }
41
42 3
    public function getIdentifier()
43
    {
44 3
        return Oauth2Module::GRANT_TYPE_IDENTIFIER_PERSONAL_ACCESS_TOKEN;
45
    }
46
47
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseTypeInterface $responseType, DateInterval $accessTokenTTL)
48
    {
49
        $client = $this->validateClient($request);
50
        $user = $this->validateUser($request);
51
        $scopes = $this->validateScopes($this->getRequestParameter('scope', $request));
52
53
        $scopes = $this->scopeRepository->finalizeScopes(
54
            $scopes,
55
            $this->getIdentifier(),
56
            $client,
57
            $user->getIdentifier(),
58
        );
59
60
        $accessToken = $this->issueAccessToken(
61
            $accessTokenTTL,
62
            $client,
63
            $user->getIdentifier(),
64
            $scopes
65
        );
66
67
        $responseType->setAccessToken($accessToken);
68
69
        return $responseType;
70
    }
71
72
    /**
73
     * Validate the client.
74
     *
75
     * @param ServerRequestInterface $request
76
     *
77
     * @throws OAuthServerException
78
     *
79
     * @return ClientEntityInterface
80
     */
81
    protected function validateClient(ServerRequestInterface $request)
82
    {
83
        [$clientId, $clientSecret] = $this->getClientCredentials($request);
84
85
        if ($this->clientRepository->validateClient($clientId, $clientSecret, $this->getIdentifier()) === false) {
86
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
87
            throw OAuthServerException::invalidClient($request);
88
        }
89
90
        return $this->getClientEntityOrFail($clientId, $request);
91
    }
92
93
    /**
94
     * @param ServerRequestInterface $request
95
     *
96
     * @return UserEntityInterface
97
     *@throws OAuthServerException
0 ignored issues
show
Coding Style introduced by
Expected 1 space after asterisk; 0 found
Loading history...
98
     *
99
     */
100
    protected function validateUser(ServerRequestInterface $request)
101
    {
102
        $userIdentifier = $this->getRequestParameter('user_id', $request);
103
104
        if (empty($userIdentifier)) {
105
            throw OAuthServerException::invalidRequest('user_id');
106
        }
107
108
        $user = $this->userRepository->getUserEntityByIdentifier($userIdentifier);
109
110
        if (empty($user)) {
111
            throw Oauth2ServerException::accessDenied();
112
        }
113
114
        return $user;
115
    }
116
117
118
}
119