|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MadWizard\WebAuthn\Pki\Jwt; |
|
4
|
|
|
|
|
5
|
|
|
use MadWizard\WebAuthn\Exception\ParseException; |
|
6
|
|
|
use MadWizard\WebAuthn\Format\Base64UrlEncoding; |
|
7
|
|
|
use MadWizard\WebAuthn\Format\ByteBuffer; |
|
8
|
|
|
|
|
9
|
|
|
class Jwt implements JwtInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $header; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $body; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ByteBuffer |
|
23
|
|
|
*/ |
|
24
|
|
|
private $signedData; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ByteBuffer |
|
28
|
|
|
*/ |
|
29
|
|
|
private $signature; |
|
30
|
|
|
|
|
31
|
11 |
|
public function __construct(string $token) |
|
32
|
|
|
{ |
|
33
|
11 |
|
$parts = explode('.', $token); |
|
34
|
11 |
|
[$headerJson, $bodyJson, $signature] = self::decodeToken($parts); |
|
35
|
|
|
|
|
36
|
10 |
|
$header = self::jsonDecode($headerJson); |
|
37
|
9 |
|
$body = self::jsonDecode($bodyJson); |
|
38
|
|
|
|
|
39
|
9 |
|
if (!is_array($header)) { |
|
|
|
|
|
|
40
|
|
|
throw new ParseException('Expecting header to be a json object.'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
9 |
|
if (!is_array($body)) { |
|
|
|
|
|
|
44
|
|
|
throw new ParseException('Expecting body to be a json object.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
9 |
|
$this->header = $header; |
|
48
|
9 |
|
$this->body = $body; |
|
49
|
9 |
|
$this->signedData = new ByteBuffer($parts[0] . '.' . $parts[1]); |
|
50
|
9 |
|
$this->signature = new ByteBuffer($signature); |
|
51
|
9 |
|
} |
|
52
|
|
|
|
|
53
|
11 |
|
private static function decodeToken(array $parts): array |
|
54
|
|
|
{ |
|
55
|
11 |
|
if (count($parts) !== 3) { |
|
56
|
1 |
|
throw new ParseException('Invalid JWT'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
10 |
|
Base64UrlEncoding::decode($parts[0]), |
|
61
|
10 |
|
Base64UrlEncoding::decode($parts[1]), |
|
62
|
10 |
|
Base64UrlEncoding::decode($parts[2]), |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
10 |
|
private static function jsonDecode(string $json): array |
|
67
|
|
|
{ |
|
68
|
10 |
|
$decoded = json_decode($json, true); |
|
69
|
10 |
|
if ($decoded === null && json_last_error() !== JSON_ERROR_NONE) { |
|
70
|
1 |
|
throw new ParseException(sprintf('JSON parse error in JWT: %s.', json_last_error_msg())); |
|
71
|
|
|
} |
|
72
|
9 |
|
return $decoded; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
9 |
|
public function getHeader(): array |
|
76
|
|
|
{ |
|
77
|
9 |
|
return $this->header; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
public function getBody(): array |
|
81
|
|
|
{ |
|
82
|
2 |
|
return $this->body; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
public function getSignedData(): ByteBuffer |
|
86
|
|
|
{ |
|
87
|
4 |
|
return $this->signedData; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
public function getSignature(): ByteBuffer |
|
91
|
|
|
{ |
|
92
|
4 |
|
return $this->signature; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|