Skip to main content
API/OS Reference
  1. Reference
  2. Home
Español English (Current)
📚 All Guides
  • 1. HTTP for REST APIs
    Fundamentals beginner
  • 2. REST Properly Understood
    Fundamentals beginner
  • 3. Designing a REST API from Scratch
    API Design intermediate
  • 4. OpenAPI as a Contract
    Documentation intermediate
  • 5. Authentication and Authorization in REST APIs
    Security intermediate
  • 6. OAuth 2.0 and OpenID Connect Without Shortcuts
    Security intermediate
  • 7. Practical API Security
    Security intermediate
  • 8. Errors, Retries, and Resilience
    Patterns intermediate
  • 9. API Versioning and Lifecycle
    Lifecycle intermediate
  • 10. Shadow APIs: Detection and Management Guide
    API Security intermediate
  • 10. API Observability
    Infrastructure intermediate
  • 2xx Success Status Codes
  • 401 Unauthorized vs 403 Forbidden
  • 404 Not Found vs 410 Gone
  • 429 Too Many Requests
  • 4xx Client Error Status Codes
  • 5xx Server Error Status Codes
  • Accept Header
  • Client-Server Architecture
  • Content-Type Header
  • DELETE Method
  • GET Method
  • HATEOAS (Hypermedia as the Engine of Application State)
  • HTTP Headers
  • HTTP Methods
  • HTTP Request
  • HTTP Response
  • HTTP Status Codes
  • Hypermedia
  • Idempotency
  • OPTIONS Method
  • PATCH Method
  • POST Method
  • PUT Method
  • Representation
  • Resource
  • REST
  • RESTful API
  • RPC
  • Safe Methods
  • SOAP
  • Statelessness
  • Uniform Interface
  • Unsafe Methods
  • URI vs URL vs URN
  • Versioning
  • Webhooks
  • YAML
  • AMQP
  • HTTP (HyperText Transfer Protocol)
  • HTTP/1.1
  • HTTP/2
  • HTTP/3
  • HTTPS (HTTP Secure)
  • Kafka
  • Long-Polling
  • MQTT
  • mTLS
  • Polling
  • QUIC
  • SSE (Server-Sent Events)
  • TCP
  • TLS
  • WebSockets
  • ABAC (Attribute-Based Access Control)
  • Audience (in tokens)
  • Claims (Registered/Public/Private)
  • Fine-Grained Authorization
  • PDP (Policy Decision Point)
  • PEP (Policy Enforcement Point)
  • Permissions
  • RBAC (Role-Based Access Control)
  • Caching
  • Pagination
  • Quota
  • Rate Limiting
  • Throttling
  • API Lifecycle
  • API Versioning
  • Backward Compatibility
  • Breaking Change
  • Date-Based Versioning
  • Deprecation
  • Deprecation Policy
  • Header-Based Versioning
  • Media Type Versioning
  • Query Parameter Versioning
  • Semantic Versioning
  • Sunset Header
  • URI Path Versioning
  • API Abuse
  • API Gateway
  • API Security
  • Backoff
  • Circuit Breaker
  • Error Handling
  • Logging
  • Metrics
  • Observability
  • Retry Logic
  • SLA / SLO / SLI
  • Timeout
  • Tracing
  • Zero Trust
  • API Contract
  • API Documentation
  • Code-First Design
  • Contract-First Design
  • CORS
  • JSON Merge Patch
  • JSON Patch
  • OpenAPI
  • OpenAPI 3
  • Request Schema
  • Response Schema
  • RFC 2616
  • RFC 3986
  • RFC 6749
  • RFC 6750
  • RFC 7159
  • RFC 7230-7235
  • RFC 7519
  • RFC 7807 / RFC 9457
  • RFC 8259
  • RFC 8414
  • RFC 8707
  • RFC 959
  • Schema
  • Swagger
  • Anti-Corruption Layer
  • BFF (Backend for Frontend)
  • Correlation ID
  • EDA (Event-Driven Architecture)
  • EIP (Enterprise Integration Patterns)
  • ESB (Enterprise Service Bus)
  • Message Broker
  • Orchestration vs Choreography
  • SOAP WSDL
  • XSD (XML Schema Definition)
  • API Catalog
  • API Discovery
  • API Governance
  • API Inventory
  • API Red Teaming
  • API Zombie
  • Shadow API
  • Shadow API Detection
Total 161
Categories 10
Back to API/OS
  1. Home
  2. OAuth 2.0

OAuth 2.0

Published: January 6, 2025 Updated: January 6, 2025

Table of Contents

  • Definition
  • Context
    • Key Concepts
  • Example
  • Code
    • Authorization Request
    • Token Exchange
    • Using Access Token
  • Diagram
  • Best Practices
  • Common Mistakes
  • Analogy
  • Security
    • Threats
    • Recommendations
  • Related Terms

Definition

OAuth 2.0 (Open Authorization 2.0) is an industry-standard authorization framework that enables third-party applications to obtain limited access to user accounts on an HTTP service without exposing user credentials.

Unlike OAuth 1.0, OAuth 2.0 focuses exclusively on authorization (not authentication), delegating the authentication process to separate protocols like OpenID Connect.

Context

OAuth 2.0 was designed to solve the problem of credential sharing. Before OAuth, users had to share their passwords with third-party apps to grant access to their data—a serious security risk.

Key Concepts

  • Resource Owner: The user who owns the data
  • Client: The application requesting access
  • Resource Server: The API hosting protected resources
  • Authorization Server: Issues access tokens after authenticating the user

Example

A typical OAuth 2.0 flow (Authorization Code Grant):

  1. User clicks “Login with Google” in App A
  2. App A redirects to Google’s authorization server
  3. User logs in and consents to requested permissions
  4. Google redirects back to App A with an authorization code
  5. App A exchanges the code for an access token
  6. App A uses the token to access Google APIs on behalf of the user

Code

Authorization Request

GET /authorize?response_type=code
    &client_id=abc123
    &redirect_uri=https://app.example.com/callback
    &scope=read:profile
    &state=xyz789 HTTP/1.1
Host: auth.example.com

Token Exchange

POST /token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=AUTH_CODE_HERE
&redirect_uri=https://app.example.com/callback
&client_id=abc123
&client_secret=secret456

Using Access Token

GET /api/user/profile HTTP/1.1
Host: api.example.com
Authorization: Bearer ACCESS_TOKEN_HERE

Diagram

sequenceDiagram
    participant User
    participant Client as Client App
    participant AuthServer as Authorization Server
    participant ResourceServer as Resource Server

    User->>Client: 1. Initiate login
    Client->>AuthServer: 2. Authorization request
    AuthServer->>User: 3. Login & consent
    User->>AuthServer: 4. Approve
    AuthServer->>Client: 5. Authorization code
    Client->>AuthServer: 6. Exchange code for token
    AuthServer->>Client: 7. Access token
    Client->>ResourceServer: 8. API request with token
    ResourceServer->>Client: 9. Protected resource

Diagram rendering failed

The diagram could not be rendered. Please try refreshing the page.

Best Practices

  1. Always use HTTPS - OAuth 2.0 requires TLS encryption
  2. Use PKCE (Proof Key for Code Exchange) for public clients
  3. Short-lived access tokens - Expire in 1 hour or less
  4. Refresh tokens for long sessions - Store securely
  5. Validate redirect URIs - Prevent authorization code interception
  6. Use state parameter - Prevent CSRF attacks
  7. Limit scopes - Request minimum necessary permissions
  8. Rotate client secrets - Regularly update credentials

Common Mistakes

  1. Using implicit flow - Deprecated, use Authorization Code + PKCE instead
  2. Storing tokens in localStorage - XSS vulnerability, use httpOnly cookies
  3. Not validating state parameter - CSRF risk
  4. Overly broad scopes - Principle of least privilege
  5. Exposing client secrets - Keep in backend only
  6. Not using refresh tokens - Poor UX with frequent re-auth

Analogy

OAuth 2.0 is like a valet key for your car:

  • You (resource owner) give a valet (client) a special key
  • The valet key (access token) can only drive the car, not open the trunk (limited scope)
  • The valet can’t make a copy of your master key (no password sharing)
  • The key expires after a few hours (token expiration)
  • You can revoke the key anytime (token revocation)

Security

Threats

  • Authorization code interception - Mitigate with PKCE
  • Token leakage - Use short-lived tokens + refresh rotation
  • CSRF attacks - Validate state parameter
  • Phishing - Educate users on legitimate auth screens
  • Replay attacks - Use nonces and token binding

Recommendations

  • Implement token rotation for refresh tokens
  • Use JWT for self-contained tokens with signature verification
  • Enable rate limiting on token endpoints
  • Monitor for anomalous access patterns
  • Implement token revocation lists

Related Terms

  • JWT (JSON Web Token)
  • OpenID Connect
  • PKCE (Proof Key for Code Exchange)
  • API Gateway
  • Access Control

Category: Authentication & Security Difficulty: Intermediate Last Updated: January 6, 2025

Also available in:

  • Español

About

The largest independent repository of REST API knowledge

Visit API/OS Main Site

Contact

  • [email protected]

Legal

  • Privacy Policy
  • Terms of Service

Advertising

Advertise your product or service to thousands of API developers.

Request Information

Premium advertising spaces available.

© 2026 API/OS Team. All rights reserved.

🍪 We Use Cookies

We use Google Analytics to understand how the site is used and improve it. We don't sell or share your data. Privacy Policy