PasswordGrant   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 94.87%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 81
ccs 37
cts 39
cp 0.9487
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateUser() 0 22 2
A respondToAccessTokenRequest() 0 31 2
A getIdentifier() 0 3 1
A __construct() 0 8 1
1
<?php
2
3
/**
4
 * OAuth 2.0 Password grant.
5
 *
6
 * @author      Alex Bilbie <[email protected]>
7
 * @copyright   Copyright (c) Alex Bilbie
8
 * @license     http://mit-license.org/
9
 *
10
 * @link        https://github.com/thephpleague/oauth2-server
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\OAuth2\Server\Grant;
16
17
use DateInterval;
18
use League\OAuth2\Server\Entities\ClientEntityInterface;
19
use League\OAuth2\Server\Entities\UserEntityInterface;
20
use League\OAuth2\Server\Exception\OAuthServerException;
21
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
22
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
23
use League\OAuth2\Server\RequestAccessTokenEvent;
24
use League\OAuth2\Server\RequestEvent;
25
use League\OAuth2\Server\RequestRefreshTokenEvent;
26
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
29
/**
30
 * Password grant class.
31
 */
32
class PasswordGrant extends AbstractGrant
33
{
34 6
    public function __construct(
35
        UserRepositoryInterface $userRepository,
36
        RefreshTokenRepositoryInterface $refreshTokenRepository
37
    ) {
38 6
        $this->setUserRepository($userRepository);
39 6
        $this->setRefreshTokenRepository($refreshTokenRepository);
40
41 6
        $this->refreshTokenTTL = new DateInterval('P1M');
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 5
    public function respondToAccessTokenRequest(
48
        ServerRequestInterface $request,
49
        ResponseTypeInterface $responseType,
50
        DateInterval $accessTokenTTL
51
    ): ResponseTypeInterface {
52
        // Validate request
53 5
        $client = $this->validateClient($request);
54 3
        $scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope));
55 3
        $user = $this->validateUser($request, $client);
56
57 2
        $finalizedScopes = $this->scopeRepository->finalizeScopes(
58 2
            $scopes,
59 2
            $this->getIdentifier(),
60 2
            $client,
61 2
            $user->getIdentifier()
62 2
        );
63
64
        // Issue and persist new access token
65 2
        $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $finalizedScopes);
66 2
        $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken));
67 2
        $responseType->setAccessToken($accessToken);
68
69
        // Issue and persist new refresh token if given
70 2
        $refreshToken = $this->issueRefreshToken($accessToken);
71
72 2
        if ($refreshToken !== null) {
73 1
            $this->getEmitter()->emit(new RequestRefreshTokenEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request, $refreshToken));
74 1
            $responseType->setRefreshToken($refreshToken);
75
        }
76
77 2
        return $responseType;
78
    }
79
80
    /**
81
     * @throws OAuthServerException
82
     */
83 3
    protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client): UserEntityInterface
84
    {
85 3
        $username = $this->getRequestParameter('username', $request)
86
            ?? throw OAuthServerException::invalidRequest('username');
87
88 3
        $password = $this->getRequestParameter('password', $request)
89
            ?? throw OAuthServerException::invalidRequest('password');
90
91 3
        $user = $this->userRepository->getUserEntityByUserCredentials(
92 3
            $username,
93 3
            $password,
94 3
            $this->getIdentifier(),
95 3
            $client
96 3
        );
97
98 3
        if ($user instanceof UserEntityInterface === false) {
99 1
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));
100
101 1
            throw OAuthServerException::invalidCredentials();
102
        }
103
104 2
        return $user;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 5
    public function getIdentifier(): string
111
    {
112 5
        return 'password';
113
    }
114
}
115