mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-23 15:57:23 -04:00
Merge pull request #2068 from alphagov/2fa-input
Improve 2fa code input
This commit is contained in:
@@ -160,12 +160,16 @@ def password(label='Password'):
|
||||
Blacklist(message='Choose a password that’s harder to guess')])
|
||||
|
||||
|
||||
def sms_code():
|
||||
verify_code = '^\d{5}$'
|
||||
return StringField('Text message code',
|
||||
validators=[DataRequired(message='Can’t be empty'),
|
||||
Regexp(regex=verify_code,
|
||||
message='Code not found')])
|
||||
class SMSCode(StringField):
|
||||
validators = [
|
||||
DataRequired(message='Can’t be empty'),
|
||||
Regexp(regex='^\d+$', message='Numbers only'),
|
||||
Length(min=5, message='Not enough numbers'),
|
||||
Length(max=5, message='Too many numbers'),
|
||||
]
|
||||
|
||||
def __call__(self, **kwargs):
|
||||
return super().__call__(type='tel', pattern='[0-9]*', **kwargs)
|
||||
|
||||
|
||||
def organisation_type():
|
||||
@@ -315,12 +319,20 @@ class TwoFactorForm(StripWhitespaceForm):
|
||||
self.validate_code_func = validate_code_func
|
||||
super(TwoFactorForm, self).__init__(*args, **kwargs)
|
||||
|
||||
sms_code = sms_code()
|
||||
sms_code = SMSCode('Text message code')
|
||||
|
||||
def validate(self):
|
||||
|
||||
if not self.sms_code.validate(self):
|
||||
return False
|
||||
|
||||
is_valid, reason = self.validate_code_func(self.sms_code.data)
|
||||
|
||||
def validate_sms_code(self, field):
|
||||
is_valid, reason = self.validate_code_func(field.data)
|
||||
if not is_valid:
|
||||
raise ValidationError(reason)
|
||||
self.sms_code.errors.append(reason)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class EmailNotReceivedForm(StripWhitespaceForm):
|
||||
@@ -477,19 +489,6 @@ class ChangeMobileNumberForm(StripWhitespaceForm):
|
||||
mobile_number = international_phone_number()
|
||||
|
||||
|
||||
class ConfirmMobileNumberForm(StripWhitespaceForm):
|
||||
def __init__(self, validate_code_func, *args, **kwargs):
|
||||
self.validate_code_func = validate_code_func
|
||||
super(ConfirmMobileNumberForm, self).__init__(*args, **kwargs)
|
||||
|
||||
sms_code = sms_code()
|
||||
|
||||
def validate_sms_code(self, field):
|
||||
is_valid, msg = self.validate_code_func(field.data)
|
||||
if not is_valid:
|
||||
raise ValidationError(msg)
|
||||
|
||||
|
||||
class ChooseTimeForm(StripWhitespaceForm):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
@@ -18,8 +18,8 @@ from app.main.forms import (
|
||||
ChangeMobileNumberForm,
|
||||
ChangeNameForm,
|
||||
ChangePasswordForm,
|
||||
ConfirmMobileNumberForm,
|
||||
ConfirmPasswordForm,
|
||||
TwoFactorForm,
|
||||
)
|
||||
from app.utils import is_gov_user
|
||||
|
||||
@@ -169,7 +169,7 @@ def user_profile_mobile_number_confirm():
|
||||
if NEW_MOBILE_PASSWORD_CONFIRMED not in session:
|
||||
return redirect(url_for('.user_profile_mobile_number'))
|
||||
|
||||
form = ConfirmMobileNumberForm(_check_code)
|
||||
form = TwoFactorForm(_check_code)
|
||||
|
||||
if form.validate_on_submit():
|
||||
user = user_api_client.get_user(current_user.id)
|
||||
|
||||
Reference in New Issue
Block a user