Client::decrypt()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 17
ccs 0
cts 10
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\MicroMerchant\Certficates;
13
14
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
15
use EasyWeChat\MicroMerchant\Kernel\BaseClient;
16
use EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException;
17
18
/**
19
 * Class Client.
20
 *
21
 * @author   liuml  <[email protected]>
22
 * @DateTime 2019-05-30  14:19
23
 */
24
class Client extends BaseClient
25
{
26
    /**
27
     * get certficates.
28
     *
29
     * @param bool $returnRaw
30
     *
31
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
32
     *
33
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
34
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
35
     * @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException
36
     * @throws \GuzzleHttp\Exception\GuzzleException
37
     */
38
    public function get(bool $returnRaw = false)
39
    {
40
        $params = [
41
            'sign_type' => 'HMAC-SHA256',
42
            'nonce_str' => uniqid('micro'),
43
        ];
44
45
        if (true === $returnRaw) {
46
            return $this->requestRaw('risk/getcertficates', $params);
47
        }
48
        /** @var array $response */
49
        $response = $this->requestArray('risk/getcertficates', $params);
50
51
        if ('SUCCESS' !== $response['return_code']) {
52
            throw new InvalidArgumentException(sprintf('Failed to get certificate. return_code_msg: "%s" .', $response['return_code'].'('.$response['return_msg'].')'));
53
        }
54
        if ('SUCCESS' !== $response['result_code']) {
55
            throw new InvalidArgumentException(sprintf('Failed to get certificate. result_err_code_desc: "%s" .', $response['result_code'].'('.$response['err_code'].'['.$response['err_code_desc'].'])'));
56
        }
57
        $certificates = \GuzzleHttp\json_decode($response['certificates'], true)['data'][0];
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\json_decode() has been deprecated: json_decode will be removed in guzzlehttp/guzzle:8.0. Use Utils::jsonDecode instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

57
        $certificates = /** @scrutinizer ignore-deprecated */ \GuzzleHttp\json_decode($response['certificates'], true)['data'][0];

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
58
        $ciphertext = $this->decrypt($certificates['encrypt_certificate']);
59
        unset($certificates['encrypt_certificate']);
60
        $certificates['certificates'] = $ciphertext;
61
62
        return $certificates;
63
    }
64
65
    /**
66
     * decrypt ciphertext.
67
     *
68
     * @param array $encryptCertificate
69
     *
70
     * @return string
71
     *
72
     * @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException
73
     */
74
    public function decrypt(array $encryptCertificate)
75
    {
76
        if (false === extension_loaded('sodium')) {
77
            throw new InvalidExtensionException('sodium extension is not installed,Reference link https://php.net/manual/zh/book.sodium.php');
78
        }
79
80
        if (false === sodium_crypto_aead_aes256gcm_is_available()) {
81
            throw new InvalidExtensionException('aes256gcm is not currently supported');
82
        }
83
84
        // sodium_crypto_aead_aes256gcm_decrypt function needs to open libsodium extension.
85
        // https://www.php.net/manual/zh/function.sodium-crypto-aead-aes256gcm-decrypt.php
86
        return sodium_crypto_aead_aes256gcm_decrypt(
87
            base64_decode($encryptCertificate['ciphertext'], true),
88
            $encryptCertificate['associated_data'],
89
            $encryptCertificate['nonce'],
90
            $this->app['config']->apiv3_key
91
        );
92
    }
93
}
94