Passed
Pull Request — master (#1122)
by Andrew
04:11 queued 31s
created

PasswordGrant::validateUser()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0312

Importance

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