Failed Conditions
Push — master ( 349866...67c1d1 )
by Florent
10:56 queued 06:08
created

AccessTokenMiddleware::process()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 3
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\Core\Middleware;
15
16
use Psr\Http\Server\RequestHandlerInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId;
19
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRepository;
20
use OAuth2Framework\Component\Core\Message\OAuth2Message;
21
use OAuth2Framework\Component\Core\TokenType\TokenTypeManager;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
25
final class AccessTokenMiddleware implements MiddlewareInterface
26
{
27
    /**
28
     * @var TokenTypeManager
29
     */
30
    private $tokenTypeManager;
31
32
    /**
33
     * @var AccessTokenRepository
34
     */
35
    private $accessTokenRepository;
36
37
    /**
38
     * AccessTokenMiddleware constructor.
39
     *
40
     * @param TokenTypeManager      $tokenTypeManager
41
     * @param AccessTokenRepository $accessTokenRepository
42
     */
43
    public function __construct(TokenTypeManager $tokenTypeManager, AccessTokenRepository $accessTokenRepository)
44
    {
45
        $this->tokenTypeManager = $tokenTypeManager;
46
        $this->accessTokenRepository = $accessTokenRepository;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
53
    {
54
        $additional_credential_values = [];
55
        $token = $this->tokenTypeManager->findToken($request, $additional_credential_values, $type);
56
        if (null !== $token) {
57
            $tokenId = AccessTokenId::create($token);
58
            $accessToken = $this->accessTokenRepository->find($tokenId);
59
            if (null === $accessToken || false === $type->isTokenRequestValid($accessToken, $request, $additional_credential_values)) {
0 ignored issues
show
Bug introduced by
The method isTokenRequestValid() does not seem to exist on object<OAuth2Framework\C...re\TokenType\TokenType>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
                throw new OAuth2Message(400, OAuth2Message::ERROR_INVALID_TOKEN, 'Invalid access token.');
61
            }
62
            $request = $request->withAttribute('access_token', $accessToken);
63
        }
64
65
        return $handler->handle($request);
66
    }
67
}
68