This tutorial demonstrates how to add authorization to a Python API built with Flask.We recommend that you log in to follow this quickstart with examples configured for your account.
In the APIs section of the Auth0 dashboard, click Create API. Provide a name and an identifier for your API, for example, https://quickstarts/api. You will use the identifier as an audience later, when you are configuring the Access Token verification. Leave the Signing Algorithm as RS256.
By default, your API uses RS256 as the algorithm for signing tokens. Since RS256 uses a private/public keypair, it verifies the tokens against the public key for your Auth0 account. The public key is in the JSON Web Key Set (JWKS) format, and can be accessed here.
Permissions let you define how resources can be accessed on behalf of the user with a given access token. For example, you might choose to grant read access to the messages resource if users have the manager access level, and a write access to that resource if they have the administrator access level.You can define allowed permissions in the Permissions view of the Auth0 Dashboard’s APIs section.
This example uses the read:messages scope.
This example demonstrates:
How to check for a JSON Web Token (JWT) in the Authorization header of an incoming HTTP request.
Individual routes can be configured to look for a particular scope in the Access Token by using the following:
Python
Report incorrect code
Copy
Ask AI
# /server.pydef requires_scope(required_scope): """Determines if the required scope is present in the Access Token Args: required_scope (str): The scope required to access the resource """ token = get_token_auth_header() unverified_claims = jwt.decode(token, options={"verify_signature": False}) if unverified_claims.get("scope"): token_scopes = unverified_claims["scope"].split() for token_scope in token_scopes: if token_scope == required_scope: return True return False
The routes shown below are available for the following requests:
GET /api/public: available for non-authenticated requests
GET /api/private: available for authenticated requests containing an access token with no additional scopes
GET /api/private-scoped: available for authenticated requests containing an access token with the read:messages scope granted
You can use the decorators and functions define above in the corresponding endpoints.
Python
Report incorrect code
Copy
Ask AI
# Controllers API# This doesn't need authentication@APP.route("/api/public")@cross_origin(headers=["Content-Type", "Authorization"])def public(): response = "Hello from a public endpoint! You don't need to be authenticated to see this." return jsonify(message=response)# This needs authentication@APP.route("/api/private")@cross_origin(headers=["Content-Type", "Authorization"])@requires_authdef private(): response = "Hello from a private endpoint! You need to be authenticated to see this." return jsonify(message=response)# This needs authorization@APP.route("/api/private-scoped")@cross_origin(headers=["Content-Type", "Authorization"])@requires_authdef private_scoped(): if requires_scope("read:messages"): response = "Hello from a private endpoint! You need to be authenticated and have a scope of read:messages to see this." return jsonify(message=response) raise AuthError({ "code": "Unauthorized", "description": "You don't have access to this resource" }, 403)