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.

Validator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addConstraint() 0 4 1
B validate() 0 27 4
B validateFromArray() 0 22 5
A unserializePortion() 0 7 1
A mergeUnserializedPortion() 0 10 2
A normalizeConstraints() 0 6 1
1
<?php
2
3
namespace Gandung\JWT\Validator;
4
5
use Gandung\JWT\ValidatorInterface;
6
7
/**
8
 * @author Paulus Gandung Prakosa <[email protected]>
9
 */
10
class Validator implements ValidatorInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $constraint = [];
16
17
    public function addConstraint(Constraints\ConstraintInterface $constraint)
18
    {
19
        $this->constraint[] = $constraint;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function validate($value)
26
    {
27
        $q = explode('.', $value);
28
29
        if (sizeof($q) !== 3) {
30
            throw new \InvalidArgumentException(
31
                "Supply valid JWT token."
32
            );
33
        }
34
35
        $m = $this->mergeUnserializedPortion(
36
            $this->unserializePortion($q[0]),
37
            $this->unserializePortion($q[1])
38
        );
39
40
        $this->normalizeConstraints();
41
42
        foreach ($this->constraint as $c) {
43
            if (!array_key_exists($c, $m)) {
44
                throw new \RuntimeException(
45
                    sprintf("Supplied JWT Token doesn't have '%s' constraint.", $c)
46
                );
47
            }
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * Validate constraints from given array.
55
     *
56
     * @param array $array
57
     * @param null|object $key
58
     * @return boolean
59
     * @throws \RuntimeException when supplied array doesn't matched given constraint.
60
     */
61
    public function validateFromArray($array, $key = null)
62
    {
63
        if (is_null($key)) {
64
            $this->normalizeConstraints();
65
66
            foreach ($this->constraint as $c) {
67
                if (!array_key_exists($c, $array)) {
68
                    throw new \RuntimeException(
69
                        sprintf("Supplied data doesn't have '%s' constraint.", $c)
70
                    );
71
                }
72
            }
73
        } else {
74
            if (!array_key_exists($key->getValue(), $array)) {
75
                throw new \RuntimeException(
76
                    sprintf("Supplied data doesn't have '%s' constraint.", $key->getValue())
77
                );
78
            }
79
        }
80
81
        return true;
82
    }
83
84
    private function unserializePortion($portion)
85
    {
86
        return json_decode(
87
            \Gandung\JWT\__jwt_base64UrlDecode($portion),
88
            \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE
89
        );
90
    }
91
92
    private function mergeUnserializedPortion()
93
    {
94
        $res = [];
95
96
        foreach (func_get_args() as $v) {
97
            $res = array_merge_recursive($res, $v);
98
        }
99
100
        return $res;
101
    }
102
103
    private function normalizeConstraints()
104
    {
105
        $this->constraint = array_map(function ($q) {
106
            return $q->getValue();
107
        }, $this->constraint);
108
    }
109
}
110