This is an overview of the various OAuth2.0 authorization flows and their respective request and response parameters, as defined in RFC6749. This reference aims to provide a quick and easy reference for developers. Please refer to the specification for more detailed information.
This repository automatically generates a static webpage at https://mijolabs.github.io/oauth2-cheatsheet.
- Authorization Code Grant
- Password Credentials Grant
- Client Credentials Grant
- Token Refresh
- Common Token Response
- Error Responses
- Extensions
Method | URI |
---|---|
GET |
authorization_endpoint |
Parameter | Value |
---|---|
content-type |
application/x-www-form-urlencoded |
Parameter | Value | Required | Comment |
---|---|---|---|
response_type |
code |
Required | |
client_id |
string | Required | |
redirect_uri |
string | Optional | |
scope |
string | Optional | |
state |
string | Recommended | |
code_challenge |
string | Optional | Required for PKCE. |
code_challenge_method |
plain |S256 |
Optional | Optional when using PKCE. Defaults to plain if not present in the request but S256 strongly recommended. |
Parameter | Value | Mandatory |
---|---|---|
code |
string | Required |
state |
string | Recommended |
Method | URI |
---|---|
POST |
token_endpoint |
Parameter | Value |
---|---|
content-type |
application/x-www-form-urlencoded |
Parameter | Value | Required | Comment |
---|---|---|---|
grant_type |
authorization_code |
Required | |
code |
string | Required | |
redirect_uri |
string | Required | |
client_id |
string | Required | |
code_verifier |
string | Optional | Required for PKCE. |
Token Endpoint: Error Responses
Method | URI |
---|---|
POST |
token_endpoint |
Parameter | Value |
---|---|
content-type |
application/x-www-form-urlencoded |
Parameter | Value | Required |
---|---|---|
grant_type |
password |
Required |
username |
string | Required |
password |
string | Required |
scope |
string | Optional |
Token Endpoint: Error Responses
Method | URI |
---|---|
POST |
token_endpoint |
Parameter | Value |
---|---|
content-type |
application/x-www-form-urlencoded |
Parameter | Value | Required |
---|---|---|
grant_type |
client_credentials |
Required |
scope |
string | Optional |
Token Endpoint: Error Responses
Method | URI |
---|---|
POST |
token_endpoint |
Parameter | Value |
---|---|
content-type |
application/x-www-form-urlencoded |
Parameter | Value | Required |
---|---|---|
grant_type |
refresh_token |
Required |
refresh_token |
string | Required |
scope |
string | Optional |
Token Endpoint: Error Responses
Parameter | Value |
---|---|
cache-control |
nostore |
pragma |
no-cache |
Parameter | Value | Required | Comment |
---|---|---|---|
access_token |
string | Required | |
token_type |
bearer | mac |
Required | |
expires_in |
integer | Recommended | The lifetime in seconds of the access token. |
refresh_token |
string | Optional | Not included in response to a Client Credentials Grant token request. |
scope |
string | Optional |
Value | Description |
---|---|
unauthorized_client |
The client is not authorized to request an authorization code using this method. |
access_denied |
The resource owner or authorization server denied the request. |
unsupported_response_type |
The authorization server does not support obtaining an authorization code using this method. |
invalid_scope |
The requested scope is invalid, unknown, or malformed. |
server_error |
The authorization server encountered an unexpected condition that prevented it from fulfilling the request. (This error code is needed because a 500 Internal Server Error HTTP status code cannot be returned to the client via an HTTP redirect.) |
temporarily_unavailable |
The authorization server is currently unable to handle the request due to a temporary overloading or maintenance of the server. (This error code is needed because a 503 Service Unavailable HTTP status code cannot be returned to the client via an HTTP redirect.) |
Value | Description |
---|---|
invalid_request |
The request is missing a required parameter, includes an unsupported parameter value (other than grant type), repeats a parameter, includes multiple credentials, utilizes more than one mechanism for authenticating the client, or is otherwise malformed. |
invalid_client |
Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method). The authorization server MAY return an HTTP 401 (Unauthorized) status code to indicate which HTTP authentication schemes are supported. If the client attempted to authenticate via the "Authorization" request header field, the authorization server MUST respond with an HTTP 401 (Unauthorized) status code and include the "WWW-Authenticate" response header field matching the authentication scheme used by the client. |
invalid_grant |
The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client. |
unauthorized_client |
The authenticated client is not authorized to use this authorization grant type. |
unsupported_grant_type |
The authorization grant type is not supported by the authorization server. |
invalid_scope |
The requested scope is invalid, unknown, malformed, or exceeds the scope granted by the resource owner. |
OAuth 2.0 public clients utilizing the Authorization Code Grant are susceptible to the authorization code interception attack. This specification describes the attack as well as a technique to mitigate against the threat through the use of Proof Key for Code Exchange (PKCE, pronounced "pixy").
code_verifier
= high-entropy cryptographic random STRING using the unreserved characters A-Z
/ a-z
/ 0-9
/ -
/ .
/ _
/ ~
, with a minimum length of 43 characters and a maximum length of 128 characters.
It is RECOMMENDED that the output of a suitable random number generator be used to create a 32-octet sequence. The octet sequence is then base64url-encoded to produce a 43-octet URL safe string to use as the code verifier.
The client then creates a code challenge derived from the code verifier by using one of the following transformations on the code verifier:
Method | Transformation |
---|---|
plain |
code_challenge = code_verifier |
S256 |
code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier))) |
If the client is capable of using
S256
, it MUST useS256
, asS256
is Mandatory To Implement (MTI) on the server. Clients are permitted to useplain
only if they cannot supportS256
for some technical reason and know via out-of-band configuration that the server supports "plain". Theplain
transformation is for compatibility with existing deployments and for constrained environments that can't use theS256
transformation.
import secrets
import hashlib
import base64
def generate_code_verifier(self, length: int = 128) -> str:
if not 43 <= length <= 128:
raise ValueError("Parameter 'length' must verify '43 <= length <= 128'")
code_verifier = secrets.token_urlsafe(96)[:length]
return code_verifier
def generate_code_challenge(self,
code_verifier: str,
code_challenge_method: str = 'S256') -> str:
if code_challenge_method == 'S256':
code_challenge = hashlib.sha256(code_verifier.encode('utf-8')).digest()
code_challenge = base64.urlsafe_b64decode(code_challenge)
code_challenge = code_challenge.decode('utf-8').rstrip('=')
elif code_challenge_method == 'plain':
code_challenge = code_verifier
return code_challenge
code_verifier = generate_code_verifier()
code_challenge = generate_code_challenge(code_verifier))
Parameter | Value | Required | Comment |
---|---|---|---|
code_challenge |
string | Optional | Required for PKCE. |
code_challenge_method |
plain | S256 |
Optional | Optional when using PKCE. Defaults to plain if not present in the request but S256 strongly recommended. |
Parameter | Value | Required | Comment |
---|---|---|---|
code_verifier |
string | Optional | Required for PKCE. |