OAuth is an open standard for authorization that allows a user to grant a third-party application limited access to their resources on another service, without sharing their password. It works by using tokens issued by an authorization server, which the third-party app presents to the resource server to access specific data or actions on the user's behalf.
Background: Why OAuth Was Created
Before OAuth, a common but insecure pattern was for applications to ask users for their username and password directly. For example, a photo-printing service might ask for your social media credentials so it could download your photos. This gave the third-party app full access to your account, including the ability to change your password or post on your behalf. If the app was compromised, your credentials were exposed.
OAuth was designed to solve this problem by introducing a delegation model. Instead of sharing passwords, users authorize a third-party app to obtain a token that grants limited access to specific resources. The token can be revoked without changing the password, and the access scope can be restricted. OAuth 1.0 was published in 2007, and OAuth 2.0, the version most widely used today, was standardized in 2012. It is important to note that OAuth is an authorization framework, not an authentication protocol; it tells a service what a client can do, not who the user is. For authentication, OpenID Connect (OIDC) is often layered on top of OAuth 2.0.
Key Roles in OAuth
OAuth defines four main roles that interact during the authorization process:
- Resource Owner: Typically the user who owns the data and can grant access to it.
- Client: The application requesting access to the resource owner's data (e.g., a mobile app or website).
- Authorization Server: The server that authenticates the resource owner and issues access tokens to the client after obtaining authorization.
- Resource Server: The server hosting the protected resources, which accepts and validates access tokens to serve the requested data.
These roles can be hosted by the same entity or separated. For instance, in a social media context, the authorization server and resource server might both be operated by the social media platform, while the client is a third-party app.
How OAuth Works: Step by Step
The core OAuth flow, often called the Authorization Code flow, involves several steps. Here is a typical sequence for a web application:
- Authorization Request: The client redirects the resource owner (user) to the authorization server's authorization endpoint. The request includes the client's identifier, requested scopes (permissions), redirect URI, and state parameter (for CSRF protection).
- User Authentication and Consent: The authorization server authenticates the user (if not already logged in) and presents a consent screen showing the requested permissions. The user approves or denies.
- Authorization Grant: If approved, the authorization server redirects the user back to the client's redirect URI with an authorization code.
- Token Request: The client's backend exchanges the authorization code for an access token by making a request to the authorization server's token endpoint. This request includes the client's credentials (client ID and secret) and the authorization code.
- Access Token Issued: The authorization server validates the code and client credentials, then issues an access token (and often a refresh token) to the client.
- Resource Access: The client uses the access token to make requests to the resource server. The resource server validates the token and returns the requested data if the token is valid and has the necessary scope.
This flow is considered secure because the access token is never exposed to the user's browser, and the client secret is kept confidential on the server. For mobile and single-page applications, the Authorization Code flow with PKCE (Proof Key for Code Exchange) is recommended to prevent authorization code interception.
Tokens and Scopes
OAuth uses tokens as the credentials for accessing protected resources. The most common types are:
- Access Token: A credential that grants access to specific resources. It is typically short-lived (e.g., minutes to hours) and can be a bearer token, meaning anyone holding it can use it. Hence, it must be protected in transit and storage.
- Refresh Token: A long-lived credential used to obtain new access tokens after the current one expires. Refresh tokens are optional and are typically issued to confidential clients (like web servers) that can keep them secure.
Scopes are strings that define the specific permissions being requested. For example, a scope might be read:profile or write:posts. The authorization server decides which scopes to grant based on user consent and policy. The client should request only the scopes it needs, following the principle of least privilege.
Common OAuth Flows and When to Use Them
OAuth 2.0 defines several grant types (flows) for different client types and use cases:
- Authorization Code: Best for server-side web applications where the client can keep a secret. It involves a two-step process: obtaining an authorization code, then exchanging it for an access token.
- Authorization Code with PKCE: Recommended for mobile apps and single-page applications (SPAs) that cannot securely store a client secret. PKCE adds a dynamically created code verifier and challenge to prevent code interception.
- Client Credentials: Used for machine-to-machine communication where the client acts on its own behalf, not on behalf of a user. The client authenticates with its own credentials to obtain an access token.
- Implicit: Historically used for SPAs but now deprecated due to security weaknesses (token exposure in URL fragments). It is not recommended.
- Resource Owner Password Credentials: Allows the client to collect the user's username and password directly and exchange them for a token. This should only be used in highly trusted scenarios (e.g., first-party apps) and is generally discouraged.
Choosing the right flow depends on the client type and security requirements. For modern applications, the Authorization Code flow with PKCE is the most widely recommended.
Security Considerations and Common Pitfalls
OAuth is powerful but requires careful implementation. Common security issues include:
- CSRF Attacks: Using the
stateparameter in authorization requests helps prevent cross-site request forgery. - Token Leakage: Access tokens must be transmitted over HTTPS and stored securely. Avoid exposing tokens in URLs or logs.
- Scope Creep: Requesting more permissions than needed increases risk. Always request minimal scopes.
- Redirect URI Validation: The authorization server must strictly validate redirect URIs to prevent open redirector attacks.
- Token Revocation: Implement token revocation endpoints and allow users to revoke access.
Additionally, OAuth 2.0 does not provide authentication by itself. To verify a user's identity, use OpenID Connect, which adds an ID token (a signed JWT) containing user information.
Frequently Asked Questions
Is OAuth the same as OpenID Connect?
No, OAuth is an authorization framework, while OpenID Connect is an authentication layer built on top of OAuth 2.0. OpenID Connect adds an ID token and user info endpoint to verify identity.
Can OAuth be used without HTTPS?
OAuth 2.0 requires HTTPS for all token and authorization endpoints to prevent token interception. Using it without HTTPS is insecure and not compliant with the specification.
What is a bearer token?
A bearer token is an access token that grants access to whoever possesses it, without further identification. It must be protected like a password.
How long do access tokens last?
Access token lifetimes vary by provider, typically ranging from minutes to a few hours. Refresh tokens can be used to obtain new access tokens without user interaction.
Is OAuth only for web applications?
No, OAuth is used across web, mobile, desktop, and IoT applications. The appropriate flow depends on the client's capabilities and security constraints.
What is PKCE and why is it important?
PKCE (Proof Key for Code Exchange) is an extension to OAuth that mitigates authorization code interception attacks, especially for public clients like mobile apps and SPAs.