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

@@ -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):