Skip to content

Commit 457a56e

Browse files
committed
Allow more advanced, custom password logic
1 parent d4c6f4e commit 457a56e

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

flask_user/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def hash_password(self, password):
236236
return passwords.hash_password(self, password)
237237

238238
def get_password(self, user):
239-
use_auth_class = True if self.db_adapter.UserAuthClass and hasattr(user, 'user_auth') else False
239+
use_auth_class = self.db_adapter.UserAuthClass and hasattr(user, 'user_auth')
240240
# Handle v0.5 backward compatibility
241241
if self.db_adapter.UserProfileClass:
242242
hashed_password = user.password
@@ -245,7 +245,7 @@ def get_password(self, user):
245245
return hashed_password
246246

247247
def update_password(self, user, hashed_password):
248-
use_auth_class = True if self.db_adapter.UserAuthClass and hasattr(user, 'user_auth') else False
248+
use_auth_class = self.db_adapter.UserAuthClass and hasattr(user, 'user_auth')
249249

250250
if use_auth_class:
251251
user.user_auth.password = hashed_password
@@ -261,6 +261,9 @@ def verify_password(self, password, user):
261261
verified = False
262262
hashed_password = self.get_password(user)
263263

264+
if not hashed_password:
265+
return False
266+
264267
try:
265268
verified = passwords.verify_password(self, password, hashed_password)
266269
except ValueError:

flask_user/forms.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ class ChangePasswordForm(Form):
8282
next = HiddenField()
8383
submit = SubmitField(_('Change password'))
8484

85+
def __init__(self, formdata=None, obj=None, prefix='', data=None, meta=None, user=None, **kw):
86+
self.user = user
87+
return super(ChangePasswordForm, self).__init__(
88+
formdata=formdata,
89+
obj=obj,
90+
prefix=prefix,
91+
data=data,
92+
meta=meta,
93+
**kw
94+
)
95+
8596
def validate(self):
8697
# Use feature config to remove unused form fields
8798
user_manager = current_app.user_manager
@@ -210,7 +221,7 @@ def validate(self):
210221
user, user_email = user_manager.find_user_by_email(self.email.data)
211222

212223
# Handle successful authentication
213-
if user and user.password and user_manager.verify_password(self.password.data, user):
224+
if user and user_manager.verify_password(self.password.data, user):
214225
return True # Successful authentication
215226

216227
# Handle unsuccessful authentication
@@ -308,6 +319,17 @@ class ResetPasswordForm(Form):
308319
next = HiddenField()
309320
submit = SubmitField(_('Change password'))
310321

322+
def __init__(self, formdata=None, obj=None, prefix='', data=None, meta=None, user=None, **kw):
323+
self.user = user
324+
return super(ResetPasswordForm, self).__init__(
325+
formdata=formdata,
326+
obj=obj,
327+
prefix=prefix,
328+
data=data,
329+
meta=meta,
330+
**kw
331+
)
332+
311333
def validate(self):
312334
# Use feature config to remove unused form fields
313335
user_manager = current_app.user_manager

flask_user/views.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def change_password():
8080
db_adapter = user_manager.db_adapter
8181

8282
# Initialize form
83-
form = user_manager.change_password_form(request.form)
83+
form = user_manager.change_password_form(request.form, user=current_user)
84+
8485
form.next.data = request.args.get('next', _endpoint_url(user_manager.after_change_password_endpoint)) # Place ?next query param in next form field
8586

8687
# Process valid POST
@@ -580,7 +581,7 @@ def reset_password(token):
580581
user_email.confirmed_at = datetime.utcnow()
581582

582583
# Initialize form
583-
form = user_manager.reset_password_form(request.form)
584+
form = user_manager.reset_password_form(request.form, user=user)
584585

585586
# Process valid POST
586587
if request.method=='POST' and form.validate():
@@ -590,9 +591,7 @@ def reset_password(token):
590591

591592
# Change password
592593
hashed_password = user_manager.hash_password(form.new_password.data)
593-
user_auth = user.user_auth if db_adapter.UserAuthClass and hasattr(user, 'user_auth') else user
594-
db_adapter.update_object(user_auth, password=hashed_password)
595-
db_adapter.commit()
594+
user_manager.update_password(user, hashed_password)
596595

597596
# Send 'password_changed' email
598597
if user_manager.enable_email and user_manager.send_password_changed_email:
@@ -733,5 +732,3 @@ def _endpoint_url(endpoint):
733732
if endpoint:
734733
url = url_for(endpoint)
735734
return url
736-
737-

0 commit comments

Comments
 (0)