OAuth 2.0 and OpenID Connect: Fundamentals of Authorization and Authentication

Explains OAuth 2.0 authorization flows and OpenID Connect authentication layer with diagrams, covering access tokens, ID tokens, and security considerations.

Introduction

In web applications, user authentication (verifying identity) and authorization (granting permissions) are the most critical security functions.

  • Authentication: “Who are you?” → Verify user identity
  • Authorization: “What can you do?” → Control access to resources

OAuth 2.0 is a framework for authorization, while OpenID Connect (OIDC) is an authentication layer built on top of OAuth 2.0. This article explains how each works and the security considerations to keep in mind.

OAuth 2.0 Overview

Four Roles

OAuth 2.0 defines four roles:

RoleDescriptionExample
Resource OwnerOwner of the resourceEnd user
ClientApplication accessing the resourceWeb app, mobile app
Authorization ServerServer that issues tokensGoogle, Auth0
Resource ServerProvides protected resourcesAPI server

Client Registration

To use OAuth, you must register your client with the Authorization Server and obtain:

  • client_id: Client identifier (public)
  • client_secret: Client secret key (confidential, server-side only)
  • redirect_uri: URL to redirect after authorization

OAuth 2.0 Grant Types

Authorization Code Grant

The most common flow for web applications.

1. Client → Authorization Server: Authorization request
   GET /authorize?response_type=code
     &client_id=CLIENT_ID
     &redirect_uri=REDIRECT_URI
     &scope=read write
     &state=RANDOM_STATE

2. Resource Owner: Login & consent

3. Authorization Server → Client: Return authorization code
   302 Redirect to REDIRECT_URI?code=AUTH_CODE&state=RANDOM_STATE

4. Client → Authorization Server: Token exchange (back channel)
   POST /token
     grant_type=authorization_code
     &code=AUTH_CODE
     &redirect_uri=REDIRECT_URI
     &client_id=CLIENT_ID
     &client_secret=CLIENT_SECRET

5. Authorization Server → Client: Return access token
   { "access_token": "...", "token_type": "bearer", "expires_in": 3600 }

Authorization Code + PKCE

SPAs and mobile apps cannot securely store client_secret, so PKCE (Proof Key for Code Exchange) is used.

  1. Client generates a random code_verifier
  2. Computes code_challenge = BASE64URL(SHA256(code_verifier))
  3. Includes code_challenge in the authorization request
  4. Sends code_verifier during token exchange
  5. Authorization Server recomputes code_challenge from code_verifier to verify

This prevents token exchange even if the authorization code is intercepted.

Client Credentials Grant

Used for server-to-server (Machine-to-Machine) communication with no user involved.

POST /token
  grant_type=client_credentials
  &client_id=CLIENT_ID
  &client_secret=CLIENT_SECRET
  &scope=api:read

Deprecated Grant Types

  • Implicit Grant: Token returned in URL fragment, high leakage risk. Deprecated in favor of PKCE.
  • Resource Owner Password Credentials: User password passed directly to client, significant security concerns.

Access Tokens and Refresh Tokens

Access Tokens

Bearer tokens sent via the Authorization header in API requests.

GET /api/resource
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...
  • Expiration: Typically short-lived (15 minutes to 1 hour)
  • Scope: Limits accessible resources

Refresh Tokens

Used to obtain new access tokens without re-authentication when the current token expires.

POST /token
  grant_type=refresh_token
  &refresh_token=REFRESH_TOKEN
  &client_id=CLIENT_ID

OpenID Connect (OIDC)

What is OIDC?

OIDC is an authentication layer built on top of OAuth 2.0. While OAuth 2.0 handles “what can be accessed,” OIDC handles “who is the user.”

Adding scope=openid to the authorization request activates the OIDC flow.

ID Token (JWT)

OIDC issues an ID Token in addition to the access token. The ID Token is in JSON Web Token (JWT) format, consisting of three parts:

Header.Payload.Signature

Header:

{
  "alg": "RS256",
  "typ": "JWT",
  "kid": "key-id-123"
}

Payload (Standard Claims):

ClaimDescriptionExample
subUnique user identifier"user-123"
issToken issuer"https://auth.example.com"
audTarget client"client-id"
expExpiration (UNIX timestamp)1709942400
iatIssued at1709938800
nameUser name"Taro Yamada"
emailEmail address"taro@example.com"

Signature: Signs Header + Payload with a secret key to detect tampering.

UserInfo Endpoint

Additional user information can be retrieved using the access token.

GET /userinfo
Authorization: Bearer ACCESS_TOKEN

Security Considerations

State Parameter (CSRF Protection)

Include a random state value in the authorization request and verify it matches on redirect. This prevents CSRF attacks.

Mandatory PKCE

OAuth 2.1 is expected to require PKCE for all client types. Always use PKCE in new implementations.

Token Storage

Client TypeRecommended StorageNotes
Server-side web appServer sessionReference via HttpOnly Cookie
SPAIn-memory (variable)localStorage is discouraged (XSS risk)
Mobile appOS-provided secure storageKeychain / Keystore

redirect_uri Validation

The Authorization Server must verify exact match with pre-registered redirect_uri. Allowing wildcards or partial matches creates open redirect attack risks.

Summary

AspectOAuth 2.0OpenID Connect
PurposeAuthorization (resource access control)Authentication (user identity verification)
Primary tokenAccess tokenID Token (JWT)
Scopesread, write, etc.openid, profile, email, etc.
Use caseAPI protectionSSO, login

References