Almost every developer who adds "Sign in with Google" to an app ends up asking the same question: am I using OAuth or OpenID Connect? The honest answer is usually both, and the confusion is understandable, because they share endpoints, flows and even the word "token". Yet they answer different questions.

OAuth 2.0 answers: what is this application allowed to do? It is an authorization framework. OpenID Connect (OIDC) answers: who is this user? It is an authentication layer built on top of OAuth 2.0.

Think of a hotel. The keycard that the front desk gives you opens your room, the gym and the pool. It says nothing about who you are. Anyone who holds the card can use it, and the door does not care about your name. That is an OAuth access token: a permission slip. When you check in, the clerk looks at your passport and writes your name in the register. That is OpenID Connect: a statement about identity, issued by someone who checked. In real systems you receive both at the same moment, which is why people mix them up.

This guide explains how each one works, how the two tokens differ, how the authorization code flow with PKCE protects a login, how to debug tokens, and which mistakes cause the most security problems. If you are also deciding between cookies and tokens for the app itself, read our guide to session vs token authentication.

What is OAuth 2.0?

OAuth 2.0 (defined in RFC 6749) lets a user give an application limited access to their data on another service, without giving that application their password. It defines four roles:

  • Resource owner: the user, who owns the data.
  • Client: the application that wants access, such as your web app or mobile app.
  • Authorization server: the service that authenticates the user, asks for consent and issues tokens.
  • Resource server: the API that holds the data and accepts access tokens.

The result of an OAuth flow is an access token. The client sends it to the resource server in a header, and the server decides what the token allows.

GET /orders HTTP/1.1
Host: api.example.com
Authorization: Bearer <access token>

Scopes and token formats

A scope names a permission, such as orders:read or calendar.events. The user sees the requested scopes on the consent screen, and the token carries only what was granted. Keep scopes narrow, because they limit the damage if a token leaks.

The standard does not say what an access token looks like. There are two common shapes:

  • Opaque token: a random string with no meaning to the client. The API validates it by asking the authorization server (token introspection). This allows immediate revocation.
  • JWT access token: a signed JSON Web Token that the API can check locally, with no network call. RFC 9068 describes a profile for this. It is fast and scales well, but is harder to revoke before it expires.

A JWT access token typically has a payload like this. Note that aud names the API, not your app:

{
  "iss": "https://login.example.com",
  "sub": "usr_91c4e8",
  "aud": "https://api.example.com",
  "scope": "orders:read",
  "client_id": "client-7f3a",
  "iat": 1793000000,
  "exp": 1793003600
}

Why OAuth 2.0 alone is not a login system

Early on, many apps used OAuth to log users in: get an access token, call the provider's profile API, and treat success as "this person is logged in". It works in demos and fails in practice, for good reasons:

  • An access token is meant for the API. It is not addressed to your app, so your app cannot tell whether a token was issued for it or for a different app.
  • An attacker who tricks a user into authorising a malicious app can take that token and present it to your app. Your app sees a valid token and logs the attacker in as the victim. This is a token substitution attack.
  • There is no standard way to read the user's identity from a token. Every provider invented its own profile API.
  • Nothing says when or how the user logged in.

This is the pseudo-authentication problem, and it is the reason OpenID Connect exists.

What is OpenID Connect?

OpenID Connect (OIDC) is a thin identity layer on top of OAuth 2.0. It reuses the same roles, endpoints and flows and adds three things: a way to ask for identity, a standard token that proves it, and a standard way to fetch profile data.

  • The openid scope. A request that includes openid is an OIDC request. Without it, you are doing plain OAuth. Optional scopes such as profile, email, address and phone ask for groups of user claims.
  • The ID token. A signed JWT that states who the user is and how they authenticated, addressed to your app.
  • The UserInfo endpoint. An API that returns claims about the user when you call it with an access token that has the openid scope.
  • Discovery. A provider publishes its settings at /.well-known/openid-configuration, so that a client can find every endpoint and the signing keys automatically.

The ID token

OpenID Connect requires an ID token to be a JWT, so that your app can verify its signature and read the claims. A decoded example looks like this:

{"alg":"RS256","kid":"key-2026-10","typ":"JWT"}

{
  "iss": "https://login.example.com",
  "sub": "usr_91c4e8",
  "aud": "client-7f3a",
  "iat": 1793000000,
  "exp": 1793003600,
  "nonce": "n-0S6_WzA2Mj",
  "auth_time": 1792999950,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "email_verified": true
}
  • iss: the issuer. It must match the provider that you expect.
  • sub: the subject, a stable and unique user ID within that issuer. Use this as the user's key, not the email address.
  • aud: the audience, which must contain your own client_id.
  • iat and exp: issue and expiry time, in seconds since 1 January 1970 UTC.
  • nonce: a value that your app sent in the request, echoed back to bind the token to that login attempt and block replays.
  • auth_time: when the user actually authenticated, which is useful if you require a fresh login.

The provider's keys are published at its jwks_uri, and the header's kid tells you which key signed this token. The discovery document that lists all of this looks like this (shortened):

{
  "issuer": "https://login.example.com",
  "authorization_endpoint": "https://login.example.com/authorize",
  "token_endpoint": "https://login.example.com/oauth/token",
  "userinfo_endpoint": "https://login.example.com/userinfo",
  "jwks_uri": "https://login.example.com/.well-known/jwks.json",
  "scopes_supported": ["openid", "profile", "email"],
  "response_types_supported": ["code"],
  "id_token_signing_alg_values_supported": ["RS256"],
  "code_challenge_methods_supported": ["S256"]
}

ID token vs access token

This is the question behind most integration bugs, so keep it simple:

  • Purpose. The ID token tells your client who the user is. The access token lets your client call an API.
  • Audience. The ID token is addressed to your client (aud is your client_id). The access token is addressed to the resource server (aud is the API).
  • Who reads it. Your client reads the ID token. The API reads the access token. A client should treat the access token as opaque, even when it happens to be a JWT.
  • Format. The ID token is always a JWT. The access token can be opaque or a JWT.
  • Where it goes. Never send the ID token to an API as a credential. Send the access token.

OAuth 2.0 vs OpenID Connect at a Glance

  • Primary Purpose: OAuth 2.0: Authorization (what an app may access) | OpenID Connect: Authentication (who the user is)
  • Tokens Issued: OAuth 2.0: Access token, optionally a refresh token | OpenID Connect: ID token, plus an access token and optionally a refresh token
  • Token Format: OAuth 2.0: Not defined (opaque or JWT) | OpenID Connect: ID token is always a JWT
  • Scopes Used: OAuth 2.0: Custom, such as orders:read | OpenID Connect: Standard: openid, profile, email, address, phone
  • User Profile Endpoint: OAuth 2.0: None defined (every provider differs) | OpenID Connect: UserInfo endpoint, plus claims in the ID token
  • Security Focus: OAuth 2.0: Limited, revocable access for a client | OpenID Connect: Verifying identity (signature, issuer, audience, nonce)
  • Standard Flows: OAuth 2.0: Authorization code with PKCE, client credentials, refresh token | OpenID Connect: The same flows with the openid scope
  • Typical Use Cases: OAuth 2.0: Securing REST APIs, letting an app act on a user's data | OpenID Connect: Sign in with Google, single sign-on, reading user identity

Authorization code flow with PKCE explained

The old implicit flow returned tokens directly in the URL fragment of the redirect. That put tokens into browser history and into scripts, and it had no way to prove that the party redeeming the result was the party that started the flow. Current security guidance (the OAuth 2.0 Security Best Current Practice) tells you not to use it. The recommended flow, for single-page apps, mobile apps and increasingly for every client, is the authorization code flow with PKCE.

PKCE (Proof Key for Code Exchange, RFC 7636, pronounced "pixy") solves a specific problem. The redirect carries a short-lived authorization code. A public client such as a browser app or a mobile app cannot keep a client secret, so anyone who intercepts the code could redeem it. PKCE ties the code to a secret that only the original client knows:

  1. The client creates a random code_verifier.
  2. It sends only a hash of it, the code_challenge, in the authorization request.
  3. When it redeems the code, it sends the original verifier. The server hashes it and compares it with the stored challenge.

An attacker who steals the code does not have the verifier, so the code is worthless.

Client (SPA / mobile app)          Authorization server                       API
   |                                         |                          |
   | 1. random code_verifier                 |                          |
   |    code_challenge = BASE64URL(SHA256(code_verifier))                     |
   |                                         |                          |
   | 2. redirect user to /authorize?response_type=code&scope=openid...  |
   |    &state=...&nonce=...&code_challenge=...&code_challenge_method=S256 -->|
   |                                         | 3. user logs in and consents |
   |<-- 4. redirect to app: ?code=AUTH_CODE&state=... ---|                          |
   | 5. check that state matches             |                          |
   |                                         |                          |
   | 6. POST /token: code + code_verifier ->| 7. SHA256(verifier) equals   |
   |                                         |    stored challenge?         |
   |<-- 8. access_token + id_token (+ refresh_token) -|                         |
   | 9. verify the id_token                  |                          |
   |                                         |                          |
   | 10. GET /orders  Authorization: Bearer access_token --------------->|

The authorization request carries your parameters in the query string:

GET /authorize?response_type=code
  &client_id=client-7f3a
  &redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
  &scope=openid%20profile%20email
  &state=Xc9k2PqW7a
  &nonce=n-0S6_WzA2Mj
  &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
  &code_challenge_method=S256

In Node.js, creating the verifier and the challenge takes two lines. A verifier must have 43 to 128 characters from the URL-safe set, and 32 random bytes written in Base64URL give exactly 43:

const crypto = require('crypto');

const codeVerifier = crypto.randomBytes(32).toString('base64url');
const codeChallenge = crypto.createHash('sha256').update(codeVerifier).digest('base64url');

Then the code is redeemed with a form-encoded POST to the token endpoint. The verifier from step 1 goes with it:

POST /oauth/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=k4Zt9QpL2vRn
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
&client_id=client-7f3a
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

The response is JSON. With the openid scope, it contains an ID token next to the access token:

{
  "access_token": "SlAV32hkKG...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "8xLOxBtZp8...",
  "scope": "openid profile email",
  "id_token": "eyJhbGciOiJSUzI1NiIs..."
}

The OpenID Connect authentication flow, step by step

When your backend or app receives the ID token, validation is not optional. Check the signature with the key from the provider's jwks_uri, then check that iss is the expected issuer, that aud contains your client_id, that the token has not expired, and that nonce matches the one you sent. Only then do you trust sub as the identity. Most OpenID Connect libraries do all of this for you. Use one, and do not write the checks by hand.

A note on "Sign in with GitHub": as far as I know, GitHub's standard OAuth Apps do not return an ID token. You call its user API with the access token to learn who signed in, so it behaves like the pre-OIDC pattern described above. Google, Microsoft and most enterprise identity providers do implement OpenID Connect. Check the current documentation of the provider you use.

Inspecting and debugging OAuth and OIDC payloads

When a login fails or an API answers 401, work through the pieces in order.

  1. Check the token endpoint response. A single wrong character breaks a client parser. Paste the response into the JSON Validator to confirm that it is valid JSON and to find the exact position of a problem. Do the same with the /.well-known/openid-configuration document, especially when a hand-edited or proxied copy is involved.
  2. Decode the ID token. Paste it into the JWT Decoder to read the header and the claims. Look for an aud that is not your client ID, an iss with a different URL or a trailing slash, an exp that has passed, and a missing or wrong nonce. Convert exp and iat with the Timestamp Converter, and remember that they are in seconds. Decoding only reads the token. It does not verify the signature, so a readable token proves nothing by itself.
  3. Decode the access token if it is a JWT. Here aud should name the API, and scope should contain what the endpoint requires. If the token is not a JWT at all, it is opaque, and only the authorization server can explain it.
  4. Check the client authentication header. Confidential clients often authenticate at the token endpoint with HTTP Basic, where the header is the Base64 text of client_id:client_secret. For example, Authorization: Basic bXktY2xpZW50OnMzY3JldA== decodes to my-client:s3cret. Paste the part after the word Basic into the Base64 Encoder / Decoder to see what your client really sends. A very common bug is a stray newline or an extra colon in the value. The standard also says that the ID and the secret must be form-encoded before they are Base64-encoded, so a secret with special characters breaks clients that skip that step. If the decoder complains about the length of the text, our guide to the Base-64 length error explains the usual causes.

All three tools run in your browser, but never paste a live production token or client secret into any online tool. Use expired or test tokens. A secret that you have pasted somewhere should be rotated. Remember also that Base64 is an encoding and not encryption: anyone can decode that Basic header.

Common implementation mistakes

  • Sending the ID token to a backend API. The ID token is addressed to your client, so an API that accepts it can be fed an ID token that was issued for a different app. Send the access token to APIs, and make the API check aud, iss, exp and the scopes.
  • Skipping state validation. The state value ties the callback to the browser session that started the flow. If your app does not generate it and compare it on return, an attacker can make a victim's browser complete a flow with the attacker's authorization code (login CSRF).
  • Skipping nonce and the ID token checks. Without signature, issuer, audience, expiry and nonce validation, a stolen or forged token can be replayed.
  • Not using PKCE, or using the implicit flow, for a single-page or mobile app.
  • Loose redirect URIs. Register exact redirect URIs. A wildcard or a partial match lets an attacker redirect the code to a page that they control.
  • Storing tokens insecurely. Tokens in localStorage can be read by any script on the page. Prefer short-lived tokens in memory, keep refresh tokens out of JavaScript's reach, or use a backend that holds the tokens and gives the browser a session cookie. Our authentication guide compares the options.
  • Using the email address as the user key. Emails change and can be reused. Key your users by iss plus sub, and trust an email only when email_verified is true.

FAQs

Can I use OpenID Connect without OAuth 2.0?

No. OpenID Connect is an extension of OAuth 2.0. An OIDC login is an OAuth flow with the openid scope, and it uses the same authorization and token endpoints. You can use OAuth 2.0 without OpenID Connect when you only need access to an API and do not care who the user is.

Why does Google Sign-In return both an access token and an ID token?

They have different jobs. The ID token is for your app: it says who signed in. The access token is for calling Google APIs on the user's behalf, such as the UserInfo endpoint, and only with the scopes the user granted. If all you want is login, you can ask for the openid, profile and email scopes only, and use the ID token.

Should my backend API validate the ID token or the access token?

It depends on what your backend is doing. An API that protects resources validates the access token, and it must check that the audience is the API. Never accept an ID token there. A web backend that runs the login itself, by exchanging the code for tokens, validates the ID token once, at login time, and then creates its own session for the user. The two checks answer different questions: "may this caller use this API?" and "who just signed in?".

A rule of thumb

If you need to know who the user is, you need OpenID Connect. If you only need permission to call an API on someone's behalf, OAuth 2.0 is enough. In both cases, use the authorization code flow with PKCE, validate everything that you receive, and keep each token on the path it was made for: the ID token to your client, the access token to the API.