1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UAPAY; |
4
|
|
|
|
5
|
|
|
use UAPAY\Log as Log; |
6
|
|
|
use UAPAY\Exception; |
7
|
|
|
use Firebase\JWT\JWT; |
8
|
|
|
|
9
|
|
|
abstract class Response |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $json; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $jwt=array( |
20
|
|
|
'using' => false, |
21
|
|
|
'UAPAY_pubkey' => '', |
22
|
|
|
'our_privkey' => '', |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $status; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param array $json_string JSON string |
34
|
|
|
* @param array $jwt_options array of options |
35
|
|
|
*/ |
36
|
|
|
public function __construct($json_string, $jwt_options=null) |
37
|
|
|
{ |
38
|
|
|
if (isset($jwt_options) && is_array($jwt_options)) |
39
|
|
|
{ |
40
|
|
|
$this->jwt = array_merge($this->jwt, $jwt_options); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->json_decode($json_string); |
|
|
|
|
44
|
|
|
$this->json_handle(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Decode JSON |
49
|
|
|
* |
50
|
|
|
* @param string $json_string |
51
|
|
|
* @throws Exception\Runtime |
52
|
|
|
*/ |
53
|
|
|
protected function json_decode($json_string) |
54
|
|
|
{ |
55
|
|
|
$this->json = json_decode($json_string, true); |
56
|
|
|
|
57
|
|
|
if (json_last_error() != 0) |
58
|
|
|
{ |
59
|
|
|
throw new Exception\Runtime('decoding error of the json response!'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Handle decoded JSON |
65
|
|
|
* |
66
|
|
|
* @throws Exception\JSON |
67
|
|
|
*/ |
68
|
|
|
protected function json_handle() |
69
|
|
|
{ |
70
|
|
|
if (isset($this->json['error'])) |
71
|
|
|
{ |
72
|
|
|
throw new Exception\JSON('json response contain an error message!'); |
73
|
|
|
} |
74
|
|
|
if ( ! isset($this->json['status'])) |
75
|
|
|
{ |
76
|
|
|
throw new Exception\JSON('invalid json response!'); |
77
|
|
|
} |
78
|
|
|
if ($this->json['status'] == 0) |
79
|
|
|
{ |
80
|
|
|
throw new Exception\JSON('json response contain an error status!'); |
81
|
|
|
} |
82
|
|
|
$this->status = $this->json['status']; |
83
|
|
|
|
84
|
|
|
if ( ! isset($this->json['data']) || !is_array($this->json['data'])) |
85
|
|
|
{ |
86
|
|
|
throw new Exception\JSON('json does not contain the data field!'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->jwt['using'] === true) |
90
|
|
|
{ |
91
|
|
|
if ( ! isset($this->json['data']['token'])) |
92
|
|
|
{ |
93
|
|
|
throw new Exception\JSON('data does not contain the token field!'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->json['data'] = $this->token_decode($this->json['data']['token']); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get UAPAY public key |
102
|
|
|
* |
103
|
|
|
* @throws Exception\Runtime |
104
|
|
|
* @return string Public key |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
protected function uapay_public_key() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
// check public key file |
109
|
|
|
if ( ! file_exists($this->jwt['UAPAY_pubkey'])) |
110
|
|
|
{ |
111
|
|
|
throw new Exception\Runtime('The file with the public key was not find!'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// load public key file |
115
|
|
|
$fpkey = fopen($this->jwt['UAPAY_pubkey'], "rb"); |
116
|
|
|
if ($fpkey === FALSE) |
117
|
|
|
{ |
118
|
|
|
throw new Exception\Runtime('The file with the public key was not open!'); |
119
|
|
|
} |
120
|
|
|
$public_key = fread($fpkey, 8192); |
121
|
|
|
if ($public_key === FALSE) |
122
|
|
|
{ |
123
|
|
|
throw new Exception\Runtime('The file with the public key was not read!'); |
124
|
|
|
} |
125
|
|
|
fclose($fpkey); |
126
|
|
|
|
127
|
|
|
return $public_key; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Decode token |
132
|
|
|
* |
133
|
|
|
* @param string $token |
134
|
|
|
* @throws Exception\Runtime |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
protected function token_decode($token) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
try |
140
|
|
|
{ |
141
|
|
|
$decoded = (array) JWT::decode($token, $this->uapay_public_key(), array('RS512')); |
142
|
|
|
} |
143
|
|
|
catch (\Exception $e) |
144
|
|
|
{ |
145
|
|
|
Log::instance()->error($e->getMessage().PHP_EOL.$e->getTraceAsString()); |
|
|
|
|
146
|
|
|
throw new Exception\JSON('unable to decode JWT token', $e); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
Log::instance()->debug('decoded payload:'); |
|
|
|
|
150
|
|
|
Log::instance()->debug(print_r($decoded, true)); |
151
|
|
|
|
152
|
|
|
return $decoded; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get status code |
157
|
|
|
* |
158
|
|
|
* @return int |
159
|
|
|
*/ |
160
|
|
|
public function status() |
161
|
|
|
{ |
162
|
|
|
return $this->status; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|