1
|
|
|
<?php |
2
|
|
|
namespace Graceland\SafeInCloud; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
5
|
|
|
|
6
|
|
|
class Response |
7
|
|
|
{ |
8
|
|
|
protected $body; |
9
|
|
|
protected $encrypter; |
10
|
|
|
protected $request; |
11
|
|
|
protected $response; |
12
|
|
|
|
13
|
|
|
public function __construct(ResponseInterface $response, Request $request) |
14
|
|
|
{ |
15
|
|
|
$this->request = $request; |
16
|
|
|
$this->response = $response; |
17
|
|
|
$this->encrypter = $this->request->getEncrypter(); |
18
|
|
|
$this->body = $this->response->json(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
|
|
public function get($key, $default = null) |
25
|
|
|
{ |
26
|
|
|
if ($this->has($key) === false) { |
27
|
|
|
return $default; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $this->body[$key]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function getDecrypted($key, $default = null, $assocKeys = []) |
37
|
|
|
{ |
38
|
|
|
$value = $this->get($key, $default); |
39
|
|
|
|
40
|
|
|
if ($value === $default) { |
41
|
|
|
return $default; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->decrypt($value, null, $assocKeys); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function has($key) |
48
|
|
|
{ |
49
|
|
|
return (isset($this->body[$key]) === true); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isSuccessful() |
53
|
|
|
{ |
54
|
|
|
return (isset($this->body['success']) === true && $this->body['success'] === true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isValid() |
58
|
|
|
{ |
59
|
|
|
if ($this->response->getStatusCode() < 200 || $this->response->getStatusCode() >= 300) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (isset($this->body['nonce']) === false || isset($this->body['verifier']) === false) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$nonce = base64_decode($this->body['nonce']); |
68
|
|
|
$verifier = $this->decrypt($this->body['verifier'], $nonce); |
69
|
|
|
|
70
|
|
|
return $verifier === $this->body['nonce']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
protected function decrypt($data, $nonce = null, $assocKeys = []) |
77
|
|
|
{ |
78
|
|
|
if ($nonce === null) { |
79
|
|
|
$nonce = $this->request->getNonce(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (is_array($data) === false) { |
83
|
|
|
return $this->encrypter->decrypt(base64_decode($data), $nonce); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$decrypted = []; |
87
|
|
|
$isAssoc = (bool) count(array_filter(array_keys($data), 'is_string')); |
88
|
|
|
|
89
|
|
|
foreach ($data as $key => $value) { |
90
|
|
|
if ($isAssoc === true and in_array($key, $assocKeys) === false) { |
|
|
|
|
91
|
|
|
$decrypted[$key] = $value; |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$decrypted[$key] = $this->decrypt($value, $nonce, $assocKeys); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $decrypted; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.