/
Jwt.php
151 lines (126 loc) · 4.25 KB
/
Jwt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/*
* This file is part of the tmilos/jose-jwt package.
*
* (c) Milos Tomic <tmilos@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Tmilos\JoseJwt;
use Tmilos\JoseJwt\Context\Context;
use Tmilos\JoseJwt\Error\IntegrityException;
use Tmilos\JoseJwt\Error\JoseJwtException;
use Tmilos\JoseJwt\Util\StringUtils;
use Tmilos\JoseJwt\Util\UrlSafeB64Encoder;
class Jwt
{
private function __construct()
{
}
/**
* @param Context $context
* @param array|object $payload
* @param string|resource $key
* @param string $jwsAlgorithm
* @param array $extraHeaders
*
* @return string
*/
public static function encode(Context $context, $payload, $key, $jwsAlgorithm, $extraHeaders = [])
{
$header = array_merge([
'alg' => '',
'typ' => 'JWT',
], $extraHeaders);
$hashAlgorithm = $context->jwsAlgorithms()->get($jwsAlgorithm);
if (null == $hashAlgorithm) {
throw new JoseJwtException(sprintf('Unknown algorithm "%s"', $jwsAlgorithm));
}
$header['alg'] = $jwsAlgorithm;
$payloadString = StringUtils::payload2string($payload, $context->jsonMapper());
$signingInput = implode('.', [
UrlSafeB64Encoder::encode(json_encode($header)),
UrlSafeB64Encoder::encode($payloadString),
]);
$signature = $hashAlgorithm->sign($signingInput, $key);
$signature = UrlSafeB64Encoder::encode($signature);
return $signingInput.'.'.$signature;
}
/**
* @param Context $context
* @param string $token
* @param string|resource $key
*
* @return array
*/
public static function decode(Context $context, $token, $key)
{
if (empty($token) || trim($token) === '') {
throw new JoseJwtException('Incoming token expected to be in compact serialization form, but is empty');
}
$parts = explode('.', $token);
if (count($parts) == 5) {
return Jwe::decode($context, $token, $key);
}
$decodedParts = [];
foreach ($parts as $part) {
$decodedParts[] = UrlSafeB64Encoder::decode($part);
}
$header = json_decode($decodedParts[0], true);
if (null == $header) {
throw new JoseJwtException('Invalid header');
}
// signed or plain JWT
$signedInput = $parts[0].'.'.$parts[1];
$algorithmId = $header['alg'];
$algorithm = $context->jwsAlgorithms()->get($algorithmId);
if (null === $algorithm) {
throw new JoseJwtException(sprintf('Invalid algorithm "%s"', $algorithmId));
}
if (false === $algorithm->verify($decodedParts[2], $signedInput, $key)) {
throw new IntegrityException('Invalid signature');
}
return json_decode($decodedParts[1], true);
}
/**
* @param $token
*
* @return array
*/
public static function header($token)
{
if (null === $token || trim($token) === '') {
throw new JoseJwtException('Incoming token expected to be in compact serialization form, but is empty');
}
$parts = explode('.', $token);
if (count($parts) != 3 && count($parts) != 5) {
throw new JoseJwtException('Invalid JWT');
}
$header = json_decode(UrlSafeB64Encoder::decode($parts[0]), true);
if (null == $header) {
throw new JoseJwtException('Invalid header');
}
return $header;
}
/**
* @param $token
*
* @return array
*/
public static function payload($token)
{
if (null === $token || trim($token) === '') {
throw new JoseJwtException('Incoming token expected to be in compact serialization form, but is empty');
}
$parts = explode('.', $token);
if (count($parts) != 3) {
throw new JoseJwtException('Invalid JWT');
}
$payload = json_decode(UrlSafeB64Encoder::decode($parts[1]), true);
if (null == $payload) {
throw new JoseJwtException('Invalid payload');
}
return $payload;
}
}