|
| 1 | +import logging |
| 2 | + |
| 3 | +import requests |
| 4 | +from flask_restful import Resource |
| 5 | +from flask import request, redirect |
| 6 | +from data.users import getUserByAuthID, getUserIdFromAuthID |
| 7 | +from utils.authentication import authenticate |
| 8 | +from data.discord import saveDiscordId |
| 9 | +from urllib.parse import quote |
| 10 | +from hashlib import sha256 |
| 11 | +from utils.aws import getDiscordClientID, getDiscordClientSecret, getRedirectDomain |
| 12 | + |
| 13 | +logger = logging.getLogger("Discord") |
| 14 | + |
| 15 | + |
| 16 | +def getRedirectUrl(): |
| 17 | + return getRedirectDomain() + "/discord/callback" |
| 18 | + |
| 19 | + |
| 20 | +def getUrlSafeRedirectUrl(): |
| 21 | + url = getRedirectUrl() |
| 22 | + return quote(url, safe='') |
| 23 | + |
| 24 | + |
| 25 | +class DiscordIntegration(Resource): |
| 26 | + PATH = '/discord' |
| 27 | + |
| 28 | + def get(self): |
| 29 | + userId = request.args.get('id') |
| 30 | + if userId is None: |
| 31 | + return {}, 400 |
| 32 | + |
| 33 | + state = sha256(str(userId).encode("ASCII")).hexdigest() |
| 34 | + urlEncodedCallback = getUrlSafeRedirectUrl() |
| 35 | + discordAuthorizationURL = f'https://discord.com/oauth2/authorize?response_type=code&client_id={getDiscordClientID()}&scope=identify&state={state}&redirect_uri={urlEncodedCallback}' |
| 36 | + return redirect(discordAuthorizationURL, code=302) |
| 37 | + |
| 38 | + def post(self): |
| 39 | + authenticationPayload = authenticate(request.headers) |
| 40 | + if authenticationPayload is None: |
| 41 | + return {"message": "Authorization Header Failure"}, 401 |
| 42 | + auth0_id = authenticationPayload['sub'] |
| 43 | + userId = getUserIdFromAuthID(auth0_id) |
| 44 | + |
| 45 | + state = request.args.get('state') |
| 46 | + code = request.args.get('code') |
| 47 | + if not state == sha256(str(userId).encode("ASCII")).hexdigest(): |
| 48 | + user = getUserByAuthID(auth0_id) |
| 49 | + logger.error("A Hacker is under a CSRF attack. user=%s", user) |
| 50 | + return {}, 400 |
| 51 | + if code is None: |
| 52 | + return {}, 400 |
| 53 | + |
| 54 | + payload = { |
| 55 | + 'client_id': getDiscordClientID(), |
| 56 | + 'client_secret': getDiscordClientSecret(), |
| 57 | + 'grant_type': 'authorization_code', |
| 58 | + 'code': code, |
| 59 | + 'redirect_uri': getRedirectUrl() |
| 60 | + } |
| 61 | + headers = { |
| 62 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 63 | + } |
| 64 | + response = requests.post('https://discord.com/api/oauth2/token', data=payload, headers=headers) |
| 65 | + discordData = response.json() |
| 66 | + if not response.status_code == 200: |
| 67 | + logger.error("Requesting Token from code Failure: UserId=%s", userId) |
| 68 | + return {}, 500 |
| 69 | + hackerAccessToken = discordData['access_token'] |
| 70 | + headers = { |
| 71 | + 'authorization': 'Bearer ' + hackerAccessToken |
| 72 | + } |
| 73 | + identityResponse = requests.get("https://discord.com/api/users/@me", headers=headers) |
| 74 | + if not identityResponse.status_code == 200: |
| 75 | + logger.error("Requesting Identity from Discord Failed: UserId=%s", userId) |
| 76 | + return {}, 500 |
| 77 | + userDiscordId = identityResponse.json()['id'] |
| 78 | + saveSuccessful = saveDiscordId(auth0Id=auth0_id, discordId=userDiscordId) |
| 79 | + if saveSuccessful: |
| 80 | + return {"Message": "Discord Integration Success"} |
| 81 | + return {}, 500 |
0 commit comments