Skip to content
Snippets Groups Projects
Commit 1728610e authored by MaximeOLIVA's avatar MaximeOLIVA
Browse files

change_password

parent e3908ab3
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -76,6 +76,17 @@ def tokenToEmail(token): ...@@ -76,6 +76,17 @@ def tokenToEmail(token):
return None return None
def tokenToPassword(token):
try:
email = tokenToEmail(token)
cursor = get_db().cursor()
cursor.execute("SELECT PASSWORD FROM USERS WHERE EMAIL=?;", [email])
password = cursor.fetchone()[0]
return password
except:
return None
def is_online(token): def is_online(token):
try: try:
cursor = get_db().cursor() cursor = get_db().cursor()
...@@ -103,26 +114,27 @@ def get_data_email(email): ...@@ -103,26 +114,27 @@ def get_data_email(email):
return None return None
def change_password(email, newpassword): def change_password(email, newpassword):
try: try:
sql = "UPDATE USERS SET password = 'newpassword1' WHERE email = 'cbdgsdsd11mail.com';" sql = "UPDATE USERS SET PASSWORD = ? WHERE EMAIL = ?;"
get_db().execute(sql) get_db().execute(sql, (newpassword, email))
get_db().commit() get_db().commit()
return True return True
except: except:
return False return False
def check_email_exists(email): def check_email_exists(email):
cursor = get_db().cursor() cursor = get_db().cursor()
cursor.execute("SELECT email FROM USERS WHERE email=?", (email,)) cursor.execute("SELECT email FROM USERS WHERE email=?", (email,))
result = cursor.fetchone() result = cursor.fetchone()
return True if result else False return True if result else False
def postMessage(email_sender, email_recipient, message): def postMessage(email_sender, email_recipient, message):
try: try:
get_db().execute("INSERT into MESSAGES (EMAIL_RECIPIENT, EMAIL_SENDER, MESSAGE) values(?, ?, ?)", [email_recipient, email_sender, message]) get_db().execute("INSERT into MESSAGES (EMAIL_RECIPIENT, EMAIL_SENDER, MESSAGE) values(?, ?, ?)", [email_recipient, email_sender, message])
get_db().commit() get_db().commit()
return True return True
except: except:
return False return False
\ No newline at end of file
...@@ -81,13 +81,21 @@ def sign_out(): ...@@ -81,13 +81,21 @@ def sign_out():
def change_password(): def change_password():
data = request.get_json() data = request.get_json()
if('token' in data and 'oldPassword' in data and 'newPassword' in data): if('token' in data and 'oldPassword' in data and 'newPassword' in data):
if database_helper.is_online(data['token']): if(len(data['oldPassword']) > 5 and len(data['newPassword']) > 5):
email = database_helper.tokenToEmail(data['token']) if database_helper.is_online(data['token']):
#check old password is good email = database_helper.tokenToEmail(data['token'])
if database_helper.change_password(email, data['newPassword']): password = database_helper.tokenToPassword(data['token'])
return "", 204 if(data['oldPassword'] == password):
if database_helper.change_password(email, data['newPassword']):
return "", 204
else:
return "", 400
else:
return "", 403
else:
return "", 401
else: else:
return "", 401 return "", 406
else: else:
return "", 400 return "", 400
...@@ -123,7 +131,7 @@ def get_user_data_email(token, email): ...@@ -123,7 +131,7 @@ def get_user_data_email(token, email):
@app.route("/account/post_message", methods = ['POST']) @app.route("/account/post_message", methods = ['POST'])
def post_message(): def post_message():
data = request.get_json() data = request.get_json()
if('token' in data if('token' in data
and 'message' in data and 'message' in data
and 'email_recipient' in data): and 'email_recipient' in data):
email_sender = database_helper.tokenToEmail(data['token']) email_sender = database_helper.tokenToEmail(data['token'])
...@@ -137,10 +145,10 @@ def post_message(): ...@@ -137,10 +145,10 @@ def post_message():
else: else:
return "", 404 return "", 404
else: else:
return "", 404 return "", 403
else: else:
return "", 401 return "", 400
def generate_token(): def generate_token():
characters = string.ascii_letters + string.digits characters = string.ascii_letters + string.digits
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment