1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yrc\web; |
4
|
|
|
|
5
|
|
|
use yrc\web\JsonResponseFormatter; |
6
|
|
|
use yii\web\NotAcceptableHttpException; |
7
|
|
|
use Yii; |
8
|
|
|
|
9
|
|
|
class Json25519ResponseFormatter extends JsonResponseFormatter |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Take the response generated by JsonResponseFormatter and anonymously encrypt it |
13
|
|
|
* @param array $response |
14
|
|
|
*/ |
15
|
|
|
protected function formatJson($response) |
16
|
|
|
{ |
17
|
|
|
parent::formatJson($response); |
|
|
|
|
18
|
|
|
$response->getHeaders()->set('Content-Type', 'application/json+25519; charset=UTF-8'); |
|
|
|
|
19
|
|
|
|
20
|
|
|
// If we do not have a user identity in place we cannot encrypt the response. Tell the user the Accept headers are not acceptable |
21
|
|
|
if (Yii::$app->user->isGuest) { |
22
|
|
|
throw new NotAcceptableHttpException; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Retrieve the token object from the user |
26
|
|
|
$token = Yii::$app->user->getIdentity()->getToken(); |
27
|
|
|
|
28
|
|
|
// Abort if we don't get a token back. |
29
|
|
|
if ($token === null) { |
30
|
|
|
throw new NotAcceptableHttpException; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Calculate the keypair |
34
|
|
|
$keyPair = \Sodium\crypto_box_keypair_from_secretkey_and_publickey( |
35
|
|
|
\base64_decode($token->getCryptToken()->secret_box_kp), |
36
|
|
|
\base64_decode($token->getCryptToken()->client_public) |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
// Encrypt the content |
40
|
|
|
$nonce = \Sodium\randombytes_buf(\Sodium\CRYPTO_BOX_NONCEBYTES); |
41
|
|
|
$content = \Sodium\crypto_box( |
42
|
|
|
$response->content, |
43
|
|
|
$nonce, |
44
|
|
|
$keyPair |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$signature = \Sodium\crypto_sign_detached( |
48
|
|
|
$content, |
49
|
|
|
\base64_decode($token->getCryptToken()->secret_sign_kp) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
// Calculate a nonce and set it in the header |
53
|
|
|
$response->getHeaders()->set('x-nonce', \base64_encode($nonce)); |
|
|
|
|
54
|
|
|
|
55
|
|
|
// Send the public key in the clear. The client may need this on the initial authentication request |
56
|
|
|
$response->getHeaders()->set('x-pubkey', \base64_encode($token->getCryptToken()->getBoxPublicKey())); |
|
|
|
|
57
|
|
|
$response->getHeaders()->set('x-sigpubkey', \base64_encode($token->getCryptToken()->getSignPublicKey())); |
|
|
|
|
58
|
|
|
// Sign the raw response and send the signature alongside the header |
59
|
|
|
$response->getHeaders()->set('x-signature', \base64_encode($signature)); |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Update the response content |
62
|
|
|
$response->content = \base64_encode($content); |
63
|
|
|
} |
64
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: