@@ -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
3347def 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