GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( eac42d...55bb68 )
by François
02:33
created

BearerAuthentication   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 79
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A optionalAuth() 0 11 2
B requireAuth() 0 24 4
A getBearerToken() 0 13 3
A invalidTokenException() 0 13 1
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace fkooman\RemoteStorage\OAuth;
20
21
use fkooman\RemoteStorage\Http\Exception\HttpException;
22
use fkooman\RemoteStorage\Http\Request;
23
24
class BearerAuthentication
25
{
26
    /** @var TokenStorage */
27
    private $tokenStorage;
28
29
    /** @var string */
30
    private $realm;
31
32
    public function __construct(TokenStorage $tokenStorage, $realm = 'Protected Area')
33
    {
34
        $this->tokenStorage = $tokenStorage;
35
        $this->realm = $realm;
36
    }
37
38
    public function optionalAuth(Request $request)
39
    {
40
        $authorizationHeader = $request->getHeader('HTTP_AUTHORIZATION', false, null);
41
42
        // is authorization header there?
43
        if (is_null($authorizationHeader)) {
44
            return false;
45
        }
46
47
        return $this->requireAuth($request);
48
    }
49
50
    public function requireAuth(Request $request)
51
    {
52
        $authorizationHeader = $request->getHeader('HTTP_AUTHORIZATION');
53
54
        // validate the HTTP_AUTHORIZATION header
55
        if (false === $bearerToken = self::getBearerToken($authorizationHeader)) {
56
            throw $this->invalidTokenException();
57
        }
58
59
        $accessTokenKey = $bearerToken[0];
60
        $accessToken = $bearerToken[1];
61
62
        $tokenInfo = $this->tokenStorage->get($accessTokenKey);
63
        if (false === $tokenInfo) {
64
            throw $this->invalidTokenException();
65
        }
66
67
        // time safe string compare, using polyfill on PHP < 5.6
68
        if (hash_equals($tokenInfo['access_token'], $accessToken)) {
69
            return new TokenInfo($tokenInfo);
70
        }
71
72
        throw $this->invalidTokenException();
73
    }
74
75
    private static function getBearerToken($authorizationHeader)
76
    {
77
        if (1 !== preg_match('|^Bearer ([[:alpha:][:digit:]-._~+/]+=*)$|', $authorizationHeader, $m)) {
78
            return false;
79
        }
80
81
        $bearerToken = $m[1];
82
        if (false === strpos($bearerToken, '.')) {
83
            return false;
84
        }
85
86
        return explode('.', $bearerToken);
87
    }
88
89
    private function invalidTokenException()
90
    {
91
        return new HttpException(
92
            'invalid_token',
93
            401,
94
            [
95
                'WWW-Authenticate' => sprintf(
96
                    'Bearer realm="%s",error="invalid_token"',
97
                    $this->realm
98
                ),
99
            ]
100
        );
101
    }
102
}
103