Simple Flask-Rebar authenticator for Auth0. Your access tokens need to be JWT tokens for this authenticator to work.
# Config
app.config.from_mapping({
"AUTH0_ENDPOINT": "perdu.auth0.com", # The Auth0 domain for your tenant
"AUTH0_ALGORITHMS": ["RS256"], # The authorized algorithms, you should not have to change it
"AUTH0_AUDIENCE": "https://api.perdu.com" # The API Identifier as set on Auth0
"AUTH0_HEADER_AUTHENTICATION": True # Use the authentication by header
"AUTH0_HEADER_NAME": "Authorization" # (OPTIONAL) Change the header used
"AUTH0_HEADER_PREFIX": "Bearer" # (OPTIONAL) Change the prefix used
# OR
"AUTH0_COOKIE_AUTHENTICATION": True # Use the authentication by cookie
"AUTH0_COOKIE_NAME": "Some Cookie" # Name of the cookie containing the access token
"AUTH0_TESTING": False # (OPTIONAL) Disable expiration check of tokens during tests
})
# Create
authenticator = Auth0Authenticator(app)
@authenticator.identity_handler
def create_user(claims: Dict[str, Any]) -> Any:
"""Built a user object from the claims"""
return { "id": claims["sub"] }
from flask_rebar_auth0 import get_authenticated_user
@registry.handles(
rule="/users/me",
method="GET",
marshal_schema=UserSchema(),
authenticator=authenticator, # Use the authenticator
)
def get_user():
return get_authenticated_user() # Get the user data created by the identity_handler
@registry.handles(
rule="/users/me/location",
method="GET",
marshal_schema=UserLocationSchema(),
authenticator=authenticator.with_scopes(["read:location"]), # Require some scopes to access the ressource
)
def get_user_location():
user = get_authenticated_user()
return locationService.get(user)
If you wish to use swagger, you will need to register the custom authenticators.
This is needed by rebar
to be able to convert them to the right swagger security definition.
If you use the Cookie
authentication, please note that swagger 2.0 does support this method of authentication. It will be registered as a Cookie
header.
from flask_rebar_auth0 import register_authenticators
register_authenticators(registry)
During tests, we suggest setting AUTH0_TESTING
to True
to allow expired tokens to be used.
The setting also disables the request to auth0 for the keys, you MUST supply them manually.
my_key = {
"alg": "RS256",
"kty": "RSA",
"use": "sig",
"x5c": [
"MIIC+TCCAeGgAwIBAgIJFDs6wMv+QZ3IMA0GCSqGSIb3DQEBCwUAMBoxGDA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