Test Failed
Push — master ( b8ab64...956082 )
by Gabriel
02:51 queued 33s
created

Bearer::authenticate()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 2
nop 1
1
<?php
2
/*
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Gabriel Somoza
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace Somoza\Psr7\OAuth2Middleware;
27
28
use League\OAuth2\Client\Provider\AbstractProvider;
29
use League\OAuth2\Client\Token\AccessToken;
30
use Psr\Http\Message\RequestInterface;
31
32
/**
33
 * Bearer PSR7 Middleware
34
 *
35
 * @author Gabriel Somoza <[email protected]>
36
 */
37
final class Bearer
38
{
39
    const HEADER_AUTHENTICATION = 'Authentication';
40
41
    const AUTHENTICATION_SCHEMA = 'Bearer';
42
43
    /** @var AbstractProvider */
44
    private $provider;
45
46
    /** @var AccessToken */
47
    private $accessToken;
48
49
    /**
50
     * OAuth2Middleware constructor.
51
     * @param AccessToken $accessToken
0 ignored issues
show
Documentation introduced by
Should the type for parameter $accessToken not be null|AccessToken?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
52
     * @param AbstractProvider $provider
53
     */
54
    public function __construct(
55
        AbstractProvider $provider,
56
        AccessToken $accessToken = null
57
    ) {
58
        $this->provider = $provider;
59
        $this->accessToken = $accessToken;
60
    }
61
62
    /**
63
     * __invoke
64
     * @param callable $handler
65
     * @return \Closure
66
     */
67
    public function __invoke(callable $handler)
68
    {
69
        return function(RequestInterface $request, array $options) use ($handler) {
70
            $request = $this->authenticate($request);
71
            return $handler($request, $options);
72
        };
73
    }
74
75
    /**
76
     * Authenticate
77
     * @param RequestInterface $request
78
     * @return RequestInterface
79
     */
80
    protected function authenticate(RequestInterface $request)
81
    {
82
        if ($request->getMethod() !== 'GET'
83
            || $request->hasHeader('Authentication')
84
            || $request->getUri() == $this->provider->getBaseAuthorizationUrl()
85
        ) {
86
            return $request;
87
        }
88
89
        $this->checkAccessToken();
90
91
        return $request->withHeader(
92
            self::HEADER_AUTHENTICATION,
93
            self::AUTHENTICATION_SCHEMA . ' ' . $this->accessToken->getToken()
94
        );
95
    }
96
97
    /**
98
     * checkAccessToken
99
     * @return AccessToken
0 ignored issues
show
Documentation introduced by
Should the return type not be AccessToken|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
100
     */
101
    private function checkAccessToken()
102
    {
103
        $now = time();
104
        if (!$this->accessToken
105
            || ($this->accessToken->getExpires() !== null
106
                && $this->accessToken->getExpires() - $now <= 0)
107
        ) {
108
            $this->accessToken = $this->provider->getAccessToken('client_credentials');
109
        }
110
    }
111
}
112