Skip to content

Commit 108ef3c

Browse files
committed
Fixed password reset service
1 parent 60074f6 commit 108ef3c

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

selfservice/blueprints/change.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"""
55

66
from flask import Blueprint, render_template, request, redirect, flash
7-
from selfservice.utilities.reset import passwd_change, PasswordChangeFailed
7+
from selfservice.utilities.reset import passwd_change, PasswordChangeFailed, PasswordPolicyViolation, \
8+
CurrentPasswordInvalid
89
from selfservice import version
910

1011
change_bp = Blueprint("change", __name__)
@@ -24,14 +25,15 @@ def change():
2425
verify = request.form.get("verify")
2526

2627
if new_pw == verify:
27-
if len(new_pw) >= 12:
28-
try:
29-
passwd_change(username, old_pw, new_pw)
30-
return render_template("success.html", reset=True, version=version)
31-
except PasswordChangeFailed:
32-
flash("Incorrect password, please try again.")
33-
else:
34-
flash("Your password does not meet the requirements below.")
28+
try:
29+
passwd_change(username, old_pw, new_pw)
30+
return render_template("success.html", reset=True, version=version)
31+
except CurrentPasswordInvalid:
32+
flash("Your current password is incorrect, please try again.")
33+
except PasswordPolicyViolation as e:
34+
flash("Your new password does not match the requirements:", e.message)
35+
except PasswordChangeFailed:
36+
flash("An unknown error occurred.")
3537
else:
3638
flash("Whoops, those passwords didn't match!")
3739

selfservice/utilities/reset.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ class PasswordChangeFailed(Exception):
2929

3030
pass
3131

32+
class CurrentPasswordInvalid(Exception):
33+
"""
34+
Error raised when the current password is invalid.
35+
"""
36+
pass
37+
38+
class PasswordPolicyViolation(Exception):
39+
"""
40+
Error raised when the new password doesn't meet the password policy
41+
"""
42+
def __init__(self, message):
43+
self.message = message
44+
45+
3246

3347
def generate_token(session):
3448
"""
@@ -138,10 +152,22 @@ def passwd_change(username, old_pw, new_pw):
138152
# Find FreeIPA server
139153
ldap_srvs = srvlookup.lookup("ldap", "tcp", "csh.rit.edu")
140154
ldap_uri = ldap_srvs[0].hostname
155+
password_url = f"https://{ldap_uri}/ipa/session/change_password"
156+
headers = {
157+
"Referer": password_url,
158+
"Content-Type": "application/x-www-form-urlencoded",
159+
"Accept": "text/plain",
160+
}
141161
change = requests.post(
142-
f"https://{ldap_uri}/ipa/session/change_password",
162+
password_url,
163+
headers=headers,
143164
data={"user": username, "old_password": old_pw, "new_password": new_pw},
144165
timeout=30,
145166
)
146-
if change.headers.get("X-IPA-Pwchange-Result") == "invalid-password":
167+
pwchange_result = change.headers.get("X-IPA-Pwchange-Result")
168+
if pwchange_result == "invalid-password":
169+
raise CurrentPasswordInvalid
170+
if pwchange_result == "policy-error":
171+
raise PasswordPolicyViolation(change.headers.get("X-IPA-Pwchange-Policy-Error"))
172+
if pwchange_result != "ok" or change.status_code != 200:
147173
raise PasswordChangeFailed

0 commit comments

Comments
 (0)