This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
Digest.php
193 lines (174 loc) · 5.23 KB
/
Digest.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Authentication\Adapter;
use Zend\Authentication\Result as AuthenticationResult;
use Zend\Stdlib\ErrorHandler;
use Zend\Crypt\Utils as CryptUtils;
class Digest extends AbstractAdapter
{
/**
* Filename against which authentication queries are performed
*
* @var string
*/
protected $filename;
/**
* Digest authentication realm
*
* @var string
*/
protected $realm;
/**
* Sets adapter options
*
* @param mixed $filename
* @param mixed $realm
* @param mixed $identity
* @param mixed $credential
*/
public function __construct($filename = null, $realm = null, $identity = null, $credential = null)
{
if ($filename !== null) {
$this->setFilename($filename);
}
if ($realm !== null) {
$this->setRealm($realm);
}
if ($identity !== null) {
$this->setIdentity($identity);
}
if ($credential !== null) {
$this->setCredential($credential);
}
}
/**
* Returns the filename option value or null if it has not yet been set
*
* @return string|null
*/
public function getFilename()
{
return $this->filename;
}
/**
* Sets the filename option value
*
* @param mixed $filename
* @return Digest Provides a fluent interface
*/
public function setFilename($filename)
{
$this->filename = (string) $filename;
return $this;
}
/**
* Returns the realm option value or null if it has not yet been set
*
* @return string|null
*/
public function getRealm()
{
return $this->realm;
}
/**
* Sets the realm option value
*
* @param mixed $realm
* @return Digest Provides a fluent interface
*/
public function setRealm($realm)
{
$this->realm = (string) $realm;
return $this;
}
/**
* Returns the username option value or null if it has not yet been set
*
* @return string|null
*/
public function getUsername()
{
return $this->getIdentity();
}
/**
* Sets the username option value
*
* @param mixed $username
* @return Digest Provides a fluent interface
*/
public function setUsername($username)
{
return $this->setIdentity($username);
}
/**
* Returns the password option value or null if it has not yet been set
*
* @return string|null
*/
public function getPassword()
{
return $this->getCredential();
}
/**
* Sets the password option value
*
* @param mixed $password
* @return Digest Provides a fluent interface
*/
public function setPassword($password)
{
return $this->setCredential($password);
}
/**
* Defined by Zend\Authentication\Adapter\AdapterInterface
*
* @throws Exception\ExceptionInterface
* @return AuthenticationResult
*/
public function authenticate()
{
$optionsRequired = array('filename', 'realm', 'identity', 'credential');
foreach ($optionsRequired as $optionRequired) {
if (null === $this->$optionRequired) {
throw new Exception\RuntimeException("Option '$optionRequired' must be set before authentication");
}
}
ErrorHandler::start(E_WARNING);
$fileHandle = fopen($this->filename, 'r');
$error = ErrorHandler::stop();
if (false === $fileHandle) {
throw new Exception\UnexpectedValueException("Cannot open '$this->filename' for reading", 0, $error);
}
$id = "$this->identity:$this->realm";
$idLength = strlen($id);
$result = array(
'code' => AuthenticationResult::FAILURE,
'identity' => array(
'realm' => $this->realm,
'username' => $this->identity,
),
'messages' => array()
);
while (($line = fgets($fileHandle)) !== false) {
$line = trim($line);
if (empty($line)) {
break;
}
if (substr($line, 0, $idLength) === $id) {
if (CryptUtils::compareStrings(substr($line, -32), md5("$this->identity:$this->realm:$this->credential"))) {
return new AuthenticationResult(AuthenticationResult::SUCCESS, $result['identity'], $result['messages']);
}
$result['messages'][] = 'Password incorrect';
return new AuthenticationResult(AuthenticationResult::FAILURE_CREDENTIAL_INVALID, $result['identity'], $result['messages']);
}
}
$result['messages'][] = "Username '$this->identity' and realm '$this->realm' combination not found";
return new AuthenticationResult(AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND, $result['identity'], $result['messages']);
}
}