OAuth 2.0 Basics
- Home
- OAuth 2.0 Basics
This page is for Authlete 3.0. For 2.x, refer to 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 usingcurlcommands, showing how an Authorization Server and Resource Server make API requests to Authlete for authorization, token issuance, and introspection. flowchart LR subgraph EndUser [End User] userAgent[“User Agent (N/A)”] resourceOwner[“Resource Owner (N/A)”] end subgraph APIClient [API Client] client[“Client (N/A)”] end subgraph APIServer [API Server] authServer[“Authorization Server (curl)”] resourceServer[“Resource Server (curl)”] authAdmin[“Authlete Administrator”] end subgraph Authlete [Authlete] mgmtConsole[“Authlete Management Console”] authAPI[“Authlete API”] end resourceOwner —> userAgent userAgent — “Access to Client” —> client userAgent — “Authorization Request (User Authentication and Consent)” —> authServer client — “Token Request” —> authServer client — “API Request” —> resourceServer %% API Server interactions with Authlete authAdmin — “Service/Client Management” —> mgmtConsole mgmtConsole — “API Request” —> authAPI authServer — “API Request” —> authAPI resourceServer — “API Request” —> authAPI 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.comAuthlete Management Console console.authlete.comAuthorization Server as.example.comClient client.example.orgResource 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.comService 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 CONFIDENTIALRedirect URIs https://client.example.org/cb/example.comClient Authentication Method CLIENT_SECRET_BASICWalk-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. sequenceDiagram autonumber participant RO as Resource Owner participant UA as User Agent participant C as Client participant AS as Authorization Server participant RS as Resource Server participant API as Authlete API RO ->> UA: Start UA ->> C: Make a request C —>> UA: Authorization request UA ->> AS: Forward the request AS ->> API: POST /auth/authorization API —>> AS: OK to proceed(including ticket) AS —>> UA: User authentication and consent page RO UA: Enter credentials UA ->> AS: Login and consent AS ->> API: POST /auth/authorization/issue(with ticket) API —>> AS: Authorization response content(including authorization code) AS —>> UA: Authorization response UA ->> C: Forward the response C ->> AS: Token request(with authorization code) AS ->> API: POST /auth/token API —>> AS: Token response content(including access token) AS —>> C: Token response C ->> RS: Access protected resource(with access token) RS ->> API: POST /auth/introspection API —>> RS: Introspection response RS —>> C: Response to client C —>> UA: Content UA —>> RO: EndAuthorization 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 coderedirect_uri https://client.example.org/cb/example.comThe authorization server is expected to receive the following content (folded for readability) as an HTTP GET query string from the user agent. - Verify that a client associated with the client ID 12818600553323 is 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. Authlete’s /auth/authorization API performs the above request validation on behalf of the authorization server. Use the followingcurlcommand to make an authorization request to the Authlete API (Step 5):
-H “Content-Type: application/json”
-d ”
Windows
curl.exe -X POST “https://us.authlete.com/api/933860280/auth/authorization”-H "Authorization: Bearer $env:sat"
-H “Content-Type: application/json” `
-d ”
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 followingcurl command to introspect a token using the Authlete API: