Completed
Pull Request — master (#7)
by John
07:24
created

JwtToken::getPrn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\JwtBundle 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
9
namespace KleijnWeb\JwtBundle\Authenticator;
10
11
use KleijnWeb\JwtBundle\Authenticator\SignatureValidator\SignatureValidator;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
class JwtToken
17
{
18
    /**
19
     * @var array
20
     */
21
    private $claims = [];
22
23
    /**
24
     * @var array
25
     */
26
    private $header = [];
27
28
    /**
29
     * @var int
30
     */
31
    private $payload;
32
33
    /**
34
     * @var string
35
     */
36
    private $signature;
37
38
    /**
39
     * @param string $tokenString
40
     */
41
    public function __construct($tokenString)
42
    {
43
        $segments = explode('.', $tokenString);
44
45
        if (count($segments) !== 3) {
46
            throw new \InvalidArgumentException("Not a JWT token string");
47
        }
48
49
        list($headerBase64, $claimsBase64, $signatureBase64) = $segments;
50
51
        $this->payload = "{$headerBase64}.{$claimsBase64}";
52
53
        $decoder = new Decoder();
54
        $this->header = $decoder->decode($headerBase64);
55
        $this->claims = $decoder->decode($claimsBase64);
56
        $this->signature = $decoder->base64Decode($signatureBase64);
57
    }
58
59
    /**
60
     * @return string|null
61
     */
62
    public function getKeyId()
63
    {
64
        return isset($this->header['kid']) ? $this->header['kid'] : null;
65
    }
66
67
    /**
68
     * @return string|null
69
     */
70
    public function getPrn()
71
    {
72
        return isset($this->header['prn']) ? $this->header['prn'] : null;
73
    }
74
75
    /**
76
     * @param string             $secret
77
     * @param SignatureValidator $validator
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81
    public function validateSignature($secret, SignatureValidator $validator)
82
    {
83
        if (!$validator->isValid($this->payload, $secret, $this->signature)) {
84
            throw new \InvalidArgumentException("Invalid signature");
85
        }
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getClaims()
92
    {
93
        return $this->claims;
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function getHeader()
100
    {
101
        return $this->header;
102
    }
103
}
104