Skip to main content

Flask-Rebar-Auth0 is a simple Flask-Rebar authenticator for Auth0

Project description

Flask-Rebar-Auth0

PyPI version Python versions Test codecov code style License: MIT

Simple Flask-Rebar authenticator for Auth0. Your access tokens need to be JWT tokens for this authenticator to work.

Initialization

# 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"] }

Usage

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)

Swagger

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)

Testing

During tests, we suggest setting AUTH0_TESTING to True to allow expired tokens to be used. The setting also disables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LdQuk7doxGCdNlTVb2Ig8BhrjGg+5oGZh7KeZX7qHOb"
  ],
  "n": "oSx-m5j904IjXIWcFXwy1Sar9K_MCE5bxIm8vJmm_H7NWfP1auTstf8dWXGN2hBlca3IghK4wbmtxBoD7iNnoFymGXg7H2jpvSnD1o1A9LXYhTYczxv6zNj4Cp9lmvXHBM1ofyY462l9xBPjMeP5xieuN-w3l3ujs_Z2SmaT4TTa2D-OroyyhhpMvmsMvVyUvVGfSiwuukUXHoS_zCsRRvbAJGAx4qVIAutzRiCaZLMnN5Nb10iB-iyQGLGT46a0L865urlH9u7qugXdVZs2Vfu1BLIs00sxlua9rUYyW81bCBMe-D6GxhqScM3o8N9XqZaI1LUQ9pf6P0Z0xrn2RQ",
  "e": "AQAB",
  "kid": "OTEyNDRCREE1OTlEOUYwNEM2QTM5RkJEODkxOEQyMDQ0NjYxRENEMw",
  "x5t": "OTEyNDRCREE1OTlEOUYwNEM2QTM5RkJEODkxOEQyMDQ0NjYxRENEMw"
}

authenticator = Auth0Authenticator(app)
authenticator.add_key(my_key)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-rebar-auth0-0.2.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_rebar_auth0-0.2.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file flask-rebar-auth0-0.2.1.tar.gz.

File metadata

  • Download URL: flask-rebar-auth0-0.2.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.0

File hashes

Hashes for flask-rebar-auth0-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6258477309f75a4ebbd28fe446e6bc272ac307be928f704325f13e50cd2d610f
MD5 82f65fe80c4504e8e7aff2f98ccf3c10
BLAKE2b-256 56903babe6d654037403d109203baf217fa2670c672f440c3af8dbb07656fb7a

See more details on using hashes here.

File details

Details for the file flask_rebar_auth0-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: flask_rebar_auth0-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.0

File hashes

Hashes for flask_rebar_auth0-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b51bdc7d586ea3f2224afd68436e5c0f6ef6ef611304aa39f01efcb4c8c1ad52
MD5 73c92eeb34ffd4491612637f738e17a1
BLAKE2b-256 921e1a8e26f511391c221a56ba0d2cb91320a0191436ddfbbdb9228ceb35b2ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page