RFC 6750: The OAuth 2.0 Authorization Framework: Bearer Token Usage#

This section contains the generic implementation of RFC 6750.

Guide on Bearer Token#

Bearer token is used in OAuth 2.0 framework to protect resources. You need to implement the missing methods of BearerTokenValidator before using it. Learn how to use it in Flask Integration.

Token in the Request Body#

By default, resource protectors only read the access token from the Authorization header. RFC 6750 §2.2 also allows clients to send it in a form-encoded request body:

POST /resource HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded

access_token=mF_9.B5f-4.1JqM

Register the FormEncodedBodyToken extension to accept this method:

from authlib.oauth2.rfc6750 import FormEncodedBodyToken

require_oauth.register_extension(FormEncodedBodyToken())

The token is read from the body only if all these conditions are met:

  • the Content-Type is application/x-www-form-urlencoded;

  • the HTTP method is not GET;

  • the access_token parameter is not empty.

A request that sends the token both in the header and in the body gets an invalid_request error.

The RFC says that clients should not use this method unless the header cannot be used. Only enable it when your clients need it. For instance, the OpenID Connect certification tests send the token in the body of userinfo requests.

API Reference#

class authlib.oauth2.rfc6750.BearerTokenValidator(realm=None, **extra_attributes)#
authenticate_token(token_string)#

A method to query token from database with the given token string. Developers MUST re-implement this method. For instance:

def authenticate_token(self, token_string):
    return get_token_from_database(token_string)
Parameters:

token_string – A string to represent the access_token.

Returns:

token

validate_token(token, scopes, request)#

Check if token is active and matches the requested scopes.

class authlib.oauth2.rfc6750.BearerTokenGenerator(access_token_generator, refresh_token_generator=None, expires_generator=None)#

Bearer token generator which can create the payload for token response by OAuth 2 server. A typical token response would be:

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache

{
    "access_token":"mF_9.B5f-4.1JqM",
    "token_type":"Bearer",
    "expires_in":3600,
    "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA"
}
Parameters:
  • access_token_generator – a function that returns the access token. It receives client, grant_type, user, scope and expires_in, the lifetime of the token in seconds.

  • refresh_token_generator – a function that returns the refresh token. It receives client, grant_type, user and scope.

  • expires_generator – the lifetime of the access tokens, as a number of seconds or a function that receives client and grant_type.

DEFAULT_EXPIRES_IN = 3600#

default expires_in value

GRANT_TYPES_EXPIRES_IN = {'authorization_code': 864000, 'client_credentials': 864000, 'implicit': 3600, 'password': 864000}#

default expires_in value differentiate by grant_type

generate(grant_type, client, user=None, scope=None, expires_in=None, include_refresh_token=True)#

Generate a bearer token for OAuth 2.0 authorization token endpoint.

Parameters:
  • client – the client that making the request.

  • grant_type – current requested grant_type.

  • user – current authorized user.

  • expires_in – if provided, use this value as expires_in.

  • scope – current requested scope.

  • include_refresh_token – should refresh_token be included.

Returns:

Token dict

static get_allowed_scope(client, scope)#

Get the allowed scope for token generation.

Per RFC 6749 Section 3.3, if the client omits the scope parameter, the authorization server MUST either process the request using a pre-defined default value or fail the request indicating an invalid scope.

Parameters:
  • client – the client making the request

  • scope – the requested scope (may be None if omitted)

Returns:

the allowed scope string

Raises:

InvalidScopeError – if client.get_allowed_scope returns None

class authlib.oauth2.rfc6750.FormEncodedBodyToken#

ResourceProtector extension that reads the access token from a form-encoded request body per RFC 6750 Section 2.2.

The token is read only when the request has no Authorization header, a application/x-www-form-urlencoded body, and a method other than GET. Requests that send the token both in the header and in the body are refused.

Usage:

from authlib.oauth2.rfc6750 import FormEncodedBodyToken

require_oauth.register_extension(FormEncodedBodyToken())