InitialAccessTokenMiddleware   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 28 7
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\ClientRegistrationEndpoint;
15
16
use OAuth2Framework\Component\BearerTokenType\BearerToken;
17
use OAuth2Framework\Component\Core\Message\OAuth2Error;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use Psr\Http\Server\MiddlewareInterface;
21
use Psr\Http\Server\RequestHandlerInterface;
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
     * @var bool
37
     */
38
    private $isRequired;
39
40
    /**
41
     * InitialAccessTokenMiddleware constructor.
42
     */
43
    public function __construct(BearerToken $bearerToken, InitialAccessTokenRepository $initialAccessTokenRepository, bool $isRequired)
44
    {
45
        $this->bearerToken = $bearerToken;
46
        $this->initialAccessTokenRepository = $initialAccessTokenRepository;
47
        $this->isRequired = $isRequired;
48
    }
49
50
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
51
    {
52
        try {
53
            $values = [];
54
            $token = $this->bearerToken->find($request, $values);
55
            if (null === $token) {
56
                if (!$this->isRequired) {
57
                    return $handler->handle($request);
58
                }
59
60
                throw new \InvalidArgumentException('Initial Access Token is missing or invalid.');
61
            }
62
63
            $initialAccessToken = $this->initialAccessTokenRepository->find(new InitialAccessTokenId($token));
64
65
            if (null === $initialAccessToken || $initialAccessToken->isRevoked()) {
66
                throw new \InvalidArgumentException('Initial Access Token is missing or invalid.');
67
            }
68
            if ($initialAccessToken->hasExpired()) {
69
                throw new \InvalidArgumentException('Initial Access Token expired.');
70
            }
71
72
            $request = $request->withAttribute('initial_access_token', $initialAccessToken);
73
        } catch (\InvalidArgumentException $e) {
74
            throw OAuth2Error::invalidRequest($e->getMessage(), [], $e);
75
        }
76
77
        return $handler->handle($request);
78
    }
79
}
80