1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Serialization; |
4
|
|
|
|
5
|
|
|
use Emarref\Jwt\Claim; |
6
|
|
|
use Emarref\Jwt\Encoding; |
7
|
|
|
use Emarref\Jwt\HeaderParameter; |
8
|
|
|
use Emarref\Jwt\Token; |
9
|
|
|
|
10
|
|
|
class Compact implements SerializerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Encoding\EncoderInterface |
14
|
|
|
*/ |
15
|
|
|
private $encoding; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var HeaderParameter\Factory |
19
|
|
|
*/ |
20
|
|
|
private $headerParameterFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Claim\Factory |
24
|
|
|
*/ |
25
|
|
|
private $claimFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Encoding\EncoderInterface $encoding |
29
|
|
|
* @param HeaderParameter\Factory $headerParameterFactory |
30
|
|
|
* @param Claim\Factory $claimFactory |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
Encoding\EncoderInterface $encoding, |
34
|
|
|
HeaderParameter\Factory $headerParameterFactory, |
35
|
|
|
Claim\Factory $claimFactory |
36
|
|
|
) { |
37
|
|
|
$this->encoding = $encoding; |
38
|
|
|
$this->headerParameterFactory = $headerParameterFactory; |
39
|
|
|
$this->claimFactory = $claimFactory; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $headersJson |
44
|
|
|
* |
45
|
|
|
* @return HeaderParameter\ParameterInterface[] |
46
|
|
|
* @throws \InvalidArgumentException |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
protected function parseHeaders($headersJson) |
49
|
|
|
{ |
50
|
|
|
$parameters = []; |
51
|
|
|
$headers = json_decode($headersJson, true); |
52
|
|
|
|
53
|
|
|
if (!is_array($headers) || empty($headers)) { |
54
|
|
|
throw new \InvalidArgumentException('Not a valid header of JWT string passed for deserialization'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($headers as $name => $value) { |
58
|
|
|
$parameter = $this->headerParameterFactory->get($name); |
59
|
|
|
$parameter->setValue($value); |
60
|
|
|
$parameters[] = $parameter; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $parameters; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $payloadJson |
68
|
|
|
* |
69
|
|
|
* @return Claim\ClaimInterface[] |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
protected function parsePayload($payloadJson) |
73
|
|
|
{ |
74
|
|
|
$claims = []; |
75
|
|
|
$payload = json_decode($payloadJson, true); |
76
|
|
|
|
77
|
|
|
if (!is_array($payload)) { |
78
|
|
|
throw new \InvalidArgumentException('Not a valid payload of JWT string passed for deserialization'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($payload as $name => $value) { |
82
|
|
|
$claim = $this->claimFactory->get($name); |
83
|
|
|
$claim->setValue($value); |
84
|
|
|
$claims[] = $claim; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $claims; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $jwt |
92
|
|
|
* |
93
|
|
|
* @return Token |
94
|
|
|
* @throws \InvalidArgumentException |
95
|
|
|
*/ |
96
|
|
|
public function deserialize($jwt) |
97
|
|
|
{ |
98
|
|
|
$token = new Token(); |
99
|
|
|
|
100
|
|
|
if (empty($jwt)) { |
101
|
|
|
throw new \InvalidArgumentException('Not a valid JWT string passed for deserialization'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
list($encodedHeader, $encodedPayload, $encodedSignature) = array_pad(explode('.', $jwt, 3), 3, null); |
105
|
|
|
|
106
|
|
|
$decodedHeader = $this->encoding->decode($encodedHeader); |
107
|
|
|
$decodedPayload = $this->encoding->decode($encodedPayload); |
108
|
|
|
$decodedSignature = $this->encoding->decode($encodedSignature); |
109
|
|
|
|
110
|
|
|
foreach ($this->parseHeaders($decodedHeader) as $header) { |
111
|
|
|
$token->addHeader($header); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($this->parsePayload($decodedPayload) as $claim) { |
115
|
|
|
$token->addClaim($claim); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$token->setSignature($decodedSignature); |
119
|
|
|
|
120
|
|
|
return $token; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Token $token |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function serialize(Token $token) |
128
|
|
|
{ |
129
|
|
|
$serializedHeader = $token->getHeader()->getParameters()->jsonSerialize(); |
130
|
|
|
$serializedPayload = $token->getPayload()->getClaims()->jsonSerialize(); |
131
|
|
|
$signature = $token->getSignature(); |
132
|
|
|
|
133
|
|
|
return sprintf('%s.%s.%s', |
134
|
|
|
$this->encoding->encode($serializedHeader), |
135
|
|
|
$this->encoding->encode($serializedPayload), |
136
|
|
|
$this->encoding->encode($signature) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|