-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual_password_reset.py
57 lines (41 loc) · 1.42 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
53
54
55
56
57
from flask_security.utils import encrypt_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()
try:
input = raw_input
except NameError:
pass
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 = encrypt_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())