RelayRequestTrait::decryptResponse()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 18
c 1
b 0
f 1
nc 5
nop 3
dl 0
loc 27
rs 9.3554
1
<?php
2
3
namespace ByTIC\Omnipay\PlatiOnline\Message\Traits;
4
5
use ByTIC\Omnipay\Common\Message\Traits\GatewayNotificationRequestTrait;
6
use ByTIC\Omnipay\PlatiOnline\Utils\Urls;
7
use Nip\Utility\Str;
8
use Nip\Utility\Xml;
9
use phpseclib\Crypt\AES;
10
use phpseclib\Crypt\RSA;
11
12
/**
13
 * Trait RelayRequestTrait
14
 * @package ByTIC\Omnipay\PlatiOnline\Message\Traits
15
 */
16
trait RelayRequestTrait
17
{
18
    use GatewayNotificationRequestTrait;
19
20
    protected function getRelayMessage()
21
    {
22
        return $this->httpRequest->request->get($this->relayMessageKey);
23
    }
24
25
    protected function getCryptMessage()
26
    {
27
        return $this->httpRequest->request->get($this->cryptMessageKey);
28
    }
29
30
    protected function getValidationMessageUrl()
31
    {
32
        return Urls::$authResponseXml;
33
    }
34
35
    /**
36
     * @return mixed
37
     */
38
    public function isValidNotification()
39
    {
40
        return $this->hasPOST($this->relayMessageKey, $this->cryptMessageKey);
41
    }
42
43
    /**
44
     * @return bool|mixed
45
     */
46
    protected function parseNotification()
47
    {
48
        $this->validate('privateKey');
0 ignored issues
show
Bug introduced by
It seems like validate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

48
        $this->/** @scrutinizer ignore-call */ 
49
               validate('privateKey');
Loading history...
49
50
        $response = $this->decryptResponse(
51
            $this->getRelayMessage(),
52
            $this->getCryptMessage(),
53
            $this->getValidationMessageUrl()
54
        );
55
56
        return $response;
57
    }
58
59
    /**
60
     * @param $relayMessage
61
     * @param $cryptMessage
62
     * @param $validation_url
63
     * @return mixed
64
     * @throws \Exception
65
     */
66
    private function decryptResponse($relayMessage, $cryptMessage, $validation_url)
67
    {
68
        if (empty($relayMessage)) {
69
            throw new \Exception('Decriptare raspuns - nu se primeste [criptul AES]');
70
        }
71
        if (empty($cryptMessage)) {
72
            throw new \Exception('Decriptare raspuns - nu se primeste [criptul RSA]');
73
        }
74
75
        $rsa = new RSA();
76
        $rsa->loadKey($this->getPrivateKey());
0 ignored issues
show
Bug introduced by
It seems like getPrivateKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

76
        $rsa->loadKey($this->/** @scrutinizer ignore-call */ getPrivateKey());
Loading history...
77
        $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
78
        $aes_key = $rsa->decrypt(base64_decode($cryptMessage));
79
        if (empty($aes_key)) {
80
            throw new \Exception('Nu am putut decripta cheia AES din RSA');
81
        }
82
83
        $aes = new AES();
84
        $aes->setIV($this->getInitialVectorItsn());
0 ignored issues
show
Bug introduced by
It seems like getInitialVectorItsn() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

84
        $aes->setIV($this->/** @scrutinizer ignore-call */ getInitialVectorItsn());
Loading history...
85
        $aes->setKey($aes_key);
86
        $response = $aes->decrypt(base64_decode(Str::fromHex($relayMessage)));
87
        if (empty($response)) {
88
            throw new \Exception('Nu am putut decripta mesajul din criptul AES');
89
        }
90
91
        Xml::validate($response, $validation_url);
92
        return simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
93
    }
94
}
95