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 string $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
|
|
|
$this->check_if_error(); |
74
|
|
|
$this->get_status(); |
75
|
|
|
|
76
|
|
|
$this->check_if_data(); |
77
|
|
|
|
78
|
|
|
if ($this->jwt['using'] === true) |
79
|
|
|
{ |
80
|
|
|
if ( ! isset($this->json['data']['token'])) |
81
|
|
|
{ |
82
|
|
|
throw new Exception\JSON('data does not contain the token field!'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->json['data'] = $this->token_decode($this->json['data']['token']); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check if there is an error in JSON |
91
|
|
|
* |
92
|
|
|
* @throws Exception\JSON |
93
|
|
|
*/ |
94
|
|
|
protected function check_if_error() |
95
|
|
|
{ |
96
|
|
|
if (isset($this->json['error'])) |
97
|
|
|
{ |
98
|
|
|
throw new Exception\JSON('json contain an error message!'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Сheck if present status and take value |
104
|
|
|
* |
105
|
|
|
* @throws Exception\JSON |
106
|
|
|
*/ |
107
|
|
|
protected function get_status() |
108
|
|
|
{ |
109
|
|
|
if ( ! isset($this->json['status'])) |
110
|
|
|
{ |
111
|
|
|
throw new Exception\JSON('invalid json response!'); |
112
|
|
|
} |
113
|
|
|
if ($this->json['status'] == 0) |
114
|
|
|
{ |
115
|
|
|
throw new Exception\JSON('json response contain an error status!'); |
116
|
|
|
} |
117
|
|
|
$this->status = $this->json['status']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check if there is a data in JSON |
122
|
|
|
* |
123
|
|
|
* @throws Exception\JSON |
124
|
|
|
*/ |
125
|
|
|
protected function check_if_data() |
126
|
|
|
{ |
127
|
|
|
if ( ! isset($this->json['data']) || !is_array($this->json['data'])) |
128
|
|
|
{ |
129
|
|
|
throw new Exception\JSON('json does not contain the data field!'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get UAPAY public key |
135
|
|
|
* |
136
|
|
|
* @return string Public key |
137
|
|
|
*/ |
138
|
|
|
protected function uapay_public_key() |
139
|
|
|
{ |
140
|
|
|
return (new Key())->get($this->jwt['UAPAY_pubkey'], 'public'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Decode token |
145
|
|
|
* |
146
|
|
|
* @param string $token |
147
|
|
|
* @throws Exception\Runtime |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
protected function token_decode($token) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
try |
153
|
|
|
{ |
154
|
|
|
$decoded = (array) JWT::decode($token, $this->uapay_public_key(), array('RS512')); |
155
|
|
|
} |
156
|
|
|
catch (\Exception $e) |
157
|
|
|
{ |
158
|
|
|
Log::instance()->error($e->getMessage().PHP_EOL.$e->getTraceAsString()); |
159
|
|
|
throw new Exception\JSON('unable to decode JWT token', $e); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
Log::instance()->debug('decoded payload:'); |
163
|
|
|
Log::instance()->debug(print_r($decoded, true)); |
164
|
|
|
|
165
|
|
|
return $decoded; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get status code |
170
|
|
|
* |
171
|
|
|
* @return int |
172
|
|
|
*/ |
173
|
|
|
public function status() |
174
|
|
|
{ |
175
|
|
|
return $this->status; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.