Completed
Push — master ( 1de13c...bf55ce )
by Alex
33:38
created

BearerTokenValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
c 2
b 1
f 0
lcom 1
cbo 9
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validateAuthorization() 0 40 6
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\AuthorizationValidators;
11
12
use Lcobucci\JWT\Parser;
13
use Lcobucci\JWT\Signer\Rsa\Sha256;
14
use Lcobucci\JWT\ValidationData;
15
use League\OAuth2\Server\CryptTrait;
16
use League\OAuth2\Server\Exception\OAuthServerException;
17
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
20
class BearerTokenValidator implements AuthorizationValidatorInterface
21
{
22
    use CryptTrait;
23
24
    /**
25
     * @var \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface
26
     */
27
    private $accessTokenRepository;
28
29
    /**
30
     * BearerTokenValidator constructor.
31
     *
32
     * @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository
33
     */
34
    public function __construct(AccessTokenRepositoryInterface $accessTokenRepository)
35
    {
36
        $this->accessTokenRepository = $accessTokenRepository;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function validateAuthorization(ServerRequestInterface $request)
43
    {
44
        if ($request->hasHeader('authorization') === false) {
45
            throw OAuthServerException::accessDenied('Missing "Authorization" header');
46
        }
47
48
        $header = $request->getHeader('authorization');
49
        $jwt = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header[0]));
50
51
        try {
52
            // Attempt to parse and validate the JWT
53
            $token = (new Parser())->parse($jwt);
54
            if ($token->verify(new Sha256(), $this->publicKey->getKeyPath()) === false) {
55
                throw OAuthServerException::accessDenied('Access token could not be verified');
56
            }
57
58
            // Ensure access token hasn't expired
59
            $data = new ValidationData();
60
            $data->setCurrentTime(time());
61
62
            if ($token->validate($data) === false) {
63
                throw OAuthServerException::accessDenied('Access token is invalid');
64
            }
65
66
            // Check if token has been revoked
67
            if ($this->accessTokenRepository->isAccessTokenRevoked($token->getClaim('jti'))) {
68
                throw OAuthServerException::accessDenied('Access token has been revoked');
69
            }
70
71
            // Return the request with additional attributes
72
            return $request
73
                ->withAttribute('oauth_access_token_id', $token->getClaim('jti'))
74
                ->withAttribute('oauth_client_id', $token->getClaim('aud'))
75
                ->withAttribute('oauth_user_id', $token->getClaim('sub'))
76
                ->withAttribute('oauth_scopes', $token->getClaim('scopes'));
77
        } catch (\InvalidArgumentException $exception) {
78
            // JWT couldn't be parsed so return the request as is
79
            throw OAuthServerException::accessDenied($exception->getMessage());
80
        }
81
    }
82
}
83