-
Notifications
You must be signed in to change notification settings - Fork 11
/
manual_password_reset.py
52 lines (37 loc) · 1.34 KB
/
manual_password_reset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask_security.utils import hash_password
from mhn.auth.models import User
from mhn import mhn, db
import sys
from getpass import getpass
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='Reset a password for user')
parser.add_argument('-u', '--username', help='Username to reset')
parser.add_argument('-p', '--password', help='New password')
return parser.parse_args()
def main():
args = parse_args()
with mhn.test_request_context():
if not args.username:
email = input("Enter email address: ").strip()
else:
email = args.username
if not args.password:
password = getpass("Enter new password: ")
password2 = getpass("Enter new password (again): ")
if password != password2:
sys.stderr.write("Passwords didn't match, try again\n")
return 1
else:
password = args.password
user = User.query.filter_by(email=email).first()
if user:
print("user found, updating password")
user.password = hash_password(password)
db.session.add(user)
db.session.commit()
else:
sys.stderr.write("No user with that email address was found.\n")
return 0
if __name__ == "__main__":
sys.exit(main())