|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Emarref\Jwt; |
|
4
|
|
|
|
|
5
|
|
|
use Emarref\Jwt\Algorithm; |
|
6
|
|
|
use Emarref\Jwt\Claim; |
|
7
|
|
|
use Emarref\Jwt\Encoding; |
|
8
|
|
|
use Emarref\Jwt\Encryption; |
|
9
|
|
|
use Emarref\Jwt\Exception; |
|
10
|
|
|
use Emarref\Jwt\HeaderParameter; |
|
11
|
|
|
use Emarref\Jwt\Serialization; |
|
12
|
|
|
use Emarref\Jwt\Signature; |
|
13
|
|
|
use Emarref\Jwt\Verification; |
|
14
|
|
|
|
|
15
|
|
|
class Jwt |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Encoding\EncoderInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $encoder; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->encoder = new Encoding\Base64(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $jwt |
|
29
|
|
|
* @return Token |
|
30
|
|
|
*/ |
|
31
|
|
|
public function deserialize($jwt) |
|
32
|
|
|
{ |
|
33
|
|
|
$serialization = new Serialization\Compact( |
|
34
|
|
|
$this->encoder, |
|
35
|
|
|
new HeaderParameter\Factory(), |
|
36
|
|
|
new Claim\Factory() |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
return $serialization->deserialize($jwt); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Token $token |
|
44
|
|
|
* @param Encryption\EncryptionInterface $encryption |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function serialize(Token $token, Encryption\EncryptionInterface $encryption) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->sign($token, $encryption); |
|
50
|
|
|
|
|
51
|
|
|
$serialization = new Serialization\Compact( |
|
52
|
|
|
$this->encoder, |
|
53
|
|
|
new HeaderParameter\Factory(), |
|
54
|
|
|
new Claim\Factory() |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return $serialization->serialize($token); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Token $token |
|
62
|
|
|
* @param Encryption\EncryptionInterface $encryption |
|
63
|
|
|
*/ |
|
64
|
|
|
public function sign(Token $token, Encryption\EncryptionInterface $encryption) |
|
65
|
|
|
{ |
|
66
|
|
|
$signer = new Signature\Jws($encryption, $this->encoder); |
|
67
|
|
|
|
|
68
|
|
|
$signer->sign($token); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Verification\Context $context |
|
73
|
|
|
* @return Verification\VerifierInterface[] |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getVerifiers(Verification\Context $context) |
|
76
|
|
|
{ |
|
77
|
|
|
return [ |
|
78
|
|
|
new Verification\EncryptionVerifier($context->getEncryption(), $this->encoder), |
|
79
|
|
|
new Verification\AudienceVerifier($context->getAudience()), |
|
80
|
|
|
new Verification\ExpirationVerifier(), |
|
81
|
|
|
new Verification\IssuerVerifier($context->getIssuer()), |
|
82
|
|
|
new Verification\SubjectVerifier($context->getSubject()), |
|
83
|
|
|
new Verification\NotBeforeVerifier(), |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param Token $token |
|
89
|
|
|
* @param Verification\Context $context |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function verify(Token $token, Verification\Context $context) |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this->getVerifiers($context) as $verifier) { |
|
95
|
|
|
$verifier->verify($token); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|