mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2026-02-04 18:24:15 +02:00
Add support for DiscordOAuth2Scope enumeration
This commit is contained in:
@@ -6,6 +6,7 @@ import json
|
|||||||
|
|
||||||
from . import configs
|
from . import configs
|
||||||
from . import exceptions
|
from . import exceptions
|
||||||
|
from . import DiscordOAuth2Scope
|
||||||
|
|
||||||
from flask import session, request
|
from flask import session, request
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
@@ -106,7 +107,7 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
"refresh_token": token["refresh_token"]
|
"refresh_token": token["refresh_token"]
|
||||||
}
|
}
|
||||||
|
|
||||||
def _make_session(self, token: dict = None, state: str = None, scope: list = None) -> OAuth2Session:
|
def _make_session(self, token: dict = None, state: str = None, scopes: list = None) -> OAuth2Session:
|
||||||
"""A low level method used for creating OAuth2 session.
|
"""A low level method used for creating OAuth2 session.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@@ -115,9 +116,8 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
The authorization token to use which was previously received from authorization code grant.
|
The authorization token to use which was previously received from authorization code grant.
|
||||||
state : str, optional
|
state : str, optional
|
||||||
The state to use for OAuth2 session.
|
The state to use for OAuth2 session.
|
||||||
scope : list, optional
|
scopes : list, optional
|
||||||
List of valid `Discord OAuth2 Scopes
|
List of valid Discord OAuth2 Scopes from :py:class:`flask_discord.DiscordOAuth2Scope`.
|
||||||
<https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes>`_.
|
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
@@ -126,11 +126,12 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
_token = self.get_authorization_token()
|
_token = self.get_authorization_token()
|
||||||
|
scopes = {s if isinstance(s, DiscordOAuth2Scope) else DiscordOAuth2Scope(s) for s in scopes or list()}
|
||||||
return OAuth2Session(
|
return OAuth2Session(
|
||||||
client_id=self.client_id,
|
client_id=self.client_id,
|
||||||
token=token or _token,
|
token=token or _token,
|
||||||
state=state,
|
state=state,
|
||||||
scope=scope,
|
scope=scopes or None,
|
||||||
redirect_uri=self.redirect_uri,
|
redirect_uri=self.redirect_uri,
|
||||||
auto_refresh_kwargs=self._get_auto_refresh_kwargs(token),
|
auto_refresh_kwargs=self._get_auto_refresh_kwargs(token),
|
||||||
auto_refresh_url=configs.DISCORD_TOKEN_URL,
|
auto_refresh_url=configs.DISCORD_TOKEN_URL,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import jwt
|
|||||||
import typing
|
import typing
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
|
from . import DiscordOAuth2Scope
|
||||||
from . import configs, _http, models, utils, exceptions
|
from . import configs, _http, models, utils, exceptions
|
||||||
|
|
||||||
from flask import request, session, redirect, current_app
|
from flask import request, session, redirect, current_app
|
||||||
@@ -58,7 +59,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
return session.get("DISCORD_OAUTH2_STATE", str())
|
return session.get("DISCORD_OAUTH2_STATE", str())
|
||||||
|
|
||||||
def create_session(
|
def create_session(
|
||||||
self, scope: list = None, *, data: dict = None, prompt: bool = True,
|
self, scopes: list = None, *, data: dict = None, prompt: bool = True,
|
||||||
permissions: typing.Union[discord.Permissions, int] = 0, **params
|
permissions: typing.Union[discord.Permissions, int] = 0, **params
|
||||||
):
|
):
|
||||||
"""Primary method used to create OAuth2 session and redirect users for
|
"""Primary method used to create OAuth2 session and redirect users for
|
||||||
@@ -66,9 +67,8 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
scope : list, optional
|
scopes : list, optional
|
||||||
An optional list of valid `Discord OAuth2 Scopes
|
An optional list of valid Discord OAuth2 Scopes from :py:class:`flask_discord.DiscordOAuth2Scope`.
|
||||||
<https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes>`_.
|
|
||||||
data : dict, optional
|
data : dict, optional
|
||||||
A mapping of your any custom data which you want to access after authorization grant. Use
|
A mapping of your any custom data which you want to access after authorization grant. Use
|
||||||
`:py:meth:flask_discord.DiscordOAuth2Session.callback` to retrieve this data in your callback view.
|
`:py:meth:flask_discord.DiscordOAuth2Session.callback` to retrieve this data in your callback view.
|
||||||
@@ -88,9 +88,9 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
Flask redirect to discord authorization servers to complete authorization code grant process.
|
Flask redirect to discord authorization servers to complete authorization code grant process.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
scope = scope or request.args.get("scope", str()).split() or configs.DISCORD_OAUTH_DEFAULT_SCOPES
|
scopes = scopes or request.args.get("scope", str()).split() or configs.DISCORD_OAUTH_DEFAULT_SCOPES
|
||||||
|
|
||||||
if not prompt and set(scope) & set(configs.DISCORD_PASSTHROUGH_SCOPES):
|
if not prompt and set(scopes) & set(configs.DISCORD_PASSTHROUGH_SCOPES):
|
||||||
raise ValueError("You should use explicit OAuth grant for passthrough scopes like bot.")
|
raise ValueError("You should use explicit OAuth grant for passthrough scopes like bot.")
|
||||||
|
|
||||||
data = data or dict()
|
data = data or dict()
|
||||||
@@ -98,14 +98,14 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
|
|
||||||
state = jwt.encode(data, current_app.config["SECRET_KEY"], algorithm="HS256")
|
state = jwt.encode(data, current_app.config["SECRET_KEY"], algorithm="HS256")
|
||||||
|
|
||||||
discord_session = self._make_session(scope=scope, state=state)
|
discord_session = self._make_session(scopes=scopes, state=state)
|
||||||
authorization_url, state = discord_session.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL)
|
authorization_url, state = discord_session.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL)
|
||||||
|
|
||||||
self.__save_state(state)
|
self.__save_state(state)
|
||||||
|
|
||||||
params = params or dict()
|
params = params or dict()
|
||||||
params["prompt"] = "consent" if prompt else "none"
|
params["prompt"] = "consent" if prompt else "none"
|
||||||
if "bot" in scope:
|
if DiscordOAuth2Scope.BOT in scopes:
|
||||||
if not isinstance(permissions, (discord.Permissions, int)):
|
if not isinstance(permissions, (discord.Permissions, int)):
|
||||||
raise ValueError(f"Passed permissions must be an int or discord.Permissions, not {type(permissions)}.")
|
raise ValueError(f"Passed permissions must be an int or discord.Permissions, not {type(permissions)}.")
|
||||||
if isinstance(permissions, discord.Permissions):
|
if isinstance(permissions, discord.Permissions):
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
from .scopes import DiscordOAuth2Scope
|
||||||
|
|
||||||
|
|
||||||
DISCORD_API_VERSION = 9
|
DISCORD_API_VERSION = 9
|
||||||
|
|
||||||
|
|
||||||
@@ -8,12 +11,14 @@ DISCORD_AUTHORIZATION_BASE_URL = DISCORD_API_BASE_URL + "/oauth2/authorize"
|
|||||||
DISCORD_TOKEN_URL = DISCORD_API_BASE_URL + "/oauth2/token"
|
DISCORD_TOKEN_URL = DISCORD_API_BASE_URL + "/oauth2/token"
|
||||||
|
|
||||||
|
|
||||||
DISCORD_OAUTH_ALL_SCOPES = [
|
DISCORD_OAUTH_DEFAULT_SCOPES = [
|
||||||
"bot", "connections", "email", "identify", "guilds", "guilds.join",
|
DiscordOAuth2Scope.IDENTIFY, DiscordOAuth2Scope.EMAIL,
|
||||||
"gdm.join", "messages.read", "rpc", "rpc.api", "rpc.notifications.read", "webhook.incoming",
|
DiscordOAuth2Scope.GUILDS, DiscordOAuth2Scope.JOIN_GUILDS,
|
||||||
|
]
|
||||||
|
DISCORD_PASSTHROUGH_SCOPES = [
|
||||||
|
DiscordOAuth2Scope.APPLICATION_COMMANDS,
|
||||||
|
DiscordOAuth2Scope.BOT, DiscordOAuth2Scope.INCOMING_WEBHOOK,
|
||||||
]
|
]
|
||||||
DISCORD_OAUTH_DEFAULT_SCOPES = ["identify", "email", "guilds", "guilds.join", ]
|
|
||||||
DISCORD_PASSTHROUGH_SCOPES = ["bot", "webhook.incoming", ]
|
|
||||||
|
|
||||||
|
|
||||||
DISCORD_IMAGE_BASE_URL = "https://cdn.discordapp.com/"
|
DISCORD_IMAGE_BASE_URL = "https://cdn.discordapp.com/"
|
||||||
|
|||||||
Reference in New Issue
Block a user