Failed Conditions
Push — master ( 1cda70...d0eca0 )
by Florent
04:03
created

InitialAccessTokenMiddleware::process()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 11
nop 2
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\ClientRegistrationEndpoint;
15
16
use Psr\Http\Server\RequestHandlerInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use OAuth2Framework\Component\BearerTokenType\BearerToken;
19
use OAuth2Framework\Component\Core\Message\OAuth2Message;
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
     * @var bool
37
     */
38
    private $isRequired;
39
40
    /**
41
     * InitialAccessTokenMiddleware constructor.
42
     *
43
     * @param BearerToken                  $bearerToken
44
     * @param InitialAccessTokenRepository $initialAccessTokenRepository
45
     * @param bool                         $isRequired
46
     */
47
    public function __construct(BearerToken $bearerToken, InitialAccessTokenRepository $initialAccessTokenRepository, bool $isRequired)
48
    {
49
        $this->bearerToken = $bearerToken;
50
        $this->initialAccessTokenRepository = $initialAccessTokenRepository;
51
        $this->isRequired = $isRequired;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58
    {
59
        try {
60
            $values = [];
61
            $token = $this->bearerToken->find($request, $values);
62
            if (null === $token) {
63
                if (!$this->isRequired) {
64
                    return $handler->handle($request);
65
                }
66
                throw new \InvalidArgumentException('Initial Access Token is missing or invalid.');
67
            }
68
69
            $initialAccessToken = $this->initialAccessTokenRepository->find(InitialAccessTokenId::create($token));
70
71
            if (null === $initialAccessToken || $initialAccessToken->isRevoked()) {
72
                throw new \InvalidArgumentException('Initial Access Token is missing or invalid.');
73
            }
74
            if ($initialAccessToken->hasExpired()) {
75
                throw new \InvalidArgumentException('Initial Access Token expired.');
76
            }
77
78
            $request = $request->withAttribute('initial_access_token', $initialAccessToken);
79
        } catch (\InvalidArgumentException $e) {
80
            throw new OAuth2Message(400, OAuth2Message::ERROR_INVALID_REQUEST, $e->getMessage(), [], $e);
81
        }
82
83
        return $handler->handle($request);
84
    }
85
}
86