Skip to content

Commit edc5caa

Browse files
egabanchoutnapischtim
authored andcommitted
feat: adapt for pluggable password validation
* Adapts password validation to use Flask-Security-Invenio's pluggable password validation. * NOTE: the default password validation is backwards compatible, meaning that it only checks for a minimum length of 6.
1 parent 86d8f17 commit edc5caa

2 files changed

Lines changed: 11 additions & 13 deletions

File tree

invenio_accounts/views/rest.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ def validate_domain_rest(email):
233233
raise ValidationError(_("The email domain is blocked."))
234234

235235

236+
def validate_password(password):
237+
"""Validate password using Flask-Security."""
238+
if msg := current_security._password_validator(password, True):
239+
raise ValidationError(msg)
240+
241+
236242
def _abort(message, field=None, status=None):
237243
if field:
238244
raise RESTValidationError([FieldError(field, message)])
@@ -343,9 +349,7 @@ class RegisterView(MethodView):
343349
"email": fields.Email(
344350
required=True, validate=[unique_user_email, validate_domain_rest]
345351
),
346-
"password": fields.String(
347-
required=True, validate=[validate.Length(min=6, max=128)]
348-
),
352+
"password": fields.String(required=True, validate=[validate_password]),
349353
}
350354

351355
def login_user(self, user):
@@ -427,9 +431,7 @@ class ResetPasswordView(MethodView):
427431

428432
post_args = {
429433
"token": fields.String(required=True),
430-
"password": fields.String(
431-
required=True, validate=[validate.Length(min=6, max=128)]
432-
),
434+
"password": fields.String(required=True, validate=[validate_password]),
433435
}
434436

435437
def get_user(self, token=None, **kwargs):
@@ -472,12 +474,8 @@ class ChangePasswordView(MethodView):
472474
decorators = [login_required]
473475

474476
post_args = {
475-
"password": fields.String(
476-
required=True, validate=[validate.Length(min=6, max=128)]
477-
),
478-
"new_password": fields.String(
479-
required=True, validate=[validate.Length(min=6, max=128)]
480-
),
477+
"password": fields.String(required=True, validate=[validate_password]),
478+
"new_password": fields.String(required=True, validate=[validate_password]),
481479
}
482480

483481
def verify_password(self, password=None, new_password=None, **kwargs):

tests/test_views_rest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_registration_view(api):
171171
assert_error_resp(
172172
res,
173173
(
174-
("password", "length"),
174+
("password", "password must be at least"),
175175
("email", "not a valid email"),
176176
),
177177
)

0 commit comments

Comments
 (0)