-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AbstractUsernamePasswordAuthenticationStrategy.php
119 lines (103 loc) · 3.17 KB
/
AbstractUsernamePasswordAuthenticationStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Part of the Joomla Framework Authentication Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Authentication;
use Joomla\Authentication\Password\BCryptHandler;
use Joomla\Authentication\Password\HandlerInterface;
/**
* Abstract AuthenticationStrategy for username/password based authentication
*
* @since 1.1.0
*/
abstract class AbstractUsernamePasswordAuthenticationStrategy implements AuthenticationStrategyInterface
{
/**
* The password handler to validate the password against.
*
* @var HandlerInterface
* @since 1.2.0
*/
protected $passwordHandler;
/**
* The last authentication status.
*
* @var integer
* @since 1.1.0
*/
protected $status;
/**
* Constructor.
*
* @param ?HandlerInterface $passwordHandler The password handler.
*
* @since 1.2.0
*/
public function __construct(?HandlerInterface $passwordHandler = null)
{
$this->passwordHandler = $passwordHandler ?: new BCryptHandler();
}
/**
* Attempt to authenticate the username and password pair.
*
* @param string $username The username to authenticate.
* @param string $password The password to attempt authentication with.
*
* @return string|boolean A string containing a username if authentication is successful, false otherwise.
*
* @since 1.1.0
*/
protected function doAuthenticate($username, $password)
{
$hashedPassword = $this->getHashedPassword($username);
if ($hashedPassword === false) {
$this->status = Authentication::NO_SUCH_USER;
return false;
}
if (!$this->verifyPassword($username, $password, $hashedPassword)) {
$this->status = Authentication::INVALID_CREDENTIALS;
return false;
}
$this->status = Authentication::SUCCESS;
return $username;
}
/**
* Retrieve the hashed password for the specified user.
*
* @param string $username Username to lookup.
*
* @return string|boolean Hashed password on success or boolean false on failure.
*
* @since 1.1.0
*/
abstract protected function getHashedPassword($username);
/**
* Get the status of the last authentication attempt.
*
* @return integer Authentication class constant result.
*
* @since 1.1.0
*/
public function getResult()
{
return $this->status;
}
/**
* Attempt to verify the username and password pair.
*
* @param string $username The username to authenticate.
* @param string $password The password to attempt authentication with.
* @param string $hashedPassword The hashed password to attempt authentication against.
*
* @return boolean
*
* @since 1.1.0
*/
protected function verifyPassword($username, $password, $hashedPassword)
{
return $this->passwordHandler->validatePassword($password, $hashedPassword);
}
}