mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-11 13:30:17 +02:00
Allows automatic update of Discord avatar to EVE avatar (#450)
* Automatic rejection of old API IDs * Added API ID fudge factor * Added toggle-able options for api key rejection * Clarified ValidationError message * Allows automatic update of Discord avatar to EVE avatar
This commit is contained in:
parent
f6a177295d
commit
cce4361eeb
@ -23,6 +23,7 @@ class FleetFormatterForm(forms.Form):
|
|||||||
class DiscordForm(forms.Form):
|
class DiscordForm(forms.Form):
|
||||||
email = forms.CharField(label="Email Address", required=True)
|
email = forms.CharField(label="Email Address", required=True)
|
||||||
password = forms.CharField(label="Password", required=True, widget=forms.PasswordInput)
|
password = forms.CharField(label="Password", required=True, widget=forms.PasswordInput)
|
||||||
|
update_avatar = forms.BooleanField(label="Update Avatar", required=False, initial=True)
|
||||||
|
|
||||||
class ServicePasswordForm(forms.Form):
|
class ServicePasswordForm(forms.Form):
|
||||||
password = forms.CharField(label="Password", required=True)
|
password = forms.CharField(label="Password", required=True)
|
||||||
|
@ -3,6 +3,8 @@ import json
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
import urllib
|
||||||
|
import base64
|
||||||
from services.models import DiscordAuthToken
|
from services.models import DiscordAuthToken
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@ -10,6 +12,7 @@ import logging
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
DISCORD_URL = "https://discordapp.com/api"
|
DISCORD_URL = "https://discordapp.com/api"
|
||||||
|
EVE_IMAGE_SERVER = "https://image.eveonline.com"
|
||||||
|
|
||||||
class DiscordAPIManager:
|
class DiscordAPIManager:
|
||||||
|
|
||||||
@ -260,8 +263,8 @@ class DiscordAPIManager:
|
|||||||
return r.json()
|
return r.json()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_user_profile(email, password):
|
def get_user_profile(email, password, user):
|
||||||
token = DiscordAPIManager.get_token_by_user(email, password)
|
token = DiscordAPIManager.get_token_by_user(email, password, user)
|
||||||
custom_headers = {'accept': 'application/json', 'authorization': token}
|
custom_headers = {'accept': 'application/json', 'authorization': token}
|
||||||
path = DISCORD_URL + "/users/@me"
|
path = DISCORD_URL + "/users/@me"
|
||||||
r = requests.get(path, headers=custom_headers)
|
r = requests.get(path, headers=custom_headers)
|
||||||
@ -270,8 +273,8 @@ class DiscordAPIManager:
|
|||||||
return r.json()
|
return r.json()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def set_user_password(email, current_password, new_password):
|
def set_user_password(email, current_password, new_password, user):
|
||||||
profile = DiscordAPIManager.get_user_profile(email, current_password)
|
profile = DiscordAPIManager.get_user_profile(email, current_password, user)
|
||||||
avatar = profile['avatar']
|
avatar = profile['avatar']
|
||||||
username = profile['username']
|
username = profile['username']
|
||||||
data = {
|
data = {
|
||||||
@ -287,6 +290,23 @@ class DiscordAPIManager:
|
|||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
return r.json()
|
return r.json()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def set_user_avatar(email, current_password, user, avatar_url):
|
||||||
|
profile = DiscordAPIManager.get_user_profile(email, current_password, user)
|
||||||
|
avatar = "data:image/jpeg;base64," + base64.b64encode(urllib.urlopen(avatar_url).read())
|
||||||
|
username = profile['username']
|
||||||
|
data = {
|
||||||
|
'avatar': avatar,
|
||||||
|
'username': username,
|
||||||
|
'password': current_password,
|
||||||
|
'email': email
|
||||||
|
}
|
||||||
|
path = DISCORD_URL + "/users/@me"
|
||||||
|
custom_headers = {'content-type':'application/json', 'authorization': DiscordAPIManager.get_token_by_user(email, current_password, user)}
|
||||||
|
r = requests.patch(path, headers=custom_headers, data=json.dumps(data))
|
||||||
|
r.raise_for_status()
|
||||||
|
return r.json()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def destroy_user(email, current_password):
|
def destroy_user(email, current_password):
|
||||||
data = {
|
data = {
|
||||||
@ -372,14 +392,24 @@ class DiscordManager:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_user_password(email, current_password):
|
def update_user_password(email, current_password, user):
|
||||||
new_password = DiscordManager.__generate_random_pass()
|
new_password = DiscordManager.__generate_random_pass()
|
||||||
try:
|
try:
|
||||||
profile = DiscordAPIManager.set_user_password(email, current_password, new_password)
|
profile = DiscordAPIManager.set_user_password(email, current_password, new_password, user)
|
||||||
return new_password
|
return new_password
|
||||||
except:
|
except:
|
||||||
return current_password
|
return current_password
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def update_user_avatar(email, password, user, char_id):
|
||||||
|
try:
|
||||||
|
char_url = EVE_IMAGE_SERVER + "/character/" + str(char_id) + "_256.jpg"
|
||||||
|
logger.debug("Character image URL for %s: %s" % (user, char_url))
|
||||||
|
DiscordAPIManager.set_user_avatar(email, password, user, char_url)
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_user(email, password, user):
|
def add_user(email, password, user):
|
||||||
try:
|
try:
|
||||||
@ -390,6 +420,7 @@ class DiscordManager:
|
|||||||
logger.debug("Got profile for user: %s" % profile)
|
logger.debug("Got profile for user: %s" % profile)
|
||||||
user_id = profile['id']
|
user_id = profile['id']
|
||||||
logger.debug("Determined user id: %s" % user_id)
|
logger.debug("Determined user id: %s" % user_id)
|
||||||
|
|
||||||
if server_api.check_if_user_banned(user_id):
|
if server_api.check_if_user_banned(user_id):
|
||||||
logger.debug("User is currently banned. Unbanning %s" % user_id)
|
logger.debug("User is currently banned. Unbanning %s" % user_id)
|
||||||
server_api.unban_user(user_id)
|
server_api.unban_user(user_id)
|
||||||
|
@ -465,6 +465,8 @@ def activate_discord(request):
|
|||||||
logger.debug("Form contains email address beginning with %s" % email[0:3])
|
logger.debug("Form contains email address beginning with %s" % email[0:3])
|
||||||
password = form.cleaned_data['password']
|
password = form.cleaned_data['password']
|
||||||
logger.debug("Form contains password of length %s" % len(password))
|
logger.debug("Form contains password of length %s" % len(password))
|
||||||
|
update_avatar = form.cleaned_data['update_avatar']
|
||||||
|
logger.debug("Form contains update_avatar set to %r" % bool(update_avatar))
|
||||||
try:
|
try:
|
||||||
user_id = DiscordManager.add_user(email, password, request.user)
|
user_id = DiscordManager.add_user(email, password, request.user)
|
||||||
logger.debug("Received discord uid %s" % user_id)
|
logger.debug("Received discord uid %s" % user_id)
|
||||||
@ -475,6 +477,14 @@ def activate_discord(request):
|
|||||||
logger.debug("Updated discord groups for user %s." % request.user)
|
logger.debug("Updated discord groups for user %s." % request.user)
|
||||||
success = True
|
success = True
|
||||||
logger.info("Succesfully activated discord for user %s" % request.user)
|
logger.info("Succesfully activated discord for user %s" % request.user)
|
||||||
|
if (update_avatar):
|
||||||
|
authinfo = AuthServicesInfoManager.get_auth_service_info(request.user)
|
||||||
|
char_id = authinfo.main_char_id
|
||||||
|
avatar_updated = DiscordManager.update_user_avatar(email, password, request.user, char_id)
|
||||||
|
if (avatar_updated):
|
||||||
|
logger.debug("Updated user %s discord avatar." % request.user)
|
||||||
|
else:
|
||||||
|
logger.debug("Could not set user %s discord avatar." %request.user)
|
||||||
return HttpResponseRedirect("/services/")
|
return HttpResponseRedirect("/services/")
|
||||||
except:
|
except:
|
||||||
logger.exception("An unhandled exception has occured.")
|
logger.exception("An unhandled exception has occured.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user