mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-09 20:09:30 +02:00
Add requires_authorization utility
This commit is contained in:
@@ -14,7 +14,7 @@ python3 -m pip install Flask-Discord
|
|||||||
### Basic Example
|
### Basic Example
|
||||||
```python
|
```python
|
||||||
from flask import Flask, redirect, url_for
|
from flask import Flask, redirect, url_for
|
||||||
from flask_discord import DiscordOAuth2Session
|
from flask_discord import DiscordOAuth2Session, requires_authorization
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -40,6 +40,7 @@ def callback():
|
|||||||
|
|
||||||
|
|
||||||
@app.route("/me/")
|
@app.route("/me/")
|
||||||
|
@requires_authorization
|
||||||
def me():
|
def me():
|
||||||
user = discord.fetch_user()
|
user = discord.fetch_user()
|
||||||
return f"""
|
return f"""
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ Models
|
|||||||
:inherited-members:
|
:inherited-members:
|
||||||
|
|
||||||
|
|
||||||
|
Utilities
|
||||||
|
|
||||||
|
.. py:function:: flask_discord.requires_authorization
|
||||||
|
|
||||||
|
|
||||||
Exceptions
|
Exceptions
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ in exchange for fetching user's details and display them on web page.
|
|||||||
.. code-block:: python3
|
.. code-block:: python3
|
||||||
|
|
||||||
from flask import Flask, redirect, url_for
|
from flask import Flask, redirect, url_for
|
||||||
from flask_discord import DiscordOAuth2Session
|
from flask_discord import DiscordOAuth2Session, requires_authorization
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -70,6 +70,7 @@ in exchange for fetching user's details and display them on web page.
|
|||||||
|
|
||||||
|
|
||||||
@app.route("/me/")
|
@app.route("/me/")
|
||||||
|
@requires_authorization
|
||||||
def me():
|
def me():
|
||||||
user = discord.fetch_user()
|
user = discord.fetch_user()
|
||||||
return f"""
|
return f"""
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
from .client import DiscordOAuth2Session
|
|
||||||
|
|
||||||
from .exceptions import *
|
from .exceptions import *
|
||||||
|
from .utils import *
|
||||||
|
|
||||||
|
from .client import DiscordOAuth2Session
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"DiscordOAuth2Session",
|
"DiscordOAuth2Session",
|
||||||
|
"requires_authorization",
|
||||||
|
|
||||||
"HttpException",
|
"HttpException",
|
||||||
"Unauthorized",
|
"Unauthorized",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
__version__ = "0.1.10"
|
__version__ = "0.1.11"
|
||||||
|
|||||||
24
flask_discord/utils.py
Normal file
24
flask_discord/utils.py
Normal 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
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from flask import Flask, redirect, url_for
|
from flask import Flask, redirect, url_for
|
||||||
from flask_discord import DiscordOAuth2Session
|
from flask_discord import DiscordOAuth2Session, requires_authorization
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -79,5 +79,11 @@ def logout():
|
|||||||
return redirect(url_for(".index"))
|
return redirect(url_for(".index"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/secret/")
|
||||||
|
@requires_authorization
|
||||||
|
def secret():
|
||||||
|
return os.urandom(16)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user