1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ================================== |
4
|
|
|
* Responsible PHP API |
5
|
|
|
* ================================== |
6
|
|
|
* |
7
|
|
|
* @link Git https://github.com/vince-scarpa/responsibleAPI.git |
8
|
|
|
* |
9
|
|
|
* @api Responible API |
10
|
|
|
* @package responsible\core\oauth |
11
|
|
|
* |
12
|
|
|
* @author Vince scarpa <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
namespace responsible\core\auth; |
16
|
|
|
|
17
|
|
|
use responsible\core\auth; |
18
|
|
|
use responsible\core\encoder; |
19
|
|
|
|
20
|
|
|
class jwtEncoder extends jwt |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* [$cipher Cipher class] |
24
|
|
|
* @var object |
25
|
|
|
*/ |
26
|
|
|
private $cipher; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* [$claims Claims class] |
30
|
|
|
* @var object |
31
|
|
|
*/ |
32
|
|
|
private $claims; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* [$stringToSign Set the string to sign] |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $stringToSign = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* [encode] |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function encode() |
45
|
|
|
{ |
46
|
|
|
$this->cipher = new encoder\cipher; |
47
|
|
|
$this->claims = new auth\jwtClaims; |
48
|
|
|
|
49
|
|
|
$algo = parent::getAlgorithm(); |
50
|
|
|
|
51
|
|
|
$header = [ |
52
|
|
|
'typ' => self::CYT, |
53
|
|
|
'alg' => $algo['header'], |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$payload = $this->payload; |
57
|
|
|
|
58
|
|
|
$this->claims->setSegment( |
59
|
|
|
'header', |
60
|
|
|
$this->cipher->encode($this->cipher->jsonEncode($header)) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->claims->setSegment( |
64
|
|
|
'payload', |
65
|
|
|
$this->cipher->encode($this->cipher->jsonEncode($payload)) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->stringToSign = $this->claims->getSegment('header') . '.' . $this->claims->getSegment('payload'); |
69
|
|
|
|
70
|
|
|
$signature = $this->sign(); |
71
|
|
|
|
72
|
|
|
$this->claims->setSegment( |
73
|
|
|
'signature', |
74
|
|
|
$this->cipher->encode($signature) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$signed = $this->claims->getSegment('header') . '.' . $this->claims->getSegment('payload') . '.' . $this->claims->getSegment('signature'); |
78
|
|
|
|
79
|
|
|
return $signed; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* [sign] Sign the segments |
84
|
|
|
* @return string the JWT |
85
|
|
|
*/ |
86
|
|
|
private function sign() |
87
|
|
|
{ |
88
|
|
|
$algo = parent::getAlgorithm(); |
89
|
|
|
|
90
|
|
|
return $this->cipher->encode( |
91
|
|
|
$this->cipher->hash( |
92
|
|
|
$algo['hash'], |
93
|
|
|
$this->stringToSign, |
94
|
|
|
$this->key |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* [key Set the clients supplied secret key] |
101
|
|
|
* @param string $key |
102
|
|
|
* Hashed string |
103
|
|
|
* @return self |
104
|
|
|
*/ |
105
|
|
|
public function key($key = null) |
106
|
|
|
{ |
107
|
|
|
$this->key = $key; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* [payload Set the clients payload] |
114
|
|
|
* @param array $payload |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
|
|
public function payload($payload) |
118
|
|
|
{ |
119
|
|
|
$this->payload = $payload; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|