mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-10 12:29:30 +02:00
✔ Add support to specify custom parameters in authorization URL along with ability to determine authorization type
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from . import configs, _http, models
|
from . import configs, _http, models
|
||||||
|
|
||||||
from flask import request, session, redirect
|
from flask import request, session, redirect
|
||||||
|
from oauthlib.common import add_params_to_uri
|
||||||
|
|
||||||
|
|
||||||
class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
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
|
"""Primary method used to create OAuth2 session and redirect users for
|
||||||
authorization code grant.
|
authorization code grant.
|
||||||
|
|
||||||
@@ -25,6 +26,11 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
scope : list, optional
|
scope : list, optional
|
||||||
An optional list of valid `Discord OAuth2 Scopes
|
An optional list of valid `Discord OAuth2 Scopes
|
||||||
<https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes>`_.
|
<https://discordapp.com/developers/docs/topics/oauth2#shared-resources-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
|
Returns
|
||||||
-------
|
-------
|
||||||
@@ -33,9 +39,19 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
scope = scope or request.args.get("scope", str()).split() or configs.DISCORD_OAUTH_DEFAULT_SCOPES
|
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)
|
discord_session = self._make_session(scope=scope)
|
||||||
authorization_url, state = discord_session.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL)
|
authorization_url, state = discord_session.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL)
|
||||||
session["DISCORD_OAUTH2_STATE"] = state
|
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)
|
return redirect(authorization_url)
|
||||||
|
|
||||||
def callback(self):
|
def callback(self):
|
||||||
|
|||||||
@@ -14,6 +14,11 @@ DISCORD_OAUTH_DEFAULT_SCOPES = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
DISCORD_PASSTHROUGH_SCOPES = [
|
||||||
|
"bot", "webhook.incoming",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
DISCORD_IMAGE_BASE_URL = "https://cdn.discordapp.com/"
|
DISCORD_IMAGE_BASE_URL = "https://cdn.discordapp.com/"
|
||||||
DISCORD_EMBED_BASE_BASE_URL = "https://cdn.discordapp.com/"
|
DISCORD_EMBED_BASE_BASE_URL = "https://cdn.discordapp.com/"
|
||||||
DISCORD_IMAGE_FORMAT = "png"
|
DISCORD_IMAGE_FORMAT = "png"
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
Flask
|
Flask
|
||||||
cachetools
|
cachetools
|
||||||
|
setuptools
|
||||||
|
requests
|
||||||
|
oauthlib
|
||||||
requests_oauthlib
|
requests_oauthlib
|
||||||
Reference in New Issue
Block a user