Passed
Push — develop ( 7714f7...f140eb )
by nguereza
01:47
created

PasswordGrant::createAuthorizationResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * Platine OAuth2
5
 *
6
 * Platine OAuth2 is a library that implements the OAuth2 specification
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine OAuth2
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
declare(strict_types=1);
32
33
namespace Platine\OAuth2\Grant;
34
35
use Platine\Http\ResponseInterface;
36
use Platine\Http\ServerRequestInterface;
37
use Platine\OAuth2\AuthorizationServerInterface;
38
use Platine\OAuth2\Entity\Client;
39
use Platine\OAuth2\Entity\TokenOwnerInterface;
40
use Platine\OAuth2\Entity\UserAuthenticationInterface;
41
use Platine\OAuth2\Exception\OAuth2Exception;
42
use Platine\OAuth2\Service\AccessTokenService;
43
use Platine\OAuth2\Service\RefreshTokenService;
44
45
/**
46
 * This authorization grant type, also known as "resource owner password credentials", is ideal
47
 * when you trust the client (for instance for a native app)
48
 *
49
 * @class PasswordGrant
50
 * @package Platine\OAuth2\Grant
51
 */
52
class PasswordGrant extends BaseGrant implements AuthorizationServerAwareInterface
53
{
54
    public const GRANT_TYPE = 'password';
55
    public const GRANT_RESPONSE_TYPE = '';
56
57
    /**
58
     * The UserAuthenticationInterface
59
     * @var UserAuthenticationInterface
60
     */
61
    protected UserAuthenticationInterface $userAuthentication;
62
63
    /**
64
     * The AccessTokenService
65
     * @var AccessTokenService
66
     */
67
    protected AccessTokenService $accessTokenService;
68
69
    /**
70
     * The RefreshTokenService
71
     * @var RefreshTokenService
72
     */
73
    protected RefreshTokenService $refreshTokenService;
74
75
    /**
76
     * The authorization server instance
77
     * @var AuthorizationServerInterface|null
78
     */
79
    protected ?AuthorizationServerInterface $authorizationServer = null;
80
81
    /**
82
     * Create new instance
83
     * @param UserAuthenticationInterface $userAuthentication
84
     * @param AccessTokenService $accessTokenService
85
     * @param RefreshTokenService $refreshTokenService
86
     */
87
    public function __construct(
88
        UserAuthenticationInterface $userAuthentication,
89
        AccessTokenService $accessTokenService,
90
        RefreshTokenService $refreshTokenService
91
    ) {
92
        $this->userAuthentication = $userAuthentication;
93
        $this->accessTokenService = $accessTokenService;
94
        $this->refreshTokenService = $refreshTokenService;
95
    }
96
97
        /**
98
     * {@inheritdoc}
99
     */
100
    public function createAuthorizationResponse(
101
        ServerRequestInterface $request,
102
        Client $client,
103
        ?TokenOwnerInterface $owner = null
104
    ): ResponseInterface {
105
        throw OAuth2Exception::invalidRequest('Password grant does not support authorization');
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function createTokenResponse(
112
        ServerRequestInterface $request,
113
        ?Client $client = null,
114
        ?TokenOwnerInterface $owner = null
115
    ): ResponseInterface {
116
        $postParams = (array) $request->getParsedBody();
117
        $username = $postParams['username'] ?? null;
118
        $password = $postParams['password'] ?? null;
119
        $scope = $postParams['scope'] ?? null;
120
        $scopes = is_string($scope) ? explode(' ', $scope) : [];
121
122
        if ($username === null || $password === null) {
123
            throw OAuth2Exception::invalidRequest('Username and/or password is missing in the request');
124
        }
125
126
        $userOwner = $this->userAuthentication->validate($username, $password);
127
        if ($userOwner === null) {
128
            throw OAuth2Exception::accessDenied('Either username or password are incorrect');
129
        }
130
131
        $accessToken = $this->accessTokenService->createToken($userOwner, $client, $scopes);
132
133
        // Before generating a refresh token, we must make sure the
134
        //  authorization server supports this grant
135
        $refreshToken = null;
136
        if (
137
            $this->authorizationServer !== null &&
138
            $this->authorizationServer->hasGrant(RefreshTokenGrant::GRANT_TYPE)
139
        ) {
140
            $refreshToken = $this->refreshTokenService->createToken($userOwner, $client, $scopes);
141
        }
142
143
        return $this->generateTokenResponse($accessToken, $refreshToken);
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function setAuthorizationServer(
150
        AuthorizationServerInterface $authorizationServer
151
    ): void {
152
        $this->authorizationServer = $authorizationServer;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function allowPublicClients(): bool
159
    {
160
        return true;
161
    }
162
}
163