<< back to Guides

๐Ÿ” Deep Dive into OAuth 2.0

OAuth 2.0 is the industry-standard protocol for authorization. It allows applications to securely access resources on behalf of a user, without sharing their credentials.


๐Ÿง  What is OAuth?

OAuth is a delegation protocol that lets users grant third-party applications limited access to their data, without giving away passwords.

Example: Allowing a website to access your Google Calendar or GitHub repos without sharing your login credentials.


๐Ÿงฉ Key Components

Component Description
Resource Owner The user who owns the data
Client The app requesting access
Authorization Server Issues access tokens
Resource Server Hosts protected data (API)

๐Ÿ” OAuth Grant Types (Flows)

1. ๐Ÿ” Authorization Code (with PKCE)

Recommended for web/mobile apps. Uses an intermediate code for higher security.

Flow:

  1. User logs in and authorizes.
  2. App gets authorization code.
  3. App exchanges the code for an access token.
// Example exchange request
POST /token
grant_type=authorization_code
code=abc123
redirect_uri=https://app.com/callback
client_id=client123
client_secret=secret456

๐Ÿ”’ PKCE (Proof Key for Code Exchange) adds extra protection for public clients like mobile apps.


2. ๐Ÿค– Client Credentials

Used for machine-to-machine communication. No user involved.

POST /token
grant_type=client_credentials
client_id=abc
client_secret=xyz

3. ๐Ÿงพ Resource Owner Password Credentials (Deprecated)

User provides username/password directly. Not recommended due to security risks.


4. ๐Ÿ”„ Refresh Token

Used to renew access tokens without re-prompting the user.

POST /token
grant_type=refresh_token
refresh_token=abc123
client_id=xyz

๐Ÿงช OAuth Tokens

Token Type Description
Access Token Used to access APIs
Refresh Token Used to get a new access token
ID Token (OIDC) Contains user identity info (JWT)

Tokens can be opaque or JWT-based.


๐Ÿ›ก๏ธ Security Best Practices

โœ… Always use PKCE for public clients (SPA, mobile)

โœ… Use HTTPS everywhere

โœ… Store tokens securely (never in localStorage)

โœ… Set short-lived access tokens + use refresh tokens

โœ… Implement scopes to limit access

โœ… Revoke tokens if suspicious behavior is detected


๐Ÿ†š OAuth vs OpenID Connect (OIDC)

OIDC adds:


๐Ÿš€ Popular Providers & Tools

Provider Docs
Google https://developers.google.com/identity
GitHub https://docs.github.com/en/developers/apps
Auth0 https://auth0.com/docs
Okta https://developer.okta.com
Keycloak https://www.keycloak.org

Tools:


๐Ÿ“š Further Reading


โœ… Summary

Topic Notes
Use Authorization Code Flow For apps with user interaction
Use PKCE For security in SPAs/mobile apps
Use Client Credentials For service-to-service auth
Use Refresh Tokens For long sessions
Never expose secrets In public clients or repos
Prefer OpenID Connect When identity is needed

<< back to Guides