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
|
|
|
'key_type' => '', |
24
|
|
|
'algorithm' => '', |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $status; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* |
35
|
|
|
* @param string $json_string JSON string |
36
|
|
|
* @param array $jwt_options array of options |
37
|
|
|
*/ |
38
|
|
|
public function __construct($json_string, $jwt_options=null) |
39
|
|
|
{ |
40
|
|
|
if (isset($jwt_options) && is_array($jwt_options)) |
41
|
|
|
{ |
42
|
|
|
$this->jwt = array_merge($this->jwt, $jwt_options); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->json = $this->json_decode($json_string); |
46
|
|
|
$this->json_handle(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Decode JSON |
51
|
|
|
* |
52
|
|
|
* @param string $json_string |
53
|
|
|
* @return array |
54
|
|
|
* @throws Exception\Runtime |
55
|
|
|
*/ |
56
|
|
|
protected function json_decode($json_string) |
57
|
|
|
{ |
58
|
|
|
$decoded = json_decode($json_string, true); |
59
|
|
|
|
60
|
|
|
if (json_last_error() != 0) |
61
|
|
|
{ |
62
|
|
|
throw new Exception\Runtime('decoding error of the json response!'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $decoded; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handle decoded JSON |
70
|
|
|
* |
71
|
|
|
* @throws Exception\JSON |
72
|
|
|
*/ |
73
|
|
|
protected function json_handle() |
74
|
|
|
{ |
75
|
|
|
$this->check_if_error(); |
76
|
|
|
$this->get_status(); |
77
|
|
|
|
78
|
|
|
$this->check_if_data(); |
79
|
|
|
|
80
|
|
|
if ($this->jwt['using'] === true) |
81
|
|
|
{ |
82
|
|
|
if ( ! isset($this->json['data']['token'])) |
83
|
|
|
{ |
84
|
|
|
throw new Exception\JSON('data does not contain the token field!'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->json['data'] = $this->token_decode($this->json['data']['token']); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if there is an error in JSON |
93
|
|
|
* |
94
|
|
|
* @throws Exception\JSON |
95
|
|
|
*/ |
96
|
|
|
protected function check_if_error() |
97
|
|
|
{ |
98
|
|
|
if (isset($this->json['error'])) |
99
|
|
|
{ |
100
|
|
|
throw new Exception\JSON('json contain an error message!'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Сheck if present status and take value |
106
|
|
|
* |
107
|
|
|
* @throws Exception\JSON |
108
|
|
|
*/ |
109
|
|
|
protected function get_status() |
110
|
|
|
{ |
111
|
|
|
if ( ! isset($this->json['status'])) |
112
|
|
|
{ |
113
|
|
|
throw new Exception\JSON('invalid json response!'); |
114
|
|
|
} |
115
|
|
|
if ($this->json['status'] == 0) |
116
|
|
|
{ |
117
|
|
|
throw new Exception\JSON('json response contain an error status!'); |
118
|
|
|
} |
119
|
|
|
$this->status = $this->json['status']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Check if there is a data in JSON |
124
|
|
|
* |
125
|
|
|
* @throws Exception\JSON |
126
|
|
|
*/ |
127
|
|
|
protected function check_if_data() |
128
|
|
|
{ |
129
|
|
|
if ( ! isset($this->json['data']) || !is_array($this->json['data'])) |
130
|
|
|
{ |
131
|
|
|
throw new Exception\JSON('json does not contain the data field!'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get UAPAY public key |
137
|
|
|
* |
138
|
|
|
* @return string Public key |
139
|
|
|
*/ |
140
|
|
|
protected function uapay_public_key() |
141
|
|
|
{ |
142
|
|
|
return (new Key($this->jwt['key_type']))->get($this->jwt['UAPAY_pubkey'], 'public'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Decode token |
147
|
|
|
* |
148
|
|
|
* @param string $token |
149
|
|
|
* @throws Exception\Runtime |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
protected function token_decode($token) |
153
|
|
|
{ |
154
|
|
|
try |
155
|
|
|
{ |
156
|
|
|
$decoded = (array) JWT::decode($token, $this->uapay_public_key(), array($this->jwt['algorithm'])); |
157
|
|
|
} |
158
|
|
|
catch (\Exception $e) |
159
|
|
|
{ |
160
|
|
|
Log::instance()->error($e->getMessage().PHP_EOL.$e->getTraceAsString()); |
161
|
|
|
throw new Exception\JSON('unable to decode JWT token', $e); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
Log::instance()->debug('decoded payload:'); |
165
|
|
|
Log::instance()->debug(print_r($decoded, true)); |
166
|
|
|
|
167
|
|
|
return $decoded; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get status code |
172
|
|
|
* |
173
|
|
|
* @return int |
174
|
|
|
*/ |
175
|
|
|
public function status() |
176
|
|
|
{ |
177
|
|
|
return $this->status; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|