Passed
Push — master ( e9ba59...c2c890 )
by Tony Karavasilev (Тони
19:20
created

PasswordBasedAuthentication   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 1
b 0
f 0
dl 0
loc 83
ccs 0
cts 29
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 4 2
A __construct() 0 3 1
A authenticateEntity() 0 16 4
A __destruct() 0 3 1
1
<?php
2
3
/**
4
 * Cryptographic protocol for password-based authentication.
5
 */
6
7
namespace CryptoManana\CryptographicProtocol;
8
9
use \CryptoManana\Core\Abstractions\Containers\AbstractCryptographicProtocol as CryptographicProtocol;
10
use \CryptoManana\Core\Abstractions\MessageDigestion\AbstractHashAlgorithm as HashFunction;
11
use \CryptoManana\Core\Interfaces\Containers\EntityIdentificationInterface as IdentifyEntities;
12
use \CryptoManana\Core\Interfaces\Containers\EntityAuthenticationInterface as AuthenticateEntities;
13
use \CryptoManana\Core\Interfaces\MessageDigestion\SecureVerificationInterface as VerificationAlgorithm;
14
use \CryptoManana\Core\Interfaces\Containers\VerificationAlgorithmInjectableInterface as HashAlgorithmSetter;
15
use \CryptoManana\Core\Traits\Containers\VerificationAlgorithmInjectableTrait as HashAlgorithmSetterImplementation;
16
use \CryptoManana\Core\Traits\Containers\EntityIdentificationTrait as EntityIdentificationProcess;
17
18
/**
19
 * Class PasswordBasedAuthentication - The password-based authentication protocol object.
20
 *
21
 * @package CryptoManana\CryptographicProtocol
22
 *
23
 * @mixin HashAlgorithmSetterImplementation
24
 * @mixin EntityIdentificationProcess
25
 */
26
class PasswordBasedAuthentication extends CryptographicProtocol implements
27
    HashAlgorithmSetter,
28
    IdentifyEntities,
29
    AuthenticateEntities
30
{
31
    /**
32
     * The verification message digestion service dependency injection via a setter method implementation.
33
     *
34
     * {@internal Reusable implementation of `VerificationAlgorithmInjectableInterface`. }}
35
     */
36
    use HashAlgorithmSetterImplementation;
37
38
    /**
39
     * The entity identification capabilities.
40
     *
41
     * {@internal Reusable implementation of `EntityIdentificationInterface`. }}
42
     */
43
    use EntityIdentificationProcess;
44
45
    /**
46
     * The message digestion and verification service property storage.
47
     *
48
     * @var HashFunction|VerificationAlgorithm|null The message digestion service.
49
     */
50
    protected $verificationSource = null;
51
52
    /**
53
     * Container constructor.
54
     *
55
     * @param HashFunction|VerificationAlgorithm|null $hasher The message digestion and verification service.
56
     *
57
     * @throws \Exception Initialization validation.
58
     *
59
     * @internal If `null` is passed, then the comparison will be raw binary based.
60
     */
61
    public function __construct(VerificationAlgorithm $hasher = null)
62
    {
63
        $this->verificationSource = $hasher;
64
    }
65
66
    /**
67
     * Container destructor.
68
     */
69
    public function __destruct()
70
    {
71
        unset($this->verificationSource);
72
    }
73
74
    /**
75
     * Container cloning via deep copy.
76
     */
77
    public function __clone()
78
    {
79
        if ($this->verificationSource !== null) {
80
            $this->verificationSource = clone $this->verificationSource;
81
        }
82
    }
83
84
    /**
85
     * Authenticate a user or a client entity.
86
     *
87
     * @param string $correctPassphrase The correct passphrase information.
88
     * @param string $suppliedPassphrase The supplied passphrase information.
89
     *
90
     * @return bool The identity authentication result.
91
     * @throws \Exception Validation errors.
92
     */
93
    public function authenticateEntity($correctPassphrase, $suppliedPassphrase)
94
    {
95
        if (!is_string($correctPassphrase)) {
1 ignored issue
show
introduced by
The condition is_string($correctPassphrase) is always true.
Loading history...
96
            throw new \InvalidArgumentException(
97
                'The correct passphrase or hash value for verification must be a string or a binary string.'
98
            );
99
        } elseif (!is_string($suppliedPassphrase)) {
1 ignored issue
show
introduced by
The condition is_string($suppliedPassphrase) is always true.
Loading history...
100
            throw new \InvalidArgumentException(
101
                'The supplied user passphrase value must be a string or a binary string.'
102
            );
103
        }
104
105
        if ($this->verificationSource !== null) {
106
            return $this->verificationSource->verifyHash($suppliedPassphrase, $correctPassphrase);
1 ignored issue
show
Bug introduced by
The method verifyHash() does not exist on CryptoManana\Core\Abstra...n\AbstractHashAlgorithm. It seems like you code against a sub-type of CryptoManana\Core\Abstra...n\AbstractHashAlgorithm such as CryptoManana\Core\Abstra...stractKeyedHashFunction or CryptoManana\Core\Abstra...BasedDerivationFunction. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            return $this->verificationSource->/** @scrutinizer ignore-call */ verifyHash($suppliedPassphrase, $correctPassphrase);
Loading history...
107
        } else {
108
            return hash_equals($correctPassphrase, $suppliedPassphrase);
109
        }
110
    }
111
}
112