UidSignatureValidator::canValidate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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 14
    public function __construct(Signature $signature, $secret)
40
    {
41 14
        $this->secret    = $secret;
42 14
        $this->signature = $signature;
43 14
    }
44
45
    /**
46
     * Can validate.
47
     *
48
     * @param ResponseInterface $response
49
     *
50
     * @return bool
51
     */
52 12
    public function canValidate(ResponseInterface $response)
53
    {
54 12
        $data = $response->getData();
55
56 12
        return ($data->has('UID') &&
57 12
            $data->has('UIDSignature') &&
58 12
            $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
    public function validate(ResponseInterface $response)
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 6
    public function assert(ResponseInterface $response)
88
    {
89 6
        $data = $response->getData();
90
91 6
        $this->assertUid(
92 6
            $data->get('UID'),
93 6
            $data->get('signatureTimestamp'),
94 6
            $data->get('UIDSignature'),
95 6
            $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 6
    private function assertUid($uid, $timestamp, $signature, ResponseInterface $response)
126
    {
127 6
        if (!$this->signature->checkTimestamp($timestamp)) {
128 3
            throw new InvalidTimestampException($timestamp, $response);
129
        }
130 3
        $expected = $this->signature->getUidSignature($uid, $timestamp, $this->secret);
131 3
        if ($signature !== $expected) {
132 2
            throw new InvalidUidSignatureException($uid, $expected, $signature, $response);
133
        }
134
135 1
        return true;
136
    }
137
}
138