-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathexpired.class.inc
More file actions
157 lines (146 loc) · 4.67 KB
/
Copy pathexpired.class.inc
File metadata and controls
157 lines (146 loc) · 4.67 KB
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
<?php declare(strict_types=1);
namespace LORIS\login;
use \Psr\Http\Message\ServerRequestInterface;
use \Psr\Http\Message\ResponseInterface;
use \LORIS\Middleware\ETagCalculator;
/**
* POST request for authentication.
*
* Used to reset password.
*
* @category Loris
* @package Login
* @author Alizée Wickenheiser <alizee.wickenheiser@mcin.ca>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
class Expired extends \NDB_Page implements ETagCalculator
{
/**
* This function will return a json object for login module.
*
* @param ServerRequestInterface $request The incoming PSR7 request
*
* @return ResponseInterface The outgoing PSR7 response
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
// Ensure POST request.
switch ($request->getMethod()) {
case 'POST':
return $this->_handlePOST($request);
default:
return new \LORIS\Http\Response\JSON\MethodNotAllowed(
$this->allowedMethods()
);
}
}
/**
* Processes the values & saves to database and return a json response.
*
* @param ServerRequestInterface $request The incoming PSR7 request.
*
* @return ResponseInterface The outgoing PSR7 response
*/
private function _handlePOST(ServerRequestInterface $request) : ResponseInterface
{
// Parse POST request body.
$values = json_decode((string) $request->getBody(), true);
if (isset($values['command'])
&& $values['command'] === 'expired'
) {
// login - expired password update
return $this->expiredPassword($request);
}
return new \LORIS\Http\Response\JSON\BadRequest(
dgettext('login', 'Error')
);
}
/**
* Expired password update.
*
* @param ServerRequestInterface $request The incoming PSR7 request.
*
* @return ResponseInterface The outgoing PSR7 response
*/
function expiredPassword(ServerRequestInterface $request) : ResponseInterface
{
// Parse POST request body.
$values = json_decode((string) $request->getBody(), true);
// Check if password is not empty.
if (empty($values['password']) || empty($values['confirm'])) {
return new \LORIS\Http\Response\JSON\Conflict(
dgettext('login', 'Can not use an empty password.')
);
}
// Update the user password.
if (isset($_SESSION['PasswordExpiredForUser'])) {
$user = \User::factory($_SESSION['PasswordExpiredForUser']);
$plaintext = htmlspecialchars_decode($values['password']);
// Check if new password is same as old password.
if (!$user->isPasswordDifferent($plaintext)) {
return new \LORIS\Http\Response\JSON\Conflict(
dgettext('login', 'You cannot keep the same password.')
);
}
// Update password
try {
$user->updatePassword(
new \Password(
$plaintext
)
);
unset($_SESSION['PasswordExpiredForUser']);
} catch (\InvalidArgumentException $e) {
return new \LORIS\Http\Response\JSON\Conflict(
$e->getMessage()
);
}
} else {
return new \LORIS\Http\Response\JSON\Conflict(
dgettext('login', 'Server encountered an error.')
);
}
return new \LORIS\Http\Response\JsonResponse(
$values
);
}
/**
* An ETagCalculator provides the ability to calculate an ETag for
* an incoming HTTP request.
*
* @param ServerRequestInterface $request The incoming PSR7 request.
*
* @return string The value to use for the ETag header.
*/
public function ETag(ServerRequestInterface $request): string
{
if ($request->getMethod() === 'POST') {
return '';
}
}
/**
* Return an array of valid HTTP methods for this endpoint
*
* @return string[] Valid versions
*/
protected function allowedMethods(): array
{
return [
'POST',
];
}
/**
* Returns true if the user has permission to access
* the Login module
*
* @param \User $user The user whose access is being checked
*
* @return bool true if user has permission
*/
#[\Override]
public function isAccessibleBy(\User $user) : bool
{
return true;
}
}