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 versions 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, 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.
| Component | FQDN |
|---|---|
| Authlete API - US Cluster | us.authlete.com |
| Authlete Management Console | console.authlete.com |
| Authorization Server | as.example.com |
| Client | client.example.org |
| Resource Server | N/A |
Environment Setup
Follow the Quick Setup Guide to create a new Authlete Service and Client. Also, generate a Service Access Token, which is needed to call Authlete APIs. This tutorial assumes you have already created a service in the US cluster and a client as described in the Quick Setup Guide.| Item | Value |
|---|---|
| Authlete API - US Cluster | https://us.authlete.com |
| Service ID | Auto-generated, e.g., 933860280 |
| Service Access Token | Auto-generated, e.g. DL7jo1z3-iUIXyI5MnX... |
| Client ID | Auto-generated, e.g., 12818600553323 |
| Client Secret | Auto-generated |
| Client Type | CONFIDENTIAL |
| Redirect URIs | https://client.example.org/cb/example.com |
| Client Authentication Method | CLIENT_SECRET_BASIC |
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.| Parameter | Value |
|---|---|
| client_id | 12818600553323 |
| response_type | code |
| redirect_uri | https://client.example.org/cb/example.com |
- Verify that a client associated with the client ID
12818600553323is registered with the authorization server. - Check that the redirect URI
https://client.example.org/cb/example.commatches 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 the following curl command to make an authorization request to the Authlete API (Step 5):
- Replace
$satwith your actual bearer token. - For Windows, ensure PowerShell is used to handle environment variables (
$env:sat).
resultMessageprovides human-readable result of the request processing. (See also Interpreting Authlete’s result codes)actionindicates what the authorization server should do next.ticketis required for the authorization server to make a request to another API in the following step.
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 isjohn.s@example.com. - 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).
Use the following curl command to issue an authorization request to the Authlete API:
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.responseContentrepresents the content of the response from the authorization server.
/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).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).
/auth/token API to evaluate the request and generate the response.
Use the following curl command to exchange an authorization code for an access token using the Authlete API:
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.responseContentcontains the content of the response from the authorization server.
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.
Use the following curl command to introspect a token 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).