From d8d741ab28e2c71341793963d5734a6c331ca3e8 Mon Sep 17 00:00:00 2001 From: Jonas Lochmann Date: Mon, 4 Nov 2019 00:00:00 +0000 Subject: [PATCH] Make mail code verification less strict --- src/function/authentication/login-by-mail.ts | 4 ++-- src/util/random-words.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/function/authentication/login-by-mail.ts b/src/function/authentication/login-by-mail.ts index 62c7938..1adc917 100644 --- a/src/function/authentication/login-by-mail.ts +++ b/src/function/authentication/login-by-mail.ts @@ -18,7 +18,7 @@ import { Forbidden, Gone, InternalServerError, TooManyRequests } from 'http-errors' import { Database } from '../../database' import { sendAuthenticationMail } from '../../util/mail' -import { randomWords } from '../../util/random-words' +import { areWordSequencesEqual, randomWords } from '../../util/random-words' import { checkMailSendLimit } from '../../util/ratelimit-authmail' import { generateAuthToken } from '../../util/token' import { createAuthTokenByMailAddress } from './index' @@ -78,7 +78,7 @@ export const signInByMailCode = async ({ mailLoginToken, receivedCode, database } } - if (entry.receivedCode !== receivedCode) { + if (!areWordSequencesEqual(entry.receivedCode, receivedCode)) { entry.remainingAttempts-- await entry.save({ transaction }) diff --git a/src/util/random-words.ts b/src/util/random-words.ts index 8a8fbc1..01aac25 100644 --- a/src/util/random-words.ts +++ b/src/util/random-words.ts @@ -31,3 +31,13 @@ export const randomWords = (numberOfWords: number) => ( .map((item) => randomWord()) .join(' ') ) + +const preprocessStringForComparing = (input: string) => ( + input + .replace(/ |\*/g, '') + .toLowerCase() +) + +export const areWordSequencesEqual = (a: string, b: string) => ( + preprocessStringForComparing(a) === preprocessStringForComparing(b) +)