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 ( eb926b...5379ed )
by François
03:54
created

BearerValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
/**
3
 * Copyright 2015 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
namespace fkooman\VPN\Server;
18
19
use fkooman\Rest\Plugin\Authentication\Bearer\ValidatorInterface;
20
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo;
21
22
class BearerValidator implements ValidatorInterface
23
{
24
    /** @var array */
25
    private $bearerTokens;
26
27
    public function __construct(array $bearerTokens)
28
    {
29
        $this->bearerTokens = $bearerTokens;
30
    }
31
32
    /**
33
     * @return TokenInfo
34
     */
35
    public function validate($bearerToken)
36
    {
37
        foreach ($this->bearerTokens as $t) {
38
            if (self::hashEquals($t, $bearerToken)) {
39
                return new TokenInfo(
40
                    ['active' => true]
41
                );
42
            }
43
        }
44
45
        return new TokenInfo(
46
            ['active' => false]
47
        );
48
    }
49
50
    /**
51
     * Wrapper to compare two hashes in a timing safe way.
52
     *
53
     * @param string $safe the string we control
54
     * @param string $user the string the user controls
55
     *
56
     * @return bool whether or not the two strings are identical
57
     */
58
    public static function hashEquals($safe, $user)
59
    {
60
        // PHP >= 5.6.0 has "hash_equals"
61
        if (function_exists('hash_equals')) {
62
            return hash_equals($safe, $user);
63
        }
64
65
        return self::timingSafeEquals($safe, $user);
66
    }
67
    /**
68
     * A timing safe equals comparison.
69
     *
70
     * @param string $safe The internal (safe) value to be checked
71
     * @param string $user The user submitted (unsafe) value
72
     *
73
     * @return bool True if the two strings are identical.
74
     *
75
     * @see http://blog.ircmaxell.com/2014/11/its-all-about-time.html
76
     */
77
    public static function timingSafeEquals($safe, $user)
78
    {
79
        $safeLen = strlen($safe);
80
        $userLen = strlen($user);
81
        if ($userLen != $safeLen) {
82
            return false;
83
        }
84
        $result = 0;
85
        for ($i = 0; $i < $userLen; ++$i) {
86
            $result |= (ord($safe[$i]) ^ ord($user[$i]));
87
        }
88
        // They are only identical strings if $result is exactly 0...
89
        return $result === 0;
90
    }
91
}
92