diff --git a/flask_discord/__init__.py b/flask_discord/__init__.py index e69de29..3f1050e 100644 --- a/flask_discord/__init__.py +++ b/flask_discord/__init__.py @@ -0,0 +1,6 @@ +from .client import DiscordOAuth2Session + + +__all__ = [ + "DiscordOAuth2Session", +] diff --git a/flask_discord/client.py b/flask_discord/client.py new file mode 100644 index 0000000..75fc2cf --- /dev/null +++ b/flask_discord/client.py @@ -0,0 +1,49 @@ +from . import configs +from requests_oauthlib import OAuth2Session + +from flask import request, session, redirect + + + +class DiscordOAuth2Session(object): + + def __init__(self, client_id, client_secret, redirect_uri): + self.client_id = client_id + self.client_secret = client_secret + self.redirect_uri = redirect_uri + + @staticmethod + def __token_updater(token): + session["oauth2_token"] = token + + def __make_session(self, token=None, state=None, scope=None) + return OAuth2Session( + client_id=self.client_id, + token=token, + state=state, + scope=scope, + redirect_uri=self.redirect_uri, + auto_refresh_kwargs={ + 'client_id': self.client_id, + 'client_secret': self.client_secret, + }, + auto_refresh_url=configs.TOKEN_URL, + token_updater=self.__token_updater) + + def make_session(self): + scope = request.args.get("scope", configs.DEFAULT_SCOPES).split() + discord_session = self.__make_session(scope=scope) + authorization_url, state = discord_session.authorization_url(configs.AUTHORIZATION_BASE_URL) + session["oauth2_state"] = state + return redirect(authorization_url) + + def callback(self): + if request.values.get("error"): + return request.values["error"] + discord = self.__make_session(state=session.get("oauth2_state")) + token = discord.fetch_token( + configs.TOKEN_URL, + client_secret=self.client_secret, + authorization_response=request.url + ) + session["oauth2_token"] = token diff --git a/flask_discord/configs.py b/flask_discord/configs.py new file mode 100644 index 0000000..75eddec --- /dev/null +++ b/flask_discord/configs.py @@ -0,0 +1,7 @@ +API_BASE_URL = "https://discordapp.com/api" + +AUTHORIZATION_BASE_URL = API_BASE_URL + "/oauth2/authorize" +TOKEN_URL = API_BASE_URL + "/oauth2/token" + + +DEFAULT_SCOPES = "identify email connections guilds guilds.join" diff --git a/flask_discord/session.py b/flask_discord/session.py new file mode 100644 index 0000000..9a1b5d5 --- /dev/null +++ b/flask_discord/session.py @@ -0,0 +1,46 @@ +from . import configs +from requests_oauthlib import OAuth2Session + +from flask import current_app, request, session, sessions, redirect + + +class Session(dict, sessions.SessionMixin): + + def to_discord(self, scope=str()): + scope = (request.args.get("scope") or scope).split() + discord_session = OAuth2Session() + + def __init__(self, client_id, client_secret, redirect_uri, token_updater=None): + auto_refresh_kwargs = { + "client_id": client_id, + "client_secret": client_secret, + } + super().__init__( + client_id=client_id, redirect_uri=redirect_uri, + auto_refresh_kwargs=auto_refresh_kwargs, + auto_refresh_url=configs.TOKEN_URL, token_updater=token_updater + ) + + def create_session(self, scope=None): + self.scope = scope + authorization_url, state = self.authorization_url(configs.AUTHORIZATION_BASE_URL) + session["oauth2_state"] = state + return redirect(authorization_url) + + +class MySessionInterface(sessions.SessionInterface): + + def __init__(self, client_id, client_secret, redirect_uri, token_updater=None): + pass + + def open_session(self, app, request): + pass + + def save_session(self, app, session, response): + + + +class DiscordOAuth2Session(object): + + def __init__(self, app): + app.session_interface = Session()