From f6ef741e63c7c7c5234a579d03b44595c1e50822 Mon Sep 17 00:00:00 2001 From: Adarnof Date: Tue, 1 Dec 2015 06:55:33 +0000 Subject: [PATCH] Added password change function as reset action. --- alliance_auth/urls.py | 2 +- services/managers/discord_manager.py | 48 ++++++++++++++++++++++++ services/views.py | 13 ++----- stock/templates/registered/services.html | 8 ++++ 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/alliance_auth/urls.py b/alliance_auth/urls.py index 9fb00a5d..78f3fbd4 100755 --- a/alliance_auth/urls.py +++ b/alliance_auth/urls.py @@ -128,7 +128,7 @@ urlpatterns = patterns('', # Discord Service Control url(r'^activate_discord/$', 'services.views.activate_discord', name='auth_activate_discord'), url(r'^deactivate_discord/$', 'services.views.deactivate_discord', name='auth_deactivate_discord'), -# url(r'^reset_discord/$', 'services.views.reset_discord', name='auth_reset_discord'), + url(r'^reset_discord/$', 'services.views.reset_discord', name='auth_reset_discord'), # Tools url(r'^tool/fleet_formatter_tool/$', 'services.views.fleet_formatter_view', diff --git a/services/managers/discord_manager.py b/services/managers/discord_manager.py index a6405284..997c53c0 100644 --- a/services/managers/discord_manager.py +++ b/services/managers/discord_manager.py @@ -206,6 +206,45 @@ class DiscordAPIManager: return role['id'] raise KeyError('Group not found on server: ' + group_name) + @staticmethod + def get_token_by_user(email, password): + data = { + "email" : email, + "password": password, + } + custom_headers = {'content-type':'application/json'} + path = DISCORD_URL + "/auth/login" + r = requests.post(path, headers=custom_headers, data=json.dumps(data)) + r.raise_for_status() + return r.json()['token'] + + @staticmethod + def get_user_profile(email, password): + token = DiscordAPIManager.get_token_by_user(email, password) + custom_headers = {'accept': 'application/json', 'authorization': token} + path = DISCORD_URL + "/users/@me" + r = requests.get(path, headers=custom_headers) + r.raise_for_status() + return r.json() + + @staticmethod + def set_user_password(email, current_password, new_password): + profile = DiscordAPIManager.get_user_profile(email, current_password) + avatar = profile['avatar'] + username = profile['username'] + data = { + 'avatar': avatar, + 'username': username, + 'password': current_password, + 'new_password': new_password, + 'email': email, + } + path = DISCORD_URL + "/users/@me" + custom_headers = {'content-type':'application/json', 'authorization': DiscordAPIManager.get_token_by_user(email, current_password)} + r = requests.patch(path, headers=custom_headers, data=json.dumps(data)) + r.raise_for_status() + return r.json() + class DiscordManager: def __init__(self): pass @@ -281,3 +320,12 @@ class DiscordManager: return True except: return False + + @staticmethod + def update_user_password(email, current_password): + new_password = DiscordManager.__generate_random_pass() + try: + profile = DiscordAPIManager.set_user_password(email, current_password, new_password) + return new_password + except: + return current_password diff --git a/services/views.py b/services/views.py index 100f05b5..08658757 100755 --- a/services/views.py +++ b/services/views.py @@ -348,14 +348,9 @@ def deactivate_discord(request): @user_passes_test(service_blue_alliance_test) def reset_discord(request): authinfo = AuthServicesInfoManager.get_auth_service_info(request.user) - result = DiscordManager.delete_user(authinfo.discord_username) + result = DiscordManager.update_user_password(request.user.email, authinfo.discord_password) if result: - # ensures succesful deletion - AuthServicesInfoManager.update_user_discord_info("", "", request.user) - new_result = DiscordManager.add_user(authinfo.discord_username, request.user.email) - if new_result: - # ensures succesful creation - AuthServicesInfoManager.update_user_discord_info(new_result[0], new_result[1], request.user) - update_discord_groups(request.user) - return HttpResponseRedirect("/services/") + AuthServicesInfoManager.update_user_discord_info(authinfo.discord_username, result, request.user) + update_discord_groups(request.user) + return HttpResponseRedirect("/services/") return HttpResponseRedirect("/services/") diff --git a/stock/templates/registered/services.html b/stock/templates/registered/services.html index 3977eb42..0f65ed09 100755 --- a/stock/templates/registered/services.html +++ b/stock/templates/registered/services.html @@ -127,6 +127,10 @@ class="glyphicon glyphicon-ok"> {% else %} + + + @@ -294,6 +298,10 @@ class="glyphicon glyphicon-ok"> {% else %} + + +