-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitialAccessTokenMiddleware.php
More file actions
67 lines (53 loc) · 2.29 KB
/
InitialAccessTokenMiddleware.php
File metadata and controls
67 lines (53 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace OAuth2Framework\Component\ClientRegistrationEndpoint;
use OAuth2Framework\Component\BearerTokenType\BearerToken;
use OAuth2Framework\Component\Core\Message\OAuth2Error;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class InitialAccessTokenMiddleware implements MiddlewareInterface
{
private BearerToken $bearerToken;
private InitialAccessTokenRepository $initialAccessTokenRepository;
private bool $isRequired;
public function __construct(BearerToken $bearerToken, InitialAccessTokenRepository $initialAccessTokenRepository, bool $isRequired)
{
$this->bearerToken = $bearerToken;
$this->initialAccessTokenRepository = $initialAccessTokenRepository;
$this->isRequired = $isRequired;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$values = [];
$token = $this->bearerToken->find($request, $values);
if (null === $token) {
if (!$this->isRequired) {
return $handler->handle($request);
}
throw new \InvalidArgumentException('Initial Access Token is missing or invalid.');
}
$initialAccessToken = $this->initialAccessTokenRepository->find(new InitialAccessTokenId($token));
if (null === $initialAccessToken || $initialAccessToken->isRevoked()) {
throw new \InvalidArgumentException('Initial Access Token is missing or invalid.');
}
if ($initialAccessToken->hasExpired()) {
throw new \InvalidArgumentException('Initial Access Token expired.');
}
$request = $request->withAttribute('initial_access_token', $initialAccessToken);
} catch (\InvalidArgumentException $e) {
throw OAuth2Error::invalidRequest($e->getMessage(), [], $e);
}
return $handler->handle($request);
}
}