Completed
Pull Request — master (#27)
by Harry
02:14
created

UidSignatureValidator::assertUid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 4
crap 3.2098
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Validation;
15
16
use Graze\Gigya\Exception\InvalidTimestampException;
17
use Graze\Gigya\Exception\InvalidUidSignatureException;
18
use Graze\Gigya\Response\ResponseInterface;
19
20
/**
21
 * Class UidSignatureValidator.
22
 */
23
class UidSignatureValidator implements ResponseValidatorInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $secret;
29
30
    /**
31
     * @var Signature
32
     */
33
    private $signature;
34
35
    /**
36
     * @param Signature $signature
37
     * @param string    $secret
38
     */
39 13
    public function __construct(Signature $signature, $secret)
40
    {
41 13
        $this->secret    = $secret;
42 13
        $this->signature = $signature;
43 13
    }
44
45
    /**
46
     * Can validate.
47
     *
48
     * @param ResponseInterface $response
49
     *
50
     * @return bool
51
     */
52 8 View Code Duplication
    public function canValidate(ResponseInterface $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 8
        $data = $response->getData();
55
56 8
        return ($data->has('UID') &&
57 8
            $data->has('UIDSignature') &&
58 8
            $data->has('signatureTimestamp'));
59
    }
60
61
    /**
62
     * Throws exceptions if any errors are found.
63
     *
64
     * @param ResponseInterface $response
65
     *
66
     * @return bool
67
     */
68 3 View Code Duplication
    public function validate(ResponseInterface $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70 3
        $data = $response->getData();
71
72 3
        return $this->validateUid(
73 3
            $data->get('UID'),
74 3
            $data->get('signatureTimestamp'),
75 3
            $data->get('UIDSignature')
76
        );
77
    }
78
79
    /**
80
     * @param ResponseInterface $response
81
     *
82
     * @throws InvalidTimestampException
83
     * @throws InvalidUidSignatureException
84
     *
85
     * @return void
86
     */
87 1 View Code Duplication
    public function assert(ResponseInterface $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89 1
        $data = $response->getData();
90
91 1
        $this->assertUid(
92 1
            $data->get('UID'),
93 1
            $data->get('signatureTimestamp'),
94 1
            $data->get('UIDSignature'),
95 1
            $response
96
        );
97 1
    }
98
99
    /**
100
     * Validate the provided Uid signature is valid.
101
     *
102
     * @param string $uid
103
     * @param int    $timestamp Unix Timestamp
104
     * @param string $signature
105
     *
106
     * @return bool
107
     */
108 3
    public function validateUid($uid, $timestamp, $signature)
109
    {
110 3
        return ($this->signature->checkTimestamp($timestamp) &&
111 3
            $signature == $this->signature->getUidSignature($uid, $timestamp, $this->secret));
112
    }
113
114
    /**
115
     * @param string            $uid
116
     * @param int               $timestamp Unix Timestamp
117
     * @param string            $signature
118
     * @param ResponseInterface $response
119
     *
120
     * @throws InvalidTimestampException
121
     * @throws InvalidUidSignatureException
122
     *
123
     * @return bool
124
     */
125 1
    private function assertUid($uid, $timestamp, $signature, ResponseInterface $response)
126
    {
127 1
        if (!$this->signature->checkTimestamp($timestamp)) {
128
            throw new InvalidTimestampException($timestamp, $response);
129
        }
130 1
        $expected = $this->signature->getUidSignature($uid, $timestamp, $this->secret);
131 1
        if ($signature !== $expected) {
132
            throw new InvalidUidSignatureException($uid, $expected, $signature, $response);
133
        }
134
135 1
        return true;
136
    }
137
}
138