Add requires_authorization utility

This commit is contained in:
thecosmos
2020-04-29 18:38:23 +05:30
parent 30f6bc7d7a
commit e05d247408
6 changed files with 45 additions and 6 deletions

View File

@@ -1,14 +1,16 @@
from .client import DiscordOAuth2Session
from .exceptions import *
from .utils import *
from .client import DiscordOAuth2Session
__all__ = [
"DiscordOAuth2Session",
"requires_authorization",
"HttpException",
"Unauthorized",
]
__version__ = "0.1.10"
__version__ = "0.1.11"

24
flask_discord/utils.py Normal file
View File

@@ -0,0 +1,24 @@
"""Few utility functions and decorators."""
import functools
from . import exceptions
from flask import current_app
# Decorators.
def requires_authorization(view):
"""A decorator for flask views which raises exception :py:class:`flask_discord.exceptions.Unauthorized` if the user
is not authorized from Discord OAuth2.
"""
# TODO: Add support to validate scopes.
@functools.wraps(view)
def wrapper(*args, **kwargs):
if not current_app.discord.authorized:
raise exceptions.Unauthorized
return view(*args, **kwargs)
return wrapper