Signature::isValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Omnipay\Paysera\Common;
4
5
use Omnipay\Common\Http\ClientInterface;
6
7
class Signature
8
{
9
    /**
10
     * Endpoint to the public key.
11
     *
12
     * @var string
13
     */
14
    const ENDPOINT = 'http://www.paysera.com/download/public.key';
15
16
    /**
17
     * Generate the signature.
18
     *
19
     * @param  string  $data
20
     * @param  string  $password
21
     * @return string
22
     */
23 9
    public static function generate($data, $password)
24
    {
25 9
        return md5($data.$password);
26
    }
27
28
    /**
29
     * Determine the whole signature is valid.
30
     *
31
     * @param  array  $data
32
     * @param  string  $password
33
     * @param  \Omnipay\Common\Http\ClientInterface  $client
34
     * @return bool
35
     */
36 7
    public static function isValid(array $data, $password, ClientInterface $client)
37
    {
38 7
        return static::isValidSS1($data, $password) && static::isValidSS2($data, $client);
39
    }
40
41
    /**
42
     * Determine the SS1 is valid.
43
     *
44
     * @param  array  $data
45
     * @param  string  $password
46
     * @return bool
47
     */
48 7
    protected static function isValidSS1(array $data, $password)
49
    {
50 7
        return static::generate($data['data'], $password) === $data['ss1'];
51
    }
52
53
    /**
54
     * Determine the SS2 is valid.
55
     *
56
     * @param  array  $data
57
     * @param  \Omnipay\Common\Http\ClientInterface  $client
58
     * @return bool
59
     */
60 6
    protected static function isValidSS2(array $data, ClientInterface $client)
61
    {
62 6
        $response = $client->request('GET', self::ENDPOINT);
63
64 6
        $publicKey = openssl_get_publickey($response->getBody());
65
66 6
        if ($response->getStatusCode() === 200 && $publicKey !== false) {
67 6
            return openssl_verify($data['data'], Encoder::decode($data['ss2']), $publicKey) === 1;
68
        }
69
70
        return false;
71
    }
72
}
73