Suppose you want a scheduling application to find free time on your Google Calendar. The application needs permission to read your calendar, but it does not need your Google password or access to your email. You also want to withdraw that permission when you stop using the application.
OAuth provides this kind of limited access without sharing passwords. OpenID Connect builds on OAuth to let you sign in to an application using an account at another service. This is a high-level overview of the two flows and the reasons behind their design. We will focus on the authorization code flow, using the scheduling application as our example.
Why We Need OAuth
One way to give the scheduling application access would be to give it your Google password. The application could then log in as you. That creates several problems: it has the credential for your entire account, must protect that credential, and has access far beyond reading your calendar and for an unlimited time. To withdraw access, you would have to change your password, disrupting other applications that use it too.
OAuth replaces password sharing with delegated authorization: you give an application permission to perform specific operations on your behalf. The service authenticates you and handles approval. The application receives a separate credential, called an access token, for the approved access.
This separation gives each application its own access grant. You can disconnect the scheduling application without changing your password or disconnecting other applications. The first version of OAuth appeared in 2007. OAuth 2.0, published in 2012, is the framework in use today.
The Roles in OAuth
OAuth divides the work among four roles:
-
The resource owner is the user who controls the data.
-
The client is the application requesting access.
-
The authorization server authenticates the user, handles authorization, and issues tokens.
-
The resource server provides the data or operations through an API and checks access tokens.
In our example, you are the resource owner, the scheduling application is the client, Google’s accounts system is the authorization server, and the Calendar API is the resource server.
The authorization server and resource server have different jobs, even if the same company operates both. One handles login and permissions, and the other handles requests for data. A token for reading Calendar does not grant access to Gmail or Drive.
From your side, the exchange is simple. You click “Connect Google Calendar” in the scheduling application, and your browser is sent to Google. You sign in there, on Google’s own pages, and Google shows you a consent screen: a page listing the permissions the application is asking for, which you approve or deny. Your browser then returns to the application. The sections below explain what each part of that exchange accomplishes.
Scopes: Defining the Requested Access
A scope names a permission, such as reading calendar events or creating them. The application requests the scopes its task needs, and the authorization server determines which permissions to grant.
For the scheduling application, reading calendar events is enough to find free time. Creating or deleting events would require additional permission. This is the Principle of Least Privilege in action: grant only the access needed for the task.
The consent screen translates scope names into descriptions the user can understand. The user approves access at the service that controls the account, rather than trusting a promise made by the requesting application. Approval gives the application real access, so a misleading application can still misuse permissions the user grants it.
Registering the Application
Before our scheduling application requests access, its developer registers it with Google. Registration identifies the application and its redirect URI, the address where Google will return the browser after authorization.
Google assigns the application a client ID, a public identifier that tells Google which application is making the request. Our example uses an application with its own server. For such applications, Google also issues a client secret at registration, and the developer stores it on the application’s server. The secret authenticates the application when it requests tokens. It belongs to the application and is separate from any user’s password.
The registered redirect URI controls where Google will send the authorization response. When the application later makes an authorization request, Google will compare the address in the request against the registered one and refuse a mismatch, so an attacker cannot substitute a destination that would receive the authorization code.
The Authorization Code Flow
You are using the scheduling application, and it needs to connect to your Google Calendar for the first time. The authorization code flow runs as follows:
-
You start the connection. You click “Connect Google Calendar” in the scheduling application.
-
The application sends you to Google. It redirects your browser to Google’s authorization page. The request identifies the application, its redirect URI, and the calendar permissions it wants. It also includes a value used by PKCE, a protection explained below.
-
Google handles login and approval. You sign in to Google and approve the requested calendar access. Google then redirects your browser to the application’s registered redirect URI with a short-lived, single-use authorization code. If you deny access, the application receives an error instead.
-
The application exchanges the code for a token. Its server sends the code directly to Google’s token service over HTTPS. It authenticates with its client secret and supplies the PKCE proof. Google checks the code and the application’s proof, then returns an access token.
-
The application requests your calendar data. It sends the access token to the Calendar API. The API checks that the token is valid for that API and permits the requested operation before returning the data.
Your Google password never reaches the scheduling application. You enter it on Google’s pages. If you are already signed in to Google, Google uses that existing session. Previously approved permissions also let the service avoid asking for the same consent again.
The access token lets the application make subsequent API requests without sending you through the approval process for each request.
Why a Code, Then a Token?
The browser carries the authorization code back to the application. Browser redirects expose information in URLs, which can appear in browser history and logs. Sending a reusable access token along that route would expose a credential that grants access to the user’s data. These browser redirects are the front channel. The direct HTTPS connection from the application’s server to Google’s token service is the back channel.
The authorization code flow puts a temporary code in the redirect instead. The code cannot be used to read the calendar. It must be exchanged for an access token, expires quickly, and works only once. In our server-based example, the token travels directly from Google’s token service to the application’s server over HTTPS.
The exchange also requires proof from the application. The client secret authenticates the registered application, and PKCE ties the exchange to the particular authorization request. Stealing the code alone is therefore insufficient to obtain the token.
Earlier versions of OAuth allowed the access token itself to be delivered in the browser redirect. That approach is deprecated. Older tutorials that show it should not be followed.
PKCE: Tying the Code to the Request
Proof Key for Code Exchange, or PKCE, adds a proof that is different for each authorization attempt:
-
The client generates a random secret called a code verifier and keeps it for this attempt.
-
It hashes the verifier to produce a code challenge and includes the challenge in the initial authorization request.
-
When exchanging the authorization code, it sends the original verifier to the authorization server over HTTPS.
-
The server hashes the verifier and checks that it matches the challenge from the original request.
An attacker who steals the code and sees the challenge still lacks the verifier needed to complete the exchange. The hash lets the client commit to a secret before authorization and prove possession of it afterward.
PKCE is essential for mobile applications and applications that run entirely in the browser. Their distributed code cannot protect a shared client secret. These applications use PKCE without relying on a client secret. Server applications use PKCE alongside their client authentication to protect each authorization attempt.
Access Tokens and Refresh Tokens
The access token is the credential presented to the resource server. Its permissions and expiration limit the access it grants. A short lifetime reduces how long a stolen token remains useful.
OAuth does not define a format for access tokens; each provider chooses its own. To the client, the format is irrelevant: the token is a string to present with each API request, and the client never looks inside it. The format concerns the resource server, which must validate the token. Some providers issue an opaque string, which the resource server checks with the authorization server. Others issue a structured, signed token that the resource server can validate on its own. The common structured format is the JSON Web Token, or JWT, but not every access token is a JWT.
A refresh token lets the client obtain new access tokens from the authorization server without asking the user to approve access again. Refresh tokens are optional. They support continued access after an access token expires and must be protected as long-term credentials. The client sends a refresh token to the authorization server, never to the Calendar API.
Tokens require protection in storage and in transit. Access tokens are bearer tokens: possession is enough to use one, so an attacker who steals a token can exercise its permissions. HTTPS protects delivery, while expiration and revocation limit continued use. Disconnecting an application withdraws its authorization and stops it from obtaining new access tokens. An existing access token’s remaining lifetime and the service’s revocation checks determine when that token stops working.
Why OAuth Alone Is Not Login
Google authenticated you during the flow, but the scheduling application received permission to read a calendar. Those are different results.
An access token is intended for an API. OAuth does not define a standard message telling the client which user authenticated for that client. Reading a name or email address from a profile API does not supply that missing login protocol. The application needs a trustworthy response identifying the user, naming the intended client, and tying the response to the login attempt it started.
OpenID Connect supplies that response.
OpenID Connect: Signing In Through Another Service
OpenID Connect, or OIDC, adds authentication to OAuth 2.0. The service that authenticates the user is called the identity provider. In “Sign in with Google,” Google is the identity provider, and the website trusts Google to authenticate the user.
OIDC uses the authorization code flow we just covered. The client includes the special scope openid in its request to ask for an OpenID Connect login. The token exchange then returns an ID token in addition to an access token.
The ID token is a signed JWT that identifies:
-
The identity provider that issued it.
-
The user, through an identifier assigned by that provider.
-
The client it was issued for, called its audience.
-
When the token was issued and when it expires.
The client verifies the signature using the trusted provider’s public keys and checks the issuer, audience, expiration, and connection to its login request. A valid signature alone is insufficient: a token issued for another website must be rejected.
The Login Flow
Suppose the scheduling website offers “Sign in with Google” to create or access an account on the website:
-
You click “Sign in with Google.”
-
The website redirects your browser to Google with an OpenID Connect request that includes the
openidscope and a PKCE challenge. -
Google authenticates you, obtains the required approval, and returns an authorization code through your browser.
-
The website’s server exchanges the code and PKCE verifier for tokens, then validates the ID token.
-
The website uses the provider’s identity and user identifier to find or create your local account. It establishes its own login session, represented in your browser by a session cookie.
The website now knows which user is signing in. It does not receive your Google password. Google handles authentication, including its password policy and multifactor authentication.
The openid scope requests authentication. The profile and email scopes request additional information about the user. That information is supplied in the ID token or through OIDC’s user-information API. Signing in does not itself grant permission to read your calendar; calendar access requires its own scopes.
Keeping the Tokens Distinct
| Credential | Purpose | Where it is used |
|---|---|---|
| Authorization code | Is exchanged once for tokens. | The authorization server’s token service. |
| Access token | Authorizes API requests. | The resource server. |
| Refresh token | Obtains new access tokens. | The authorization server’s token service. |
| ID token | Identifies the authenticated user to the client. | The application signing the user in. |
The ID token tells the website who authenticated. The access token grants access to an API. The website’s own session keeps the user signed in afterward.
Federated login concentrates trust in the identity provider. The provider learns which applications request login, and compromise of the provider account threatens access to applications that trust it. Losing access to that account also makes those applications’ account-recovery procedures important.