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