Reduce nesting for the sample authentication decorator (#1715)
* Reduce nesting for the sample authentication decorator * Add missing decorator argument
This commit is contained in:
parent
a4185a0ba7
commit
ed1f367a8a
|
@ -13,28 +13,26 @@ def check_request_for_authorization_status(request):
|
||||||
return flag
|
return flag
|
||||||
|
|
||||||
|
|
||||||
def authorized():
|
def authorized(f):
|
||||||
def decorator(f):
|
@wraps(f)
|
||||||
@wraps(f)
|
async def decorated_function(request, *args, **kwargs):
|
||||||
async def decorated_function(request, *args, **kwargs):
|
# run some method that checks the request
|
||||||
# run some method that checks the request
|
# for the client's authorization status
|
||||||
# for the client's authorization status
|
is_authorized = check_request_for_authorization_status(request)
|
||||||
is_authorized = check_request_for_authorization_status(request)
|
|
||||||
|
|
||||||
if is_authorized:
|
if is_authorized:
|
||||||
# the user is authorized.
|
# the user is authorized.
|
||||||
# run the handler method and return the response
|
# run the handler method and return the response
|
||||||
response = await f(request, *args, **kwargs)
|
response = await f(request, *args, **kwargs)
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
# the user is not authorized.
|
# the user is not authorized.
|
||||||
return json({'status': 'not_authorized'}, 403)
|
return json({'status': 'not_authorized'}, 403)
|
||||||
return decorated_function
|
return decorated_function
|
||||||
return decorator
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
@authorized()
|
@authorized
|
||||||
async def test(request):
|
async def test(request):
|
||||||
return json({'status': 'authorized'})
|
return json({'status': 'authorized'})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user