Skip to content

Commit

Permalink
Merge pull request #13870 from Expensify/arosiclair-two-factor-valida…
Browse files Browse the repository at this point in the history
…tion

Add validation for two factor codes
  • Loading branch information
madmax330 committed Jan 3, 2023
2 parents 82f64b1 + 1ad0dc6 commit 466b027
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ const CONST = {
EMOJI_NAME: /:[\w+-]+:/g,
EMOJI_SUGGESTIONS: /:[a-zA-Z]{1,20}(\s[a-zA-Z]{0,20})?$/,
AFTER_FIRST_LINE_BREAK: /\n.*/g,
CODE_2FA: /^\d{6}$/,
},

PRONOUNS: {
Expand Down
9 changes: 9 additions & 0 deletions src/libs/ValidationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ function isValidPassword(password) {
return password.match(CONST.PASSWORD_COMPLEXITY_REGEX_STRING);
}

/**
* @param {String} code
* @returns {Boolean}
*/
function isValidTwoFactorCode(code) {
return Boolean(code.match(CONST.REGEX.CODE_2FA));
}

/**
* @param {String} input
* @returns {Boolean}
Expand Down Expand Up @@ -409,6 +417,7 @@ export {
isValidURL,
validateIdentity,
isValidPassword,
isValidTwoFactorCode,
isPositiveInteger,
isNumericWithSpecialChars,
isValidPaypalUsername,
Expand Down
19 changes: 14 additions & 5 deletions src/pages/signin/PasswordForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,40 @@ class PasswordForm extends React.Component {
* Check that all the form fields are valid, then trigger the submit callback
*/
validateAndSubmitForm() {
if (!this.state.password.trim() && this.props.account.requiresTwoFactorAuth && !this.state.twoFactorAuthCode.trim()) {
const password = this.state.password.trim();
const twoFactorCode = this.state.twoFactorAuthCode.trim();
const requiresTwoFactorAuth = this.props.account.requiresTwoFactorAuth;

if (!password && requiresTwoFactorAuth && !twoFactorCode) {
this.setState({formError: 'passwordForm.pleaseFillOutAllFields'});
return;
}

if (!this.state.password.trim()) {
if (!password) {
this.setState({formError: 'passwordForm.pleaseFillPassword'});
return;
}

if (!ValidationUtils.isValidPassword(this.state.password)) {
if (!ValidationUtils.isValidPassword(password)) {
this.setState({formError: 'passwordForm.error.incorrectPassword'});
return;
}

if (this.props.account.requiresTwoFactorAuth && !this.state.twoFactorAuthCode.trim()) {
if (requiresTwoFactorAuth && !twoFactorCode) {
this.setState({formError: 'passwordForm.pleaseFillTwoFactorAuth'});
return;
}

if (requiresTwoFactorAuth && !ValidationUtils.isValidTwoFactorCode(twoFactorCode)) {
this.setState({formError: 'passwordForm.error.incorrect2fa'});
return;
}

this.setState({
formError: null,
});

Session.signIn(this.state.password, this.state.twoFactorAuthCode);
Session.signIn(password, twoFactorCode);
}

render() {
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/ValidationUtilsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const ValidationUtils = require('../../src/libs/ValidationUtils');

describe('ValidationUtils', () => {
describe('isValidTwoFactorCode', () => {
test('numeric two factor code', () => {
expect(ValidationUtils.isValidTwoFactorCode('123456')).toBe(true);
});

test('numeric two factor code with leading zeroes', () => {
expect(ValidationUtils.isValidTwoFactorCode('000001')).toBe(true);
});

test('alphanumeric two factor code', () => {
expect(ValidationUtils.isValidTwoFactorCode('abc123')).toBe(false);
});

test('special characters two factor code', () => {
expect(ValidationUtils.isValidTwoFactorCode('!@#$%^')).toBe(false);
});

test('partial special characters two factor code', () => {
expect(ValidationUtils.isValidTwoFactorCode('123$%^')).toBe(false);
});
});
});

0 comments on commit 466b027

Please sign in to comment.