Protect your API on Python
Extract the Bearer Token from request header
"""requires-auth.py
"""
def get_auth_token():
auth = request.headers.get("Authorization", None)
if not auth:
raise Error({ code: 'auth.authorization_header_missing', status: 401 })
contents = auth.split()
if len(contents) < 2
raise Error({code: 'auth.authorization_token_invalid_format', status: 401})
elif contents[0] != 'Bearer'
raise Error({code: 'auth.authorization_token_type_not_supported', status: 401})
return contents[1]
Token validation
Install jose as your dependency
pip install python-jose[rsa]Retrieve Auth’s OIDC configurations
Create the authorization validation decorator using the Auth’s configurations
Apply decorator to your API
Last updated