From 1d2c85d3e83df4c7135dad1890d72709ad55ec6f Mon Sep 17 00:00:00 2001 From: thec0sm0s Date: Tue, 23 Jun 2020 20:46:26 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=94=20Add=20support=20to=20specify=20cust?= =?UTF-8?q?om=20parameters=20in=20authorization=20URL=20along=20with=20abi?= =?UTF-8?q?lity=20to=20determine=20authorization=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flask_discord/client.py | 18 +++++++++++++++++- flask_discord/configs.py | 5 +++++ requirements.txt | 5 ++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/flask_discord/client.py b/flask_discord/client.py index ec25d02..849168c 100644 --- a/flask_discord/client.py +++ b/flask_discord/client.py @@ -1,6 +1,7 @@ from . import configs, _http, models from flask import request, session, redirect +from oauthlib.common import add_params_to_uri class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient): @@ -16,7 +17,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient): """ - def create_session(self, scope: list = None): + def create_session(self, scope: list = None, prompt: bool = True, params: dict = None): """Primary method used to create OAuth2 session and redirect users for authorization code grant. @@ -25,6 +26,11 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient): scope : list, optional An optional list of valid `Discord OAuth2 Scopes `_. + prompt : bool, optional + Determines if the OAuth2 grant should be explicitly prompted and re-approved. Defaults to True. + Specify False for implicit grant which will skip the authorization screen and redirect to redirect URI. + params : dict, optional + An optional mapping of query parameters to supply to the authorization URL. Returns ------- @@ -33,9 +39,19 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient): """ scope = scope or request.args.get("scope", str()).split() or configs.DISCORD_OAUTH_DEFAULT_SCOPES + + if not prompt and set(scope) & set(configs.DISCORD_PASSTHROUGH_SCOPES): + raise ValueError("You should use explicit OAuth grant for passthrough scopes like bot.") + discord_session = self._make_session(scope=scope) authorization_url, state = discord_session.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL) session["DISCORD_OAUTH2_STATE"] = state + + prompt = "consent" if prompt else "none" + params = params or dict() + params.update(prompt=prompt) + authorization_url = add_params_to_uri(authorization_url, params) + return redirect(authorization_url) def callback(self): diff --git a/flask_discord/configs.py b/flask_discord/configs.py index f26c1bf..ff2c939 100644 --- a/flask_discord/configs.py +++ b/flask_discord/configs.py @@ -14,6 +14,11 @@ DISCORD_OAUTH_DEFAULT_SCOPES = [ ] +DISCORD_PASSTHROUGH_SCOPES = [ + "bot", "webhook.incoming", +] + + DISCORD_IMAGE_BASE_URL = "https://cdn.discordapp.com/" DISCORD_EMBED_BASE_BASE_URL = "https://cdn.discordapp.com/" DISCORD_IMAGE_FORMAT = "png" diff --git a/requirements.txt b/requirements.txt index 08d7a6a..0496fe4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,6 @@ Flask cachetools -requests_oauthlib \ No newline at end of file +setuptools +requests +oauthlib +requests_oauthlib