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 array |
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 = $this->json_decode($json_string); |
|
|
|
|
44
|
|
|
$this->json_handle(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Decode JSON |
49
|
|
|
* |
50
|
|
|
* @param string $json_string |
51
|
|
|
* @return array |
52
|
|
|
* @throws Exception\Runtime |
53
|
|
|
*/ |
54
|
|
|
protected function json_decode($json_string) |
55
|
|
|
{ |
56
|
|
|
$decoded = json_decode($json_string, true); |
57
|
|
|
|
58
|
|
|
if (json_last_error() != 0) |
59
|
|
|
{ |
60
|
|
|
throw new Exception\Runtime('decoding error of the json response!'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $decoded; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Handle decoded JSON |
68
|
|
|
* |
69
|
|
|
* @throws Exception\JSON |
70
|
|
|
*/ |
71
|
|
|
protected function json_handle() |
72
|
|
|
{ |
73
|
|
|
if (isset($this->json['error'])) |
74
|
|
|
{ |
75
|
|
|
throw new Exception\JSON('json response contain an error message!'); |
76
|
|
|
} |
77
|
|
|
if ( ! isset($this->json['status'])) |
78
|
|
|
{ |
79
|
|
|
throw new Exception\JSON('invalid json response!'); |
80
|
|
|
} |
81
|
|
|
if ($this->json['status'] == 0) |
82
|
|
|
{ |
83
|
|
|
throw new Exception\JSON('json response contain an error status!'); |
84
|
|
|
} |
85
|
|
|
$this->status = $this->json['status']; |
86
|
|
|
|
87
|
|
|
if ( ! isset($this->json['data']) || !is_array($this->json['data'])) |
88
|
|
|
{ |
89
|
|
|
throw new Exception\JSON('json does not contain the data field!'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($this->jwt['using'] === true) |
93
|
|
|
{ |
94
|
|
|
if ( ! isset($this->json['data']['token'])) |
95
|
|
|
{ |
96
|
|
|
throw new Exception\JSON('data does not contain the token field!'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->json['data'] = $this->token_decode($this->json['data']['token']); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get file contents |
105
|
|
|
* |
106
|
|
|
* @param string $fname |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function file_get_contents($fname) |
110
|
|
|
{ |
111
|
|
|
return file_get_contents($fname); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get UAPAY public key |
116
|
|
|
* |
117
|
|
|
* @throws Exception\Runtime |
118
|
|
|
* @return string Public key |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
protected function uapay_public_key() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
// check public key file |
123
|
|
|
if ( ! file_exists($this->jwt['UAPAY_pubkey'])) |
124
|
|
|
{ |
125
|
|
|
throw new Exception\Runtime('The file with the public key was not find!'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// load public key file |
129
|
|
|
$public_key = $this->file_get_contents($this->jwt['UAPAY_pubkey']); |
130
|
|
|
if ($public_key === FALSE) |
131
|
|
|
{ |
132
|
|
|
throw new Exception\Runtime('The file with the public key was not read!'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $public_key; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Decode token |
140
|
|
|
* |
141
|
|
|
* @param string $token |
142
|
|
|
* @throws Exception\Runtime |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
protected function token_decode($token) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
try |
148
|
|
|
{ |
149
|
|
|
$decoded = (array) JWT::decode($token, $this->uapay_public_key(), array('RS512')); |
150
|
|
|
} |
151
|
|
|
catch (\Exception $e) |
152
|
|
|
{ |
153
|
|
|
Log::instance()->error($e->getMessage().PHP_EOL.$e->getTraceAsString()); |
|
|
|
|
154
|
|
|
throw new Exception\JSON('unable to decode JWT token', $e); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
Log::instance()->debug('decoded payload:'); |
|
|
|
|
158
|
|
|
Log::instance()->debug(print_r($decoded, true)); |
159
|
|
|
|
160
|
|
|
return $decoded; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get status code |
165
|
|
|
* |
166
|
|
|
* @return int |
167
|
|
|
*/ |
168
|
|
|
public function status() |
169
|
|
|
{ |
170
|
|
|
return $this->status; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|