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.

Signature   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 36
c 1
b 0
f 1
dl 0
loc 81
rs 10
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
B verify() 0 33 10
A generate() 0 20 6
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Security\Authentication\User;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Security\Encryptions\Algorithm;
19
use O2System\Security\Encryptions\Hmac;
20
21
class Signature
22
{
23
    /**
24
     * Signature::generate
25
     *
26
     * @param array  $segments
27
     * @param string $key
28
     * @param string $algorithm
29
     *
30
     * @return bool|string
31
     * @throws \O2System\Spl\Exceptions\Logic\DomainException
32
     */
33
    public static function generate(array $segments, $key, $algorithm = 'HS256')
34
    {
35
        if (count($segments) == 2) {
36
            $data = implode('.', $segments);
37
38
            if (Algorithm::validate($algorithm)) {
39
                list($function, $algorithm) = Algorithm::map($algorithm);
40
41
                switch ($function) {
42
                    case 'HMAC':
43
                        return Hmac::hash($algorithm, $data, $key, true);
44
                    case 'hash_hmac':
45
                        return hash_hmac($algorithm, $data, $key, true);
46
                    case 'openssl':
47
                        return openssl_sign($data, $signature, $key, $algorithm);
48
                }
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    // ------------------------------------------------------------------------
56
57
    /**
58
     * Signature::verify
59
     *
60
     * Verify token with signature.
61
     *
62
     * @param string $token
63
     * @param string $signature
64
     * @param string $key
65
     * @param string $algorithm
66
     *
67
     * @return bool
68
     */
69
    public static function verify($token, $signature, $key, $algorithm = 'HS256')
70
    {
71
        $segments = explode('.', $token);
72
        $segments = array_map('trim', $segments);
73
74
        if (count($segments) == 3) {
75
            array_pop($segments);
76
            $data = implode('.', $segments);
77
78
            if (Algorithm::validate($algorithm)) {
79
                list($function, $algorithm) = Algorithm::map($algorithm);
80
81
                switch ($function) {
82
                    case 'HMAC':
83
                        return Hmac::hash($algorithm, $data, $key, true) === $signature;
84
                    case 'hash_hmac':
85
                        return hash_hmac($algorithm, $data, $key, true) === $signature;
86
                    case 'openssl':
87
                        switch ($algorithm) {
88
                            case 'RS256':
89
                                return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA1);
90
                            case 'RS256':
91
                                return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA256);
92
                            case 'RS384':
93
                                return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA384);
94
                            case 'RS512':
95
                                return (bool)openssl_verify($data, $signature, $key, OPENSSL_ALGO_SHA512);
96
                        }
97
                }
98
            }
99
        }
100
101
        return false;
102
    }
103
}