Fix #5274 2fa backup codes not validating properly

This commit is contained in:
Jamie Curnow
2026-02-05 10:51:15 +10:00
parent ac4efd2333
commit c88de65d3a
4 changed files with 16 additions and 13 deletions

View File

@@ -198,20 +198,23 @@ const internal2fa = {
return false;
}
// Try TOTP code first
const result = await verify({
token,
secret,
});
// Try TOTP code first, if it's 6 chars. it will throw errors if it's not 6 chars
// and the backup codes are 8 chars.
if (token.length === 6) {
const result = await verify({
token,
secret,
});
if (result.valid) {
return true;
if (result.valid) {
return true;
}
}
// Try backup codes
const backupCodes = auth?.meta?.backup_codes || [];
for (let i = 0; i < backupCodes.length; i++) {
const match = await bcrypt.compare(code.toUpperCase(), backupCodes[i]);
const match = await bcrypt.compare(token.toUpperCase(), backupCodes[i]);
if (match) {
// Remove used backup code
const updatedCodes = [...backupCodes];

View File

@@ -17,7 +17,7 @@
},
"code": {
"minLength": 6,
"maxLength": 6,
"maxLength": 8,
"type": "string",
"example": "012345"
}

View File

@@ -16,7 +16,7 @@
}
],
"requestBody": {
"description": "Verififcation Payload",
"description": "Verification Payload",
"required": true,
"content": {
"application/json": {
@@ -25,7 +25,7 @@
"properties": {
"code": {
"minLength": 6,
"maxLength": 6,
"maxLength": 8,
"type": "string",
"example": "123456"
}

View File

@@ -16,7 +16,7 @@
}
],
"requestBody": {
"description": "Verififcation Payload",
"description": "Verification Payload",
"required": true,
"content": {
"application/json": {
@@ -25,7 +25,7 @@
"properties": {
"code": {
"minLength": 6,
"maxLength": 6,
"maxLength": 8,
"type": "string",
"example": "123456"
}