SecretKeyValidator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 84
c 0
b 0
f 0
wmc 12
lcom 1
cbo 5
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A enforceGoogleAuthenticatorCompatibility() 0 4 1
A isGoogleAuthenticatorCompatibilityEnforced() 0 4 1
A validate() 0 14 4
A checkGoogleAuthenticatorCompatibility() 0 7 3
A checkForValidCharacters() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/2fa-library project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\TwoFA\Validator;
13
14
use Da\TwoFA\Contracts\ValidatorInterface;
15
use Da\TwoFA\Exception\GoogleAuthenticatorCompatibilityException;
16
use Da\TwoFA\Exception\InvalidCharactersException;
17
use Da\TwoFA\Traits\FailReasonTrait;
18
19
class SecretKeyValidator implements ValidatorInterface
20
{
21
    use FailReasonTrait;
22
23
    /**
24
     * Enforce Google Authenticator compatibility.
25
     */
26
    protected $googleAuthenticatorCompatibility;
27
28
    /**
29
     * Encoder constructor.
30
     *
31
     * @param bool $enforceGoogleAuthenticatorCompatibility
32
     */
33
    public function __construct($enforceGoogleAuthenticatorCompatibility = true)
34
    {
35
        $this->enforceGoogleAuthenticatorCompatibility($enforceGoogleAuthenticatorCompatibility);
36
    }
37
38
    /**
39
     * @param $enforce
40
     */
41
    public function enforceGoogleAuthenticatorCompatibility($enforce)
42
    {
43
        $this->googleAuthenticatorCompatibility = (bool)$enforce;
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function isGoogleAuthenticatorCompatibilityEnforced()
50
    {
51
        return $this->googleAuthenticatorCompatibility;
52
    }
53
54
    /**
55
     * @param mixed $value
56
     *
57
     * @return bool
58
     */
59
    public function validate($value)
60
    {
61
        try {
62
            $this->resetFailReason();
63
            $this->checkForValidCharacters($value);
64
            $this->checkGoogleAuthenticatorCompatibility($value);
65
        } catch (InvalidCharactersException $e) {
66
            $this->failReason = 'Secret key contains invalid characters.';
67
        } catch (GoogleAuthenticatorCompatibilityException $e) {
68
            $this->failReason = 'Google incompatible secret key.';
69
        }
70
71
        return null === $this->failReason ? true : false;
72
    }
73
74
    /**
75
     * Check if the secret key is compatible with Google Authenticator.
76
     *
77
     * @param string $value
78
     *
79
     * @throws GoogleAuthenticatorCompatibilityException
80
     */
81
    protected function checkGoogleAuthenticatorCompatibility($value)
82
    {
83
        if ($this->isGoogleAuthenticatorCompatibilityEnforced() &&
84
            !(new GoogleAuthenticationCompatibilityValidator())->validate($value)) {
85
            throw new GoogleAuthenticatorCompatibilityException();
86
        }
87
    }
88
89
    /**
90
     * Check if all secret key characters are valid.
91
     *
92
     * @param string $value
93
     *
94
     * @throws InvalidCharactersException
95
     */
96
    protected function checkForValidCharacters($value)
97
    {
98
        if (!(new CharactersValidator())->validate($value)) {
99
            throw new InvalidCharactersException();
100
        }
101
    }
102
}
103