Skip to content

Commit 20ec899

Browse files
committed
Auth: allow to reverse password / token order in TOTP
PR: https://forum.opnsense.org/index.php?topic=5466.0
1 parent 738f998 commit 20ec899

File tree

1 file changed

+23
-2
lines changed
  • src/opnsense/mvc/app/library/OPNsense/Auth

1 file changed

+23
-2
lines changed

src/opnsense/mvc/app/library/OPNsense/Auth/TOTP.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ trait TOTP
5353
*/
5454
private $graceperiod = 10;
5555

56+
/**
57+
* @var bool token after password
58+
*/
59+
private $passwordFirst = false;
60+
5661
/**
5762
* @var string method accepting username and returning a simplexml user object
5863
*/
@@ -149,8 +154,14 @@ public function authenticate($username, $password)
149154
if ($userObject != null && !empty($userObject->otp_seed)) {
150155
if (strlen($password) > $this->otpLength) {
151156
// split otp token code and userpassword
152-
$code = substr($password, 0, $this->otpLength);
153-
$userPassword = substr($password, $this->otpLength);
157+
$pwStart = $this->otpLength;
158+
$otpStart = 0;
159+
if ($this->passwordFirst) {
160+
$otpStart = strlen($password) - $this->otpLength;
161+
$pwStart = 0;
162+
}
163+
$userPassword = substr($password, $pwStart, strlen($password) - $this->otpLength);
164+
$code = substr($password, $otpStart, $this->otpLength);
154165
$otp_seed = \Base32\Base32::decode($userObject->otp_seed);
155166
if ($this->authTOTP($otp_seed, $code)) {
156167
// token valid, do parents auth
@@ -176,6 +187,9 @@ public function setTOTPProperties($config)
176187
if (!empty($config['graceperiod'])) {
177188
$this->graceperiod = $config['graceperiod'];
178189
}
190+
if (array_key_exists('passwordFirst', $config) && !empty($config['passwordFirst'])) {
191+
$this->passwordFirst = true;
192+
}
179193
}
180194

181195
/**
@@ -226,6 +240,13 @@ private function getTOTPConfigurationOptions()
226240
return array();
227241
}
228242
};
243+
$fields["passwordFirst"] = array();
244+
$fields["passwordFirst"]["name"] = gettext("Reverse token order");
245+
$fields["passwordFirst"]["help"] = gettext("Require the password in front of the token instead of behind it.");
246+
$fields["passwordFirst"]["type"] = "checkbox";
247+
$fields["passwordFirst"]["validate"] = function ($value) {
248+
return array();
249+
};
229250

230251
return $fields;
231252
}

0 commit comments

Comments
 (0)