This page is for Authlete 3.0. For 2.x, see this page.
Preface
This tutorial describes the basic usage of Authlete APIs to implement an OAuth 2.0 authorization server that supports the authorization code grant flow. It also shows how Resource Servers can use Authlete APIs to quickly validate the access tokens, providing secure and authorized access to APIs. Prerequisites:- Basic knowledge of OAuth 2.0.
- Access to an Authlete account. Sign up here if needed.
Components
A typical OAuth 2.0 architecture involves multiple components, shown in the flowchart below. In this tutorial, we use the live, public cloud version of the Authlete Management Console and Authlete API Cluster running in the US region (https://us.authlete.com). The Authorization and Resource servers are simulated using curl commands etc., showing how an Authorization Server and Resource Server make API requests to Authlete for authorization, token issuance, and introspection.
The FQDNs for each component are listed below. While the Authorization Server and Client are simulated, their FQDNs are used to illustrate the OAuth flow.
Environment Setup
Follow the First API Call to create a new Authlete Service and Client. Also, generate a Service Access Token, which is needed to call Authlete APIs. Enter your own values below, and the code/command samples on this page will update automatically. (In the SDK tabs, the service access token is read from theSERVICE_ACCESS_TOKEN environment variable rather than hard-coded.)
Walk-through
The sequence diagram below shows the entire OAuth 2.0 flow as used in this tutorial. Refer back to it as you proceed through each step.Authorization request from the client to the authorization server
The client makes an authorization request to the authorization server via the user agent (Steps 3 and 4). In this tutorial, we’ll assume the following values are specified as parameters in the request. The values from the Environment Setup inputs are reflected below. The authorization server is expected to receive the following content (folded for readability) as an HTTP GET query string from the user agent. A typical authorization server should evaluate the following rules before proceeding with the authorization code grant flow.- Verify that a client associated with the client ID is registered with the authorization server.
- Check that the redirect URI matches one of the URIs registered for the client.
- Confirm that other parameter values, such as
response_typeandscope, are permitted for the client to specify in its request.
/auth/authorization API performs the above request validation on behalf of the authorization server.
Use sample command/code to make a request to the Authlete API (Step 5) by choosing one from the following language tab; the values will be updated as you edit the inputs above.
resultMessageprovides human-readable result of the request processing. (See also Interpreting Authlete’s result codes)actionindicates what the authorization server should do next. (See also Action Handling)ticketis required for the authorization server to make a request to another API in the following step. (See also Two-Step API Calls)
User authentication and confirmation of granting access
The actual interaction between the resource owner and the authorization server is out of scope for this tutorial. Typically, the authorization server would authenticate the user with credentials (e.g., ID and password), determine the user’s roles and privileges, and ask if they authorize access (Steps 7, 8 and 9).Issuing an authorization code
Assume the authorization server reaches the following state after completing the previous process:- The authorization server has authenticated the resource owner and determined that the identifier to be shared with Authlete as the value of the
subjectparameter istestuser01. - The authorization server has obtained consent from the resource owner.
/auth/authorization/issue to issue an authorization code. It includes the values of subject and ticket from the /auth/authorization API response as request parameters (Step 10). Enter your values below and the samples update automatically.
resultMessageprovides a human-readable result of the request processing. (See also Interpreting Authlete’s result codes)actionindicates what the authorization server should do next. The value in this response isLOCATION, meaning the authorization server should make a redirection response back to the user agent. (See also Action Handling)responseContentrepresents the content of the response from the authorization server. (See also Action Handling)
/auth/authorization/fail API supports the termination process in terms of messages to be sent to the client, and transfer method for the response.
To summarize, an authentication server usually makes a request to either /auth/authorization/issue or /auth/authorization/fail API depending on result of user authentication and consent.
Token request
Here, we assume that the user agent receives the redirection response from the authorization server. It then sends the following request (folded for readability) to the client (Step 13). Enter the authorization code (thecode from the previous response) below and the samples update.
The client extracts the value of the code parameter, crafts a token request using this value, and sends it to the authorization server as follows (folded for readability). In this tutorial, https://as.example.com/token is the token endpoint URI (Step 14).
The authorization server evaluates the parameters in the request and then sends a token response back to the client.
In this tutorial, we’ll use Authlete’s /auth/token API to evaluate the request and generate the response.
resultMessageprovides a human-readable result of the request processing. (See also Interpreting Authlete’s result codes)actionindicates what the authorization server should do next. The value in this response isOK, meaning the authorization server should send a token response back to the client. (See also Action Handling)responseContentcontains the content of the response from the authorization server. (See also Action Handling)
The authorization server has now successfully created the tokens and provided them to the client. By leveraging Authlete APIs, the authorization server avoids the need to implement complex logic to evaluate parameters in authorization and token requests and can respond correctly using the appropriate methods.
API request (access token introspection)
In most cases, the client sends a request with the access token to the resource server providing the APIs (Step 18). The resource server is responsible for evaluating the token’s validity, retrieving information about the user and client associated with the token, and determining how to respond to the API request. Authlete provides the/auth/introspection API for this purpose. It verifies the token’s validity and provides the necessary information.
Enter the access token received by the resource server below, then use the following command to introspect it using the Authlete API.
expiresAt), the identifier of the user who approved access (subject), the grant type used to obtain the token, and the client identifier (clientId). It then determines how to respond to the API request (Step 21).