|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace yrc\web\ncryptf; |
|
4
|
|
|
|
|
5
|
|
|
use ncryptf\Request; |
|
6
|
|
|
use ncryptf\Response; |
|
7
|
|
|
use yrc\models\redis\EncryptionKey; |
|
8
|
|
|
use yii\web\NotAcceptableHttpException; |
|
9
|
|
|
use yii\web\HttpException; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
|
|
14
|
|
|
class JsonResponseFormatter extends \yrc\web\JsonResponseFormatter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Take the response generated by JsonResponseFormatter and encrypt it |
|
18
|
|
|
* @param array $response |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function formatJson($response) |
|
21
|
|
|
{ |
|
22
|
|
|
// Generate a new encryption key |
|
23
|
|
|
$key = EncryptionKey::generate(); |
|
24
|
|
|
$request = Yii::$app->request; |
|
|
|
|
|
|
25
|
|
|
if ($request->method === 'GET' && $request->getRawBody() === '' && $request->getHeaders()->get('Content-Type') === 'application/json') { |
|
26
|
|
|
$headerExtractionVersion = 1; |
|
27
|
|
|
} else { |
|
28
|
|
|
$headerExtractionVersion = Response::getVersion(\base64_decode($request->getRawBody())); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$headers = $response->getHeaders(); |
|
32
|
|
|
|
|
33
|
|
|
if ($headerExtractionVersion === 2) { |
|
34
|
|
|
$rawPublic = Response::getPublicKeyFromResponse(\base64_decode($request->getRawBody())); |
|
35
|
|
|
$version = 2; |
|
36
|
|
|
} else { |
|
37
|
|
|
$public = $request->getHeaders()->get('x-pubkey', null); |
|
38
|
|
|
$version = 1; |
|
39
|
|
|
|
|
40
|
|
|
if ($public === null) { |
|
|
|
|
|
|
41
|
|
|
$response->statusCode = 400; |
|
42
|
|
|
$response->content = ''; |
|
43
|
|
|
$response->getHeaders('x-reason', Yii::t('yrc', 'Accept: application/vnd.ncryptf+json requires x-pubkey header to be set.')); |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$rawPublic = \base64_decode($public); |
|
|
|
|
|
|
48
|
|
|
if (strlen($rawPublic) !== 32) { |
|
49
|
|
|
$response->statusCode = 400; |
|
50
|
|
|
$headers->set('x-reason', Yii::t('yrc', 'Public key is not 32 bytes in length.')); |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Upgrade GET request with empty bodies |
|
56
|
|
|
if ($request->method === 'GET' && $request->getRawBody() === '') { |
|
57
|
|
|
$version = 2; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
parent::formatJson($response); |
|
|
|
|
|
|
61
|
|
|
$headers->set('Content-Type', 'application/vnd.ncryptf+json; charset=UTF-8'); |
|
62
|
|
|
|
|
63
|
|
|
if (!Yii::$app->user->isGuest) { |
|
64
|
|
|
$token = Yii::$app->user->getIdentity()->getToken(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
if ($token === null) { |
|
67
|
|
|
$response->statusCode = 406; |
|
68
|
|
|
$response->content = ''; |
|
69
|
|
|
Yii::warning([ |
|
70
|
|
|
'message' => 'Could not fetch token keypair. Unable to generate encrypted response.' |
|
71
|
|
|
], 'yrc'); |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$r = new Request( |
|
76
|
|
|
$key->getBoxSecretKey(), |
|
77
|
|
|
\base64_decode($token->secret_sign_kp) |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$nonce = \random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES); |
|
81
|
|
|
|
|
82
|
|
|
$content = $r->encrypt( |
|
83
|
|
|
$response->content, |
|
84
|
|
|
$rawPublic, |
|
85
|
|
|
$version, |
|
86
|
|
|
$version === 2 ? null : $nonce |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
if ($version === 1) { |
|
90
|
|
|
$signature = $r->sign($response->content); |
|
91
|
|
|
// Sign the raw response and send the signature alongside the header |
|
92
|
|
|
$headers->set('x-sigpubkey', \base64_encode($token->getSignPublicKey())); |
|
93
|
|
|
$headers->set('x-signature', \base64_encode($signature)); |
|
94
|
|
|
$headers->set('x-pubkey-expiration', $key->getPublicKeyExpiration()); |
|
95
|
|
|
$headers->set('x-nonce', \base64_encode($nonce)); |
|
96
|
|
|
$headers->set('x-pubkey', \base64_encode($key->getBoxPublicKey())); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$headers->set('x-hashid', $key->hash); |
|
100
|
|
|
$response->content = \base64_encode($content); |
|
101
|
|
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.