This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.OtpAuthFrontend.php
283 lines (243 loc) · 10.3 KB
/
class.OtpAuthFrontend.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
use Pydio\Core\Services\AuthService;
use Pydio\Authfront\Core\AbstractAuthFrontend;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Controller\XMLWriter;
defined('AJXP_EXEC') or die('Access not allowed');
class OtpAuthFrontend extends AbstractAuthFrontend
{
/*
private $enable_Create_User;
private $modifyLoginScreen;
private $userFilePath;*/
private $yubicoSecretKey;
private $yubicoClientId;
private $google;
private $googleLast;
private $yubikey1;
private $yubikey2;
function tryToLogUser(\Psr\Http\Message\ServerRequestInterface &$request, \Psr\Http\Message\ResponseInterface &$response, $isLast = false){
$httpVars = $request->getParsedBody();
$exceptionMsg = "Login information is not correct! Please try again with the one-time-password at the end of your password";
if(empty($httpVars) || empty($httpVars["userid"])){
return false;
} else{
$userid = $httpVars["userid"];
$this->loadConfig($userid);
// if there is no configuration for OTP, this means that this user don't have OTP
if ((empty($this->google) &&
empty($this->googleLast) &&
empty($this->yubikey1) &&
empty($this->yubikey2))) {
return false;
}
// load Yubico class
if(!empty($this->yubikey1)) {
require_once 'Auth/Yubico.php';
}
// cut off password and otp in pass field
$cutPassword = false;
if(isSet($httpVars["otp_code"]) && !empty($httpVars["otp_code"])){
$codeOTP = $httpVars["otp_code"];
}else if(strlen($httpVars["password"]) > 6){
$codeOTP = substr($httpVars["password"], -6);
$cutPassword = true;
}else{
$this->breakAndSendError($exceptionMsg);
}
//Just the Google Authenticator set
if (!empty($this->google) &&
empty($this->yubikey1) &&
empty($this->yubikey2)) {
if($this->checkGooglePass($userid, $codeOTP, $this->google, $this->googleLast)){
$this->logDebug(__CLASS__, __FUNCTION__, "Check OTP: matched");
//return false and cut off otp from password for next authfront.
if($cutPassword){
$httpVars["password"] = substr($httpVars["password"], 0, strlen($httpVars["password"]) - 6);
$request = $request->withParsedBody($httpVars);
}
return false;
}
else{
$this->breakAndSendError($exceptionMsg);
}
}elseif
// YubiKey1 or YubiKey2 set
(empty($this->google) &&
(!empty($this->yubikey1) || !empty($this->yubikey2))
){
if ($this->checkYubiOTP($httpVars["otp_code"], $this->yubikey1, $this->yubikey2)){
return false;
}else{
$this->breakAndSendError($exceptionMsg);
}
}elseif
// Both Yubikey and Google Authenticator set
// If the last character of the password is digit, it is Google Authenticator
(ctype_digit(substr($httpVars["password"], -1))) {
if($this->checkGooglePass($userid, $codeOTP, $this->google, $this->googleLast)){
if($cutPassword){
$httpVars["password"] = substr($httpVars["password"], 0, strlen($httpVars["password"]) - 6);
$request = $request->withParsedBody($httpVars);
}
return false;
}
else{
$this->breakAndSendError($exceptionMsg);
}
}
else{
if ($this->checkYubiOTP($httpVars["otp_code"], $this->yubikey1, $this->yubikey2)){
return false;
}
else{
$this->breakAndSendError($exceptionMsg);
}
}
}
}
protected function breakAndSendError($exceptionMsg){
throw new \Pydio\Core\Exception\AuthRequiredException($exceptionMsg);
}
private function loadConfig($userid)
{
$confStorage = ConfService::getConfStorageImpl();
$userObject = $confStorage->createUserObject($userid);
if($userObject != null){
$this->google = $userObject->mergedRole->filterParameterValue("authfront.otp", "google", AJXP_REPO_SCOPE_ALL, '');
$this->googleLast = $userObject->mergedRole->filterParameterValue("authfront.otp", "google_last", AJXP_REPO_SCOPE_ALL, '');
$this->yubikey1 = $userObject->mergedRole->filterParameterValue("authfront.otp", "yubikey1", AJXP_REPO_SCOPE_ALL, '');
$this->yubikey2 = $userObject->mergedRole->filterParameterValue("authfront.otp", "yubikey2", AJXP_REPO_SCOPE_ALL, '');
}
if (!empty($this->pluginConf["YUBICO_CLIENT_ID"])) {
$this->yubicoClientId = trim($this->pluginConf["YUBICO_CLIENT_ID"]);
}
if (!empty($this->pluginConf["YUBICO_SECRET_KEY"])) {
$this->yubicoSecretKey = trim($this->pluginConf["YUBICO_SECRET_KEY"]);
}
}
// Google Authenticator
public function oath_hotp($key, $counter)
{
$key = pack("H*", $key);
$cur_counter = array(0,0,0,0,0,0,0,0);
for ($i=7;$i>=0;$i--) {
$cur_counter[$i] = pack ('C*', $counter);
$counter = $counter >> 8;
}
$bin_counter = implode($cur_counter);
// Pad to 8 chars
if (strlen ($bin_counter) < 8) {
$bin_counter = str_repeat (chr(0), 8 - strlen ($bin_counter)) . $bin_counter;
}
// HMAC
$hash = hash_hmac ('sha1', $bin_counter, $key);
return str_pad($this->oath_truncate($hash), 6, "0", STR_PAD_LEFT);
}
public function oath_truncate($hash, $length = 6)
{
// Convert to dec
foreach (str_split($hash,2) as $hex) {
$hmac_result[]=hexdec($hex);
}
// Find offset
$offset = $hmac_result[19] & 0xf;
// Algorithm from RFC
return
(
(($hmac_result[$offset+0] & 0x7f) << 24 ) |
(($hmac_result[$offset+1] & 0xff) << 16 ) |
(($hmac_result[$offset+2] & 0xff) << 8 ) |
($hmac_result[$offset+3] & 0xff)
) % pow(10,$length);
}
public function base32ToHex($b32)
{
$alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
$out = "";
$dous = "";
for ($i = 0; $i < strlen($b32); $i++) {
$in = strrpos($alphabet, $b32[$i]);
$b = str_pad(base_convert($in, 10, 2), 5, "0", STR_PAD_LEFT);
$out .= $b;
$dous .= $b.".";
}
$ar = str_split($out,20);
$out2 = "";
foreach ($ar as $val) {
$rv = str_pad(base_convert($val, 2, 16), 5, "0", STR_PAD_LEFT);
$out2 .= $rv;
}
return $out2;
}
public function checkGooglePass($loginId, $codeOTP, $userToken, $userInvalid)
{
$userToken = $this->base32ToHex($userToken);
$now = time();
$totpSkew = 2; // how many tokens in either side we should check. 2 means +-1 min
$tokenTimer = 30; // google authenticator support just 30s
$earliest = $now - ($totpSkew * $tokenTimer);
$latest = $now + ($totpSkew * $tokenTimer);
$st = ((int) ($earliest / $tokenTimer));
$en = ((int) ($latest / $tokenTimer));
$valid = 0;
$this->logDebug(__CLASS__, __FUNCTION__, "codeOTP ".$codeOTP);
for ($i=$st; ($i<=$en && $valid == 0); $i++) {
if ($i > $userInvalid) {
$stest = $this->oath_hotp($userToken, $i);
$this->logDebug(__CLASS__, __FUNCTION__, "stest ".$stest);
if ($codeOTP == $stest) {
$valid = 1;
// save google_last
$confStorage = ConfService::getConfStorageImpl();
$userObject = $confStorage->createUserObject($loginId);
$role = $userObject->personalRole;
if ($role === false) {
throw new Exception("Cant find role! ");
}
$role->setParameterValue("authfront.otp", "google_last", $i);
AuthService::updateRole($role, $userObject);
return true;
}
}
}
return false;
//return ( AJXP_Utils::pbkdf2_validate_password($pass, $userStoredPass) && $valid == 1);
}
// YubiKey
public function checkYubiOTP($otp_code, $yubikey1, $yubikey2)
{
// yubikey generates 44 character, identity is the first 12 character
$yubi1_identity = substr($yubikey1, 0, 12);
$yubi2_identity = substr($yubikey2, 0, 12);
$otp_identity = substr($otp_code, -44, 12);
if (($otp_identity != $yubi1_identity) and ($otp_identity != $yubi2_identity)) {
// YubiKey not listed in account
return false;
}
$yotp = substr($otp_code, -44);
$otp_code = substr($otp_code, 0, strlen($otp_code) - 44);
$yubi = new Auth_Yubico($this->yubicoClientId, $this->yubicoSecretKey);
$auth = $yubi->verify($yotp);
return (!PEAR::isError($auth));
}
}