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.

Parser   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 19 4
A getSignature() 0 12 3
A getJoseHeader() 0 10 2
A getPayload() 0 10 2
A checkToken() 0 16 3
1
<?php
2
3
namespace Gandung\JWT\Parser;
4
5
use Gandung\JWT\Accessor\JoseHeaderAccessor;
6
use Gandung\JWT\Accessor\JoseHeaderAccessorInterface;
7
use Gandung\JWT\Accessor\PayloadAccessor;
8
use Gandung\JWT\Accessor\PayloadAccessorInterface;
9
10
/**
11
 * @author Paulus Gandung Prakosa <[email protected]>
12
 */
13
class Parser implements ParserInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    private $portion;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function parse($token)
24
    {
25
        $this->checkToken($token);
26
27
        $key = ['jose', 'payload', 'signature'];
28
29
        foreach ($this->portion as $k => $v) {
30
            if ($k === 0 || $k === 1) {
31
                $this->portion[$k] = json_decode(
32
                    \Gandung\JWT\__jwt_base64UrlDecode($v),
33
                    JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
34
                );
35
            } else {
36
                $this->portion[$k] = $v;
37
            }
38
        }
39
40
        $this->portion = array_combine($key, $this->portion);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getSignature($raw = true)
47
    {
48
        if (!isset($this->portion['signature'])) {
49
            throw new \RuntimeException(
50
                "JWT portion doesn't have 'signature' index."
51
            );
52
        }
53
54
        return $raw === true
55
            ? \Gandung\JWT\__jwt_base64UrlDecode($this->portion['signature'])
56
            : $this->portion['signature'];
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getJoseHeader(): JoseHeaderAccessorInterface
63
    {
64
        if (!isset($this->portion['jose'])) {
65
            throw new \RuntimeException(
66
                "JWT portion doesn't have 'jose' index."
67
            );
68
        }
69
70
        return new JoseHeaderAccessor($this->portion['jose']);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getPayload(): PayloadAccessorInterface
77
    {
78
        if (!isset($this->portion['payload'])) {
79
            throw new \RuntimeException(
80
                "JWT portion doesn't have 'payload' index."
81
            );
82
        }
83
84
        return new PayloadAccessor($this->portion['payload']);
85
    }
86
87
    /**
88
     * Check if given JWT token is valid.
89
     *
90
     * @param string $token
91
     */
92
    private function checkToken($token)
93
    {
94
        if (empty($token)) {
95
            throw new \InvalidArgumentException(
96
                "JWT token must not be empty."
97
            );
98
        }
99
100
        $this->portion = explode('.', $token);
101
102
        if (sizeof($this->portion) !== 3) {
103
            throw new \RuntimeException(
104
                "Invalid JWT token."
105
            );
106
        }
107
    }
108
}
109