Signature::verify()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 3
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\AuthClient\Signature;
6
7
/**
8
 * BaseMethod is a base class for the OAuth signature methods.
9
 */
10
abstract class Signature
11
{
12
    /**
13
     * Return the canonical name of the Signature Method.
14
     *
15
     * @return string method name.
16
     */
17
    abstract public function getName(): string;
18
19
    /**
20
     * Verifies given OAuth request.
21
     *
22
     * @param string $signature signature to be verified.
23
     * @param string $baseString signature base string.
24
     * @param string $key signature key.
25
     *
26
     * @return bool success.
27
     */
28 1
    public function verify(string $signature, string $baseString, string $key): bool
29
    {
30 1
        $expectedSignature = $this->generateSignature($baseString, $key);
31 1
        if (empty($signature) || empty($expectedSignature)) {
32
            return false;
33
        }
34
35 1
        return strcmp($expectedSignature, $signature) === 0;
36
    }
37
38
    /**
39
     * Generates OAuth request signature.
40
     *
41
     * @param string $baseString signature base string.
42
     * @param string $key signature key.
43
     *
44
     * @return string signature string.
45
     */
46
    abstract public function generateSignature(string $baseString, string $key): string;
47
}
48