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