2017-03-25 08:52:56 +01:00
|
|
|
from flask import Blueprint, request, redirect, jsonify, abort, session, url_for, render_template
|
2017-03-24 09:22:03 +01:00
|
|
|
from requests_oauthlib import OAuth2Session
|
|
|
|
from config import config
|
2017-03-25 08:52:56 +01:00
|
|
|
from titanembeds.decorators import discord_users_only
|
2017-03-24 09:22:03 +01:00
|
|
|
|
|
|
|
user = Blueprint("user", __name__)
|
|
|
|
redirect_url = config['app-base-url'] + "/user/callback"
|
|
|
|
authorize_url = "https://discordapp.com/api/oauth2/authorize"
|
|
|
|
token_url = "https://discordapp.com/api/oauth2/token"
|
|
|
|
avatar_base_url = "https://cdn.discordapp.com/avatars/"
|
2017-03-25 08:52:56 +01:00
|
|
|
guild_icon_url = "https://cdn.discordapp.com/icons/"
|
2017-03-24 09:22:03 +01:00
|
|
|
|
2017-03-25 08:52:56 +01:00
|
|
|
def make_authenticated_session(token=None, state=None, scope=None):
|
2017-03-24 09:22:03 +01:00
|
|
|
return OAuth2Session(
|
|
|
|
client_id=config['client-id'],
|
|
|
|
token=token,
|
|
|
|
state=state,
|
|
|
|
scope=scope,
|
2017-03-25 08:52:56 +01:00
|
|
|
redirect_uri=request.url_root + "user/callback",
|
2017-03-24 09:22:03 +01:00
|
|
|
)
|
|
|
|
|
2017-03-25 08:52:56 +01:00
|
|
|
def discordrest_from_user(endpoint):
|
|
|
|
token = session['user_keys']
|
|
|
|
discord = make_authenticated_session(token=token)
|
|
|
|
req = discord.get("https://discordapp.com/api/v6{}".format(endpoint))
|
|
|
|
return req
|
|
|
|
|
|
|
|
def get_current_authenticated_user():
|
|
|
|
req = discordrest_from_user("/users/@me")
|
2017-03-24 09:22:03 +01:00
|
|
|
if req.status_code != 200:
|
|
|
|
abort(req.status_code)
|
|
|
|
user = req.json()
|
|
|
|
return user
|
|
|
|
|
2017-03-25 08:52:56 +01:00
|
|
|
def user_has_permission(permission, index):
|
|
|
|
return bool((int(permission) >> index) & 1)
|
|
|
|
|
|
|
|
def get_user_guilds():
|
|
|
|
req = discordrest_from_user("/users/@me/guilds")
|
|
|
|
return req
|
|
|
|
|
|
|
|
def get_user_managed_servers():
|
|
|
|
guilds = get_user_guilds().json()
|
|
|
|
filtered = []
|
|
|
|
for guild in guilds:
|
|
|
|
permission = guild['permissions'] # Manage Server, Ban Members, Kick Members
|
|
|
|
if guild['owner'] or user_has_permission(permission, 5) or user_has_permission(permission, 2) or user_has_permission(permission, 1):
|
|
|
|
filtered.append(guild)
|
|
|
|
return filtered
|
|
|
|
|
|
|
|
def generate_avatar_url(id, av):
|
|
|
|
return avatar_base_url + str(id) + '/' + str(av) + '.jpg'
|
|
|
|
|
|
|
|
def generate_guild_icon_url(id, hash):
|
|
|
|
return guild_icon_url + str(id) + "/" + str(hash) + ".jpg"
|
|
|
|
|
2017-03-24 09:22:03 +01:00
|
|
|
@user.route("/login_authenticated", methods=["GET"])
|
|
|
|
def login_authenticated():
|
2017-03-25 08:52:56 +01:00
|
|
|
session["redirect"] = request.args.get("redirect")
|
2017-03-24 09:22:03 +01:00
|
|
|
scope = ['identify', 'guilds', 'guilds.join']
|
2017-03-25 08:52:56 +01:00
|
|
|
discord = make_authenticated_session(scope=scope)
|
2017-03-24 09:22:03 +01:00
|
|
|
authorization_url, state = discord.authorization_url(
|
|
|
|
authorize_url,
|
|
|
|
access_type="offline"
|
|
|
|
)
|
|
|
|
session['oauth2_state'] = state
|
|
|
|
return redirect(authorization_url)
|
|
|
|
|
|
|
|
@user.route('/callback', methods=["GET"])
|
|
|
|
def callback():
|
|
|
|
state = session.get('oauth2_state')
|
|
|
|
if not state or request.values.get('error'):
|
2017-03-25 08:52:56 +01:00
|
|
|
return redirect(url_for('user.logout'))
|
|
|
|
discord = make_authenticated_session(state=state)
|
2017-03-24 09:22:03 +01:00
|
|
|
discord_token = discord.fetch_token(
|
|
|
|
token_url,
|
|
|
|
client_secret=config['client-secret'],
|
|
|
|
authorization_response=request.url)
|
|
|
|
if not discord_token:
|
2017-03-25 08:52:56 +01:00
|
|
|
return redirect(url_for('user.logout'))
|
|
|
|
session['user_keys'] = discord_token
|
|
|
|
session['unauthenticated'] = False
|
|
|
|
user = get_current_authenticated_user()
|
|
|
|
session['user_id'] = user['id']
|
|
|
|
session['username'] = user['username']
|
|
|
|
session['avatar'] = generate_avatar_url(user['id'], user['avatar'])
|
|
|
|
if session["redirect"]:
|
|
|
|
return redirect(session["redirect"])
|
|
|
|
return redirect(url_for("user.dashboard"))
|
2017-03-24 09:22:03 +01:00
|
|
|
|
|
|
|
@user.route('/logout', methods=["GET"])
|
|
|
|
def logout():
|
|
|
|
session.clear()
|
2017-03-25 08:52:56 +01:00
|
|
|
return redirect(url_for("index"))
|
|
|
|
|
|
|
|
@user.route("/dashboard")
|
|
|
|
@discord_users_only()
|
|
|
|
def dashboard():
|
|
|
|
return render_template("dashboard.html.jinja2", servers=get_user_managed_servers(), icon_generate=generate_guild_icon_url)
|
|
|
|
|
|
|
|
@user.route("/administrate_guild/<guild_id>")
|
|
|
|
@discord_users_only()
|
|
|
|
def administrate_guild(guild_id):
|
|
|
|
return str(guild_id)
|
2017-03-24 09:22:03 +01:00
|
|
|
|
|
|
|
@user.route('/me')
|
2017-03-25 08:52:56 +01:00
|
|
|
@discord_users_only()
|
2017-03-24 09:22:03 +01:00
|
|
|
def me():
|
2017-03-25 08:52:56 +01:00
|
|
|
return jsonify(user=get_current_authenticated_user())
|