FallbackPasswordHasher::hash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12
 * @link          http://cakephp.org CakePHP(tm) Project
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
16
declare(strict_types=1);
17
18
namespace Phauthentic\PasswordHasher;
19
20
use RuntimeException;
21
22
/**
23
 * A password hasher that can use multiple different hashes where only
24
 * one is the preferred one. This is useful when trying to migrate an
25
 * existing database of users from one password type to another.
26
 */
27
class FallbackPasswordHasher extends AbstractPasswordHasher
28
{
29
    /**
30
     * Holds the list of password hasher objects that will be used
31
     *
32
     * @var \Phauthentic\PasswordHasher\PasswordHasherCollectionInterface
33
     */
34
    protected $hashers;
35
36
    /**
37
     * Constructor
38
     *
39
     * @param \Phauthentic\PasswordHasher\PasswordHasherCollectionInterface $hasherCollection Hasher Collection
40
     */
41 10
    public function __construct(PasswordHasherCollectionInterface $hasherCollection)
42
    {
43 10
        if ($hasherCollection->count() === 0) {
44 2
            throw new RuntimeException('Your password hasher collection is empty. It must contain at least one hasher.');
45
        }
46
47 8
        $this->hashers = $hasherCollection;
48 8
    }
49
50
    /**
51
     * Adds a hasher
52
     *
53
     * @param PasswordHasherInterface $hasher Hasher instance.
54
     * @return void
55
     */
56
    public function addHasher(PasswordHasherInterface $hasher): void
57
    {
58
        $this->hashers->add($hasher);
59
    }
60
61
    /**
62
     * Generates password hash.
63
     *
64
     * Uses the first password hasher in the list to generate the hash
65
     *
66
     * @param string $password Plain text password to hash.
67
     * @return string Password hash
68
     */
69 2
    public function hash(string $password): string
70
    {
71 2
        $password = $this->saltPassword($password);
72
73 2
        return $this->hashers[0]->hash($password);
74
    }
75
76
    /**
77
     * Verifies that the provided password corresponds to its hashed version
78
     *
79
     * This will iterate over all configured hashers until one of them returns
80
     * true.
81
     *
82
     * @param string $password Plain text password to hash.
83
     * @param string $hashedPassword Existing hashed password.
84
     * @return bool True if hashes match else false.
85
     */
86 4
    public function check(string $password, string $hashedPassword): bool
87
    {
88 4
        $password = $this->saltPassword($password);
89
90
        /* @var $hasher \Phauthentic\PasswordHasher\PasswordHasherInterface */
91 4
        foreach ($this->hashers as $hasher) {
92 4
            if ($hasher->check($password, $hashedPassword)) {
93 4
                return true;
94
            }
95
        }
96
97
        return false;
98
    }
99
100
    /**
101
     * Returns true if the password need to be rehashed, with the first hasher present
102
     * in the list of hashers
103
     *
104
     * @param string $password The password to verify
105
     * @return bool
106
     */
107 2
    public function needsRehash(string $password): bool
108
    {
109 2
        $password = $this->saltPassword($password);
110
111 2
        return $this->hashers[0]->needsRehash($password);
112
    }
113
}
114