mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-14 15:00:16 +02:00
Discord Auth Token caching implemented to work around rate limits.
Addresses #146
This commit is contained in:
parent
b43902c178
commit
604c08b3b7
@ -3,6 +3,7 @@ import json
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
from services.models import DiscordAuthToken
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -13,15 +14,15 @@ DISCORD_URL = "https://discordapp.com/api"
|
|||||||
class DiscordAPIManager:
|
class DiscordAPIManager:
|
||||||
|
|
||||||
def __init__(self, server_id, email, password):
|
def __init__(self, server_id, email, password):
|
||||||
data = {
|
# data = {
|
||||||
"email" : email,
|
# "email" : email,
|
||||||
"password": password,
|
# "password": password,
|
||||||
}
|
# }
|
||||||
custom_headers = {'content-type':'application/json'}
|
# custom_headers = {'content-type':'application/json'}
|
||||||
path = DISCORD_URL + "/auth/login"
|
# path = DISCORD_URL + "/auth/login"
|
||||||
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
# r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
r.raise_for_status()
|
# r.raise_for_status()
|
||||||
self.token = r.json()['token']
|
self.token = DiscordAPIManager.get_token_by_user(email, password)
|
||||||
self.email = email
|
self.email = email
|
||||||
self.password = password
|
self.password = password
|
||||||
self.server_id = server_id
|
self.server_id = server_id
|
||||||
@ -36,6 +37,18 @@ class DiscordAPIManager:
|
|||||||
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def validate_token(token):
|
||||||
|
custom_headers = {'accept': 'application/json', 'authorization': token}
|
||||||
|
path = DISCORD_URL + "/users/@me"
|
||||||
|
r = requests.get(path, headers=custom_headers)
|
||||||
|
if r.status_code == 200:
|
||||||
|
logger.debug("Token starting with %s still valid." % token[0:5])
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logger.debug("Token starting with %s vailed validation with status code %s" % (token[0:5], r.status_code))
|
||||||
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_auth_token():
|
def get_auth_token():
|
||||||
data = {
|
data = {
|
||||||
@ -227,6 +240,16 @@ class DiscordAPIManager:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_token_by_user(email, password):
|
def get_token_by_user(email, password):
|
||||||
|
if DiscordAuthToken.objects.filter(email=email).exists():
|
||||||
|
logger.debug("Discord auth token cached for supplied email starting with %s" % email[0:3])
|
||||||
|
auth = DiscordAuthToken.objects.get(email=email)
|
||||||
|
if DiscordAPIManager.validate_token(auth.token):
|
||||||
|
logger.debug("Token still valid. Returning token starting with %s" % auth.token[0:5])
|
||||||
|
return auth.token
|
||||||
|
else:
|
||||||
|
logger.debug("Token has expired. Deleting.")
|
||||||
|
auth.delete()
|
||||||
|
logger.debug("Generating auth token for email starting with %s and password of length %s" % (email[0:3], len(password)))
|
||||||
data = {
|
data = {
|
||||||
"email" : email,
|
"email" : email,
|
||||||
"password": password,
|
"password": password,
|
||||||
@ -236,7 +259,11 @@ class DiscordAPIManager:
|
|||||||
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||||
logger.debug("Received status code %s after generating auth token for custom user." % r.status_code)
|
logger.debug("Received status code %s after generating auth token for custom user." % r.status_code)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
return r.json()['token']
|
token = r.json()['token']
|
||||||
|
auth = DiscordAuthToken(email=email, token=token)
|
||||||
|
auth.save()
|
||||||
|
logger.debug("Created cached token for email starting with %s" % email[0:3])
|
||||||
|
return token
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_user_profile(email, password):
|
def get_user_profile(email, password):
|
||||||
|
@ -30,3 +30,9 @@ class UserTSgroup(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.user.name
|
return self.user.name
|
||||||
|
|
||||||
|
class DiscordAuthToken(models.Model):
|
||||||
|
email = models.CharField(max_length=254, primary_key=True)
|
||||||
|
token = models.CharField(max_length=254)
|
||||||
|
def __str__(self):
|
||||||
|
return self.email
|
||||||
|
Loading…
x
Reference in New Issue
Block a user