Add support for DiscordOAuth2Scope enumeration

This commit is contained in:
weibeu
2021-06-27 20:31:10 +05:30
parent 7ed1d9133f
commit 17eb3d35a2
3 changed files with 24 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ import json
from . import configs
from . import exceptions
from . import DiscordOAuth2Scope
from flask import session, request
from collections.abc import Mapping
@@ -106,7 +107,7 @@ class DiscordOAuth2HttpClient(abc.ABC):
"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.
Parameters
@@ -115,9 +116,8 @@ class DiscordOAuth2HttpClient(abc.ABC):
The authorization token to use which was previously received from authorization code grant.
state : str, optional
The state to use for OAuth2 session.
scope : list, optional
List of valid `Discord OAuth2 Scopes
<https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes>`_.
scopes : list, optional
List of valid Discord OAuth2 Scopes from :py:class:`flask_discord.DiscordOAuth2Scope`.
Returns
-------
@@ -126,11 +126,12 @@ class DiscordOAuth2HttpClient(abc.ABC):
"""
_token = self.get_authorization_token()
scopes = {s if isinstance(s, DiscordOAuth2Scope) else DiscordOAuth2Scope(s) for s in scopes or list()}
return OAuth2Session(
client_id=self.client_id,
token=token or _token,
state=state,
scope=scope,
scope=scopes or None,
redirect_uri=self.redirect_uri,
auto_refresh_kwargs=self._get_auto_refresh_kwargs(token),
auto_refresh_url=configs.DISCORD_TOKEN_URL,

View File

@@ -2,6 +2,7 @@ import jwt
import typing
import discord
from . import DiscordOAuth2Scope
from . import configs, _http, models, utils, exceptions
from flask import request, session, redirect, current_app
@@ -58,7 +59,7 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
return session.get("DISCORD_OAUTH2_STATE", str())
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
):
"""Primary method used to create OAuth2 session and redirect users for
@@ -66,9 +67,8 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
Parameters
----------
scope : list, optional
An optional list of valid `Discord OAuth2 Scopes
<https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes>`_.
scopes : list, optional
An optional list of valid Discord OAuth2 Scopes from :py:class:`flask_discord.DiscordOAuth2Scope`.
data : dict, optional
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.
@@ -88,9 +88,9 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
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.")
data = data or dict()
@@ -98,14 +98,14 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
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)
self.__save_state(state)
params = params or dict()
params["prompt"] = "consent" if prompt else "none"
if "bot" in scope:
if DiscordOAuth2Scope.BOT in scopes:
if not isinstance(permissions, (discord.Permissions, int)):
raise ValueError(f"Passed permissions must be an int or discord.Permissions, not {type(permissions)}.")
if isinstance(permissions, discord.Permissions):

View File

@@ -1,3 +1,6 @@
from .scopes import DiscordOAuth2Scope
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_OAUTH_ALL_SCOPES = [
"bot", "connections", "email", "identify", "guilds", "guilds.join",
"gdm.join", "messages.read", "rpc", "rpc.api", "rpc.notifications.read", "webhook.incoming",
DISCORD_OAUTH_DEFAULT_SCOPES = [
DiscordOAuth2Scope.IDENTIFY, DiscordOAuth2Scope.EMAIL,
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/"