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.

Token::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Oauth;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Psr\Http\Server\MethodInterface;
19
use O2System\Security\Encoders\Base64;
20
use O2System\Security\Encoders\Json;
21
use O2System\Security\Generators\Signature;
22
use O2System\Spl\Traits\Collectors\ErrorCollectorTrait;
23
24
/**
25
 * Class Token
26
 * @package O2System\Security\Authentication\Oauth
27
 */
28
class Token implements MethodInterface
29
{
30
    use ErrorCollectorTrait;
31
32
    protected $consumer;
33
34
    public function __construct(Consumer $consumer)
35
    {
36
        $this->consumer = $consumer;
37
    }
38
39
    // ------------------------------------------------------------------------
40
41
    /**
42
     * Token::getVerifier
43
     *
44
     * Gets Token oauth_verifier code.
45
     *
46
     * @return bool|string
47
     */
48
    public function getVerifier()
49
    {
50
        if ( ! empty($this->key) && ! empty($this->secret)) {
51
            $key = rawurlencode($this->key);
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist on O2System\Security\Authentication\Oauth\Token. Did you maybe forget to declare it?
Loading history...
52
            $secret = rawurlencode($this->secret);
0 ignored issues
show
Bug Best Practice introduced by
The property secret does not exist on O2System\Security\Authentication\Oauth\Token. Did you maybe forget to declare it?
Loading history...
53
54
            return base64_encode($key . ':' . $secret);
55
        }
56
57
        return false;
58
    }
59
60
    // ------------------------------------------------------------------------
61
62
    /**
63
     * Token::getRequest
64
     *
65
     * Gets OAuth Request Token.
66
     *
67
     * @return array|bool Returns FALSE if failed.
68
     */
69
    public function getRequest($callbackUrl, $httpMethod = self::HTTP_POST)
70
    {
71
        $algorithm = 'HMAC-SHA1';
0 ignored issues
show
Unused Code introduced by
The assignment to $algorithm is dead and can be removed.
Loading history...
72
        if (false === ($signature = Base64::decode($this->consumer->secret))) {
73
            $this->addError(400, 'Invalid Consumer Secret');
74
75
            return false;
76
        }
77
78
        if (false === ($signature = Json::decode($signature))) {
0 ignored issues
show
introduced by
The condition false === $signature = O...son::decode($signature) is always false.
Loading history...
79
            $this->addError(400, 'Invalid Consumer Secret');
80
81
            return false;
82
        }
83
84
        $signature->callbackUrl = $callbackUrl;
0 ignored issues
show
Bug introduced by
The property callbackUrl does not seem to exist on O2System\Spl\DataStructures\SplArrayObject.
Loading history...
85
        $signature->httpMethod = $httpMethod;
0 ignored issues
show
Bug introduced by
The property httpMethod does not seem to exist on O2System\Spl\DataStructures\SplArrayObject.
Loading history...
86
        $algorithm = $signature->algorithm;
87
88
        if (false !== ($payload = Base64::decode($this->consumer->key))) {
89
            $payload = Json::decode($payload)->getArrayCopy();
90
        }
91
92
        if ($payload) {
93
            $payload[ 'timestamp' ] = time();
94
95
            $segments[] = Base64::encode(Json::encode($signature));
0 ignored issues
show
Comprehensibility Best Practice introduced by
$segments was never initialized. Although not strictly required by PHP, it is generally a good practice to add $segments = array(); before regardless.
Loading history...
96
            $segments[] = $token = Base64::encode(Signature::generate([
97
                'payload' => Base64::encode(Json::encode($payload)),
98
                'token'   => \OAuthProvider::generateToken(strlen($this->consumer->secret), true),
99
            ], $this->consumer->key, $algorithm));
100
101
            $secret = Base64::encode(Signature::generate($segments, $this->consumer->key, $algorithm));
102
103
            return [
104
                'oauth_token'        => $token,
105
                'oauth_token_secret' => $secret,
106
            ];
107
        }
108
109
        return false;
110
    }
111
112
    // ------------------------------------------------------------------------
113
}