OAuth 2.0 is the gold standard for delegated authorization. It provides multiple flows to suit different application types and trust levels. This guide dives deep into:
Recap the AuthN and AuthZ
OAuth 2.0 Flows: Client Credentials, Authorization Code, and Authorization Code with PKCE
Client types: Confidential and Public
Sequence diagrams for each flow (compatible with https://www.websequencediagram.com)
Keycloak configuration examples for each flow
curl
commands to request tokens from KeycloakSecurity concerns, best practices, and when to use what
๐ What is Authentication and Authorization?
Concept Meaning Example Authentication Verifying who you are. Logging in with username/password, or Google login to prove your identity. Authorization Verifying what you can do or access after authentication. Can this user access /admin
page or update a record after logging in?
Concept | Meaning | Example |
---|---|---|
Authentication | Verifying who you are. | Logging in with username/password, or Google login to prove your identity. |
Authorization | Verifying what you can do or access after authentication. | Can this user access /admin page or update a record after logging in? |
๐งญ Purpose and Use
Aspect Authentication Authorization Goal Prove identity Control access to resources Happens when? First step After authentication Protocol Examples OpenID Connect (on top of OAuth 2) OAuth 2.0 (Authorization Framework) Who uses the data? Login system (e.g., Keycloak) Backend/API/gateway with access policies Typical Data Username, password, biometrics Roles, permissions, scopes
Aspect | Authentication | Authorization |
---|---|---|
Goal | Prove identity | Control access to resources |
Happens when? | First step | After authentication |
Protocol Examples | OpenID Connect (on top of OAuth 2) | OAuth 2.0 (Authorization Framework) |
Who uses the data? | Login system (e.g., Keycloak) | Backend/API/gateway with access policies |
Typical Data | Username, password, biometrics | Roles, permissions, scopes |
๐ ️ Which Protocol Does What?
Flow or Protocol Used For Handles Authentication? Handles Authorization? OAuth 2.0 Delegated access ❌ No ✅ Yes OpenID Connect (OIDC) Identity layer on OAuth ✅ Yes (who the user is) ✅ Sometimes (via scopes/claims) SAML Enterprise SSO ✅ Yes ✅ Yes Basic Auth / Form Login Simple login systems ✅ Yes ❌ No
Flow or Protocol | Used For | Handles Authentication? | Handles Authorization? |
---|---|---|---|
OAuth 2.0 | Delegated access | ❌ No | ✅ Yes |
OpenID Connect (OIDC) | Identity layer on OAuth | ✅ Yes (who the user is) | ✅ Sometimes (via scopes/claims) |
SAML | Enterprise SSO | ✅ Yes | ✅ Yes |
Basic Auth / Form Login | Simple login systems | ✅ Yes | ❌ No |
๐ In OAuth 2.0 Context
-
Authentication: Usually handled by OIDC or a login form in the Identity Provider (IdP) like Keycloak.
-
Authorization: Managed through OAuth 2.0 scopes, roles, or resource server policies.
Authentication: Usually handled by OIDC or a login form in the Identity Provider (IdP) like Keycloak.
Authorization: Managed through OAuth 2.0 scopes, roles, or resource server policies.
๐ 1. What is an OAuth Client?
An OAuth client is an application requesting access to protected resources on a user's behalf or on its own behalf.
๐ ️ 2. Client Types: Confidential vs Public
Type | Can Store Secrets? | Typical Examples |
---|---|---|
Confidential | Yes | Server-side apps, CLIs |
Public | No | Mobile apps, SPAs |
✅ 3. Client Credentials Flow (Confidential Client)
Use Case: Service-to-service or machine-to-machine communication (no end-user).
๐งพ Sequence Diagram
title Client Credentials Flow (Confidential Client)
Client->Auth Server: POST /token\nclient_id + client_secret\ngrant_type=client_credentials
Auth Server->Client: 200 OK\naccess_token
Client->Resource Server: GET /protected-resource\nAuthorization: Bearer access_token
Resource Server->Client: 200 OK\nprotected data
๐ง Keycloak Setup
-
Go to Clients → Create a new client
-
Client ID:
my-service-client
-
Client Type: Confidential
-
Enable
Service Accounts Enabled
-
Set credentials and copy
client_id
&client_secret
-
Assign appropriate client roles under
Service Account Roles
-
Token Endpoint:
https://<keycloak-host>/realms/<realm>/protocol/openid-connect/token
๐♂️ 4. Authorization Code Flow (Confidential Client)
Use Case: Web applications with backend able to keep secrets. Requires user login.
๐งพ Sequence Diagram
title Authorization Code Flow (Confidential Client)
Client->User: Redirect to Auth Server login
User->Auth Server: Logs in, grants consent
Auth Server->Client: Redirect with code
Client->Auth Server: POST /token\ncode + client_id + client_secret
Auth Server->Client: 200 OK\naccess_token + refresh_token
Client->Resource Server: GET /protected-resource\nAuthorization: Bearer access_token
Resource Server->Client: 200 OK\nprotected data
๐ง Keycloak Setup
-
Go to Clients → Create a new client
-
Client ID:
my-web-client
-
Client Type: Confidential
-
Root URL:
https://your-app.com
-
Valid Redirect URIs:
https://your-app.com/callback
-
Enable
Standard Flow Enabled
-
Note the token endpoint:
https://<keycloak-host>/realms/<realm>/protocol/openid-connect/token
๐ฑ 5. Authorization Code Flow with PKCE (Public Client)
Use Case: Mobile apps or SPAs where secrets cannot be stored securely.
๐งพ Sequence Diagram
title Authorization Code Flow with PKCE (Public Client)
Client->Auth Server: Redirect with\ncode_challenge (PKCE)
User->Auth Server: Logs in, grants consent
Auth Server->Client: Redirect with code
Client->Auth Server: POST /token\ncode + code_verifier (PKCE)
Auth Server->Client: 200 OK\naccess_token
Client->Resource Server: GET /protected-resource\nAuthorization: Bearer access_token
Resource Server->Client: 200 OK\nprotected data
๐ง Keycloak Setup
-
Go to Clients → Create a new client
-
Client ID:
my-spa-client
-
Client Type: Public
-
Enable
Standard Flow Enabled
-
Set Valid Redirect URIs (e.g.
http://localhost:3000/*
) -
Enable
PKCE
(enabled by default from Keycloak 18+) -
Do not set client secret (public clients should not use one)
๐ก️ 6. Security Considerations
Risk | Applies To | Mitigation |
---|---|---|
Token theft | All flows | Use HTTPS, secure storage |
Secret leakage | Confidential | Store secrets in vaults, env vars |
Replay attacks | Public clients | Use PKCE with code_verifier |
Authorization code leakage | All code flows | Use state param + PKCE |
Refresh token misuse | All code flows | Issue only to confidential clients |
๐ 7. Which Flow to Use When?
Flow | Client Type | User Involved | Use Case |
---|---|---|---|
Client Credentials | Confidential | No | M2M, background jobs, microservices |
Authorization Code | Confidential | Yes | Web apps with secure backend |
Auth Code + PKCE | Public | Yes | SPAs, mobile apps |
๐ 8. Summary Table
Flow | Requires Secret | Safe for Public | Refresh Token | PKCE Required |
---|---|---|---|---|
Client Credentials | Yes | No | No | No |
Authorization Code | Yes | No | Yes | No |
Auth Code + PKCE | No | Yes | Sometimes | Yes |
๐ 9. Best Practices
-
Always use PKCE for mobile/web public clients
-
Use short-lived access tokens and rotating refresh tokens
-
Validate
state
andnonce
to prevent CSRF and replay -
Use scopes to enforce least privilege
๐ 10. Tools to Visualize Sequence Diagrams
All diagrams here are compatible with https://www.websequencediagram.com. Paste any of them to view and customize.
๐ Final Thoughts
Choosing the right OAuth flow depends on:
-
Whether you're authenticating a user or a service
-
Whether the client is trusted to hold secrets
-
Whether the platform supports secure storage
Use this guide as a blueprint to implement secure OAuth 2.0 integrations confidently using Keycloak as your Identity Provider.