Completed
Pull Request — master (#997)
by TEst
02:35 queued 48s
created

ImplicitGrant   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 89.89%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 8
dl 0
loc 212
ccs 80
cts 89
cp 0.8989
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setRefreshTokenTTL() 0 4 1
A setRefreshTokenRepository() 0 4 1
A canRespondToAccessTokenRequest() 0 4 1
A getIdentifier() 0 4 1
A respondToAccessTokenRequest() 0 7 1
A canRespondToAuthorizationRequest() 0 8 3
B validateAuthorizationRequest() 0 58 9
B completeAuthorizationRequest() 0 57 5
1
<?php
2
/**
3
 * @author      Alex Bilbie <[email protected]>
4
 * @copyright   Copyright (c) Alex Bilbie
5
 * @license     http://mit-license.org/
6
 *
7
 * @link        https://github.com/thephpleague/oauth2-server
8
 */
9
10
namespace League\OAuth2\Server\Grant;
11
12
use DateInterval;
13
use DateTime;
14
use League\OAuth2\Server\Entities\ClientEntityInterface;
15
use League\OAuth2\Server\Entities\UserEntityInterface;
16
use League\OAuth2\Server\Exception\OAuthServerException;
17
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
18
use League\OAuth2\Server\RequestEvent;
19
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
20
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
21
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
22
use LogicException;
23
use Psr\Http\Message\ServerRequestInterface;
24
25
class ImplicitGrant extends AbstractAuthorizeGrant
26
{
27
    /**
28
     * @var DateInterval
29
     */
30
    private $accessTokenTTL;
31
32
    /**
33
     * @var string
34
     */
35
    private $queryDelimiter;
36
37
    /**
38
     * @param DateInterval $accessTokenTTL
39
     * @param string       $queryDelimiter
40
     */
41 18
    public function __construct(DateInterval $accessTokenTTL, $queryDelimiter = '#')
42
    {
43 18
        $this->accessTokenTTL = $accessTokenTTL;
44 18
        $this->queryDelimiter = $queryDelimiter;
45 18
    }
46
47
    /**
48
     * @param DateInterval $refreshTokenTTL
49
     *
50
     * @throw LogicException
51
     */
52 1
    public function setRefreshTokenTTL(DateInterval $refreshTokenTTL)
53
    {
54 1
        throw new LogicException('The Implicit Grant does not return refresh tokens');
55
    }
56
57
    /**
58
     * @param RefreshTokenRepositoryInterface $refreshTokenRepository
59
     *
60
     * @throw LogicException
61
     */
62 1
    public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository)
63
    {
64 1
        throw new LogicException('The Implicit Grant does not return refresh tokens');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function canRespondToAccessTokenRequest(ServerRequestInterface $request)
71
    {
72 1
        return false;
73
    }
74
75
    /**
76
     * Return the grant identifier that can be used in matching up requests.
77
     *
78
     * @return string
79
     */
80 10
    public function getIdentifier()
81
    {
82 10
        return 'implicit';
83
    }
84
85
    /**
86
     * Respond to an incoming request.
87
     *
88
     * @param ServerRequestInterface $request
89
     * @param ResponseTypeInterface  $responseType
90
     * @param DateInterval           $accessTokenTTL
91
     *
92
     * @return ResponseTypeInterface
93
     */
94 1
    public function respondToAccessTokenRequest(
95
        ServerRequestInterface $request,
96
        ResponseTypeInterface $responseType,
97
        DateInterval $accessTokenTTL
98
    ) {
99 1
        throw new LogicException('This grant does not used this method');
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function canRespondToAuthorizationRequest(ServerRequestInterface $request)
106
    {
107
        return (
108 1
            isset($request->getQueryParams()['response_type'])
109 1
            && $request->getQueryParams()['response_type'] === 'token'
110 1
            && isset($request->getQueryParams()['client_id'])
111
        );
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 6
    public function validateAuthorizationRequest(ServerRequestInterface $request)
118
    {
119 6
        $clientId = $this->getQueryStringParameter(
120 6
            'client_id',
121 6
            $request,
122 6
            $this->getServerParameter('PHP_AUTH_USER', $request)
123
        );
124
125 6
        if (is_null($clientId)) {
126 1
            throw OAuthServerException::invalidRequest('client_id');
127
        }
128
129 5
        $client = $this->clientRepository->getClientEntity(
130 5
            $clientId,
131 5
            $this->getIdentifier(),
132 5
            null,
133 5
            false
134
        );
135
136 5
        if ($client instanceof ClientEntityInterface === false) {
137 1
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
138 1
            throw OAuthServerException::invalidClient();
139
        }
140
141 4
        $redirectUri = $this->getQueryStringParameter('redirect_uri', $request);
142
143 4
        if ($redirectUri !== null) {
144 4
            $this->validateRedirectUri($redirectUri, $client, $request);
145
        } elseif (is_array($client->getRedirectUri()) && count($client->getRedirectUri()) !== 1
146
            || empty($client->getRedirectUri())) {
147
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
148
            throw OAuthServerException::invalidClient();
149
        } else {
150
            $redirectUri = is_array($client->getRedirectUri())
151
                ? $client->getRedirectUri()[0]
152
                : $client->getRedirectUri();
153
        }
154
155 2
        $scopes = $this->validateScopes(
156 2
            $this->getQueryStringParameter('scope', $request, $this->defaultScope),
157 2
            $redirectUri
158
        );
159
160 2
        $stateParameter = $this->getQueryStringParameter('state', $request);
161
162 2
        $authorizationRequest = new AuthorizationRequest();
163 2
        $authorizationRequest->setGrantTypeId($this->getIdentifier());
164 2
        $authorizationRequest->setClient($client);
165 2
        $authorizationRequest->setRedirectUri($redirectUri);
166
167 2
        if ($stateParameter !== null) {
168
            $authorizationRequest->setState($stateParameter);
169
        }
170
171 2
        $authorizationRequest->setScopes($scopes);
172
173 2
        return $authorizationRequest;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 6
    public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest)
180
    {
181 6
        if ($authorizationRequest->getUser() instanceof UserEntityInterface === false) {
182 1
            throw new LogicException('An instance of UserEntityInterface should be set on the AuthorizationRequest');
183
        }
184
185 5
        $finalRedirectUri = ($authorizationRequest->getRedirectUri() === null)
186 5
            ? is_array($authorizationRequest->getClient()->getRedirectUri())
187
                ? $authorizationRequest->getClient()->getRedirectUri()[0]
188 5
                : $authorizationRequest->getClient()->getRedirectUri()
189 5
            : $authorizationRequest->getRedirectUri();
190
191
        // The user approved the client, redirect them back with an access token
192 5
        if ($authorizationRequest->isAuthorizationApproved() === true) {
193
            // Finalize the requested scopes
194 4
            $finalizedScopes = $this->scopeRepository->finalizeScopes(
195 4
                $authorizationRequest->getScopes(),
196 4
                $this->getIdentifier(),
197 4
                $authorizationRequest->getClient(),
198 4
                $authorizationRequest->getUser()->getIdentifier()
199
            );
200
201 4
            $accessToken = $this->issueAccessToken(
202 4
                $this->accessTokenTTL,
203 4
                $authorizationRequest->getClient(),
204 4
                $authorizationRequest->getUser()->getIdentifier(),
205 4
                $finalizedScopes
206
            );
207
208 2
            $response = new RedirectResponse();
209 2
            $response->setRedirectUri(
210 2
                $this->makeRedirectUri(
211 2
                    $finalRedirectUri,
212
                    [
213 2
                        'access_token' => (string) $accessToken->convertToJWT($this->privateKey),
214 2
                        'token_type'   => 'Bearer',
215 2
                        'expires_in'   => $accessToken->getExpiryDateTime()->getTimestamp() - (new DateTime())->getTimestamp(),
216 2
                        'state'        => $authorizationRequest->getState(),
217
                    ],
218 2
                    $this->queryDelimiter
219
                )
220
            );
221
222 2
            return $response;
223
        }
224
225
        // The user denied the client, redirect them back with an error
226 1
        throw OAuthServerException::accessDenied(
227 1
            'The user denied the request',
228 1
            $this->makeRedirectUri(
229 1
                $finalRedirectUri,
230
                [
231 1
                    'state' => $authorizationRequest->getState(),
232
                ]
233
            )
234
        );
235
    }
236
}
237