|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Guarded Authentication package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Jafar Jabr <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner; |
|
11
|
|
|
|
|
12
|
|
|
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Base64\Base64UrlSafeEncoder; |
|
13
|
|
|
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Base64\EncoderInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class JWT. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Jafar Jabr <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class JWT |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $payload; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $header; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var EncoderInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $encoder; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $payload |
|
41
|
|
|
* @param array $header |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(array $payload, array $header) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->setPayload($payload); |
|
46
|
|
|
$this->setHeader($header); |
|
47
|
|
|
$this->setEncoder(new Base64UrlSafeEncoder()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param EncoderInterface $encoder |
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setEncoder(EncoderInterface $encoder) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->encoder = $encoder; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Generates the signininput for the current JWT. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function generateSigninInput() |
|
68
|
|
|
{ |
|
69
|
|
|
$base64payload = $this->encoder->encode(json_encode($this->getPayload(), JSON_UNESCAPED_SLASHES)); |
|
70
|
|
|
$base64header = $this->encoder->encode(json_encode($this->getHeader(), JSON_UNESCAPED_SLASHES)); |
|
71
|
|
|
|
|
72
|
|
|
return sprintf('%s.%s', $base64header, $base64payload); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the payload of the JWT. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getPayload() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->payload; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Sets the payload of the current JWT. |
|
87
|
|
|
* @param array $payload |
|
88
|
|
|
* |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setPayload(array $payload) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->payload = $payload; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the header of the JWT. |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getHeader() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->header; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Sets the header of this JWT. |
|
110
|
|
|
* @param array $header |
|
111
|
|
|
* |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setHeader(array $header) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->header = $header; |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|