2017-08-21 15:28:23 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from functools import wraps
|
2021-11-23 21:00:25 +00:00
|
|
|
|
|
|
|
from sanic import Sanic
|
2017-08-21 15:28:23 +01:00
|
|
|
from sanic.response import json
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
|
|
|
|
app = Sanic(__name__)
|
2017-08-21 15:28:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def check_request_for_authorization_status(request):
|
|
|
|
# Note: Define your check, for instance cookie, session.
|
|
|
|
flag = True
|
|
|
|
return flag
|
|
|
|
|
|
|
|
|
2019-11-14 20:57:41 +00:00
|
|
|
def authorized(f):
|
|
|
|
@wraps(f)
|
|
|
|
async def decorated_function(request, *args, **kwargs):
|
|
|
|
# run some method that checks the request
|
|
|
|
# for the client's authorization status
|
|
|
|
is_authorized = check_request_for_authorization_status(request)
|
|
|
|
|
|
|
|
if is_authorized:
|
|
|
|
# the user is authorized.
|
|
|
|
# run the handler method and return the response
|
|
|
|
response = await f(request, *args, **kwargs)
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
# the user is not authorized.
|
2021-11-23 21:00:25 +00:00
|
|
|
return json({"status": "not_authorized"}, 403)
|
|
|
|
|
2019-11-14 20:57:41 +00:00
|
|
|
return decorated_function
|
2017-08-21 15:28:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
2019-11-14 20:57:41 +00:00
|
|
|
@authorized
|
2017-08-21 15:28:23 +01:00
|
|
|
async def test(request):
|
2021-11-23 21:00:25 +00:00
|
|
|
return json({"status": "authorized"})
|
|
|
|
|
2017-08-21 15:28:23 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0", port=8000)
|