Failed Conditions
Push — ng ( ada769...ebc492 )
by Florent
08:29 queued 40s
created

InitialAccessTokenMiddleware   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B process() 0 24 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Server\ClientRegistrationEndpoint;
15
16
use Interop\Http\Server\RequestHandlerInterface;
17
use Interop\Http\Server\MiddlewareInterface;
18
use OAuth2Framework\Component\Server\BearerTokenType\BearerToken;
19
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
23
final class InitialAccessTokenMiddleware implements MiddlewareInterface
24
{
25
    /**
26
     * @var BearerToken
27
     */
28
    private $bearerToken;
29
30
    /**
31
     * @var InitialAccessTokenRepository
32
     */
33
    private $initialAccessTokenRepository;
34
35
    /**
36
     * InitialAccessTokenMiddleware constructor.
37
     *
38
     * @param BearerToken                  $bearerToken
39
     * @param InitialAccessTokenRepository $initialAccessTokenRepository
40
     */
41
    public function __construct(BearerToken $bearerToken, InitialAccessTokenRepository $initialAccessTokenRepository)
42
    {
43
        $this->bearerToken = $bearerToken;
44
        $this->initialAccessTokenRepository = $initialAccessTokenRepository;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
51
    {
52
        try {
53
            $values = [];
54
            $token = $this->bearerToken->findToken($request, $values);
55
            if (null === $token) {
56
                throw new \InvalidArgumentException('Initial Access Token is missing or invalid.');
57
            }
58
59
            $initialAccessToken = $this->initialAccessTokenRepository->find(InitialAccessTokenId::create($token));
60
            if (null === $initialAccessToken || $initialAccessToken->isRevoked()) {
61
                throw new \InvalidArgumentException('Initial Access Token is missing or invalid.');
62
            }
63
            if ($initialAccessToken->hasExpired()) {
64
                throw new \InvalidArgumentException('Initial Access Token expired.');
65
            }
66
67
            $request = $request->withAttribute('initial_access_token', $initialAccessToken);
68
        } catch (\InvalidArgumentException $e) {
69
            throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_REQUEST, $e->getMessage(), [], $e);
70
        }
71
72
        return $handler->handle($request);
73
    }
74
}
75