Common Authentication Flows
This section provides an overview and illustrative examples of common authentication flows used in modern web applications and services. Understanding these flows is crucial for implementing secure and robust authentication mechanisms.
1. Username and Password Authentication
The most traditional method. Users provide their credentials directly to the application. This is often used for initial login and may be combined with other methods for enhanced security.
POST /login HTTP/1.1
Host: example.com
Content-Type: application/json
{
"username": "user@example.com",
"password": "your_secure_password"
}
The server validates credentials and, if successful, issues a session token or cookie.
2. Token-Based Authentication (e.g., JWT)
After initial authentication, the server issues a token (like JSON Web Tokens - JWT) to the client. The client then includes this token in subsequent requests to prove its identity.

Flow:
- User logs in with credentials.
- Server validates credentials and generates a signed token (e.g., JWT).
- Server sends the token back to the client.
- Client stores the token (e.g., in localStorage or cookies).
- For subsequent requests, the client sends the token in the
Authorization
header:Authorization: Bearer <token>
. - Server verifies the token's signature and expiration.
3. OAuth 2.0 Authorization Code Flow
A widely used protocol for delegated authorization. It allows users to grant third-party applications access to their data on other services without sharing their credentials.

Simplified Steps:
- The application redirects the user to the authorization server.
- The user logs in and authorizes the application.
- The authorization server redirects the user back to the application with an authorization code.
- The application exchanges the authorization code (plus its client secret) for an access token and a refresh token from the authorization server.
- The application uses the access token to make API requests on behalf of the user.
4. OpenID Connect (OIDC) Flow
Built on top of OAuth 2.0, OIDC adds an identity layer, allowing clients to verify the identity of the end-user based on the authentication performed by an Authorization Server, as well as to obtain basic profile information about the end-user.
The most common OIDC flow for web applications is the "Authorization Code Flow with Proof Key for Code Exchange (PKCE)".
# Client generates code_verifier and code_challenge
curl -X POST "https://auth.example.com/oauth2/token" \
-d "grant_type=authorization_code" \
-d "client_id=YOUR_CLIENT_ID" \
-d "code=AUTHORIZATION_CODE_FROM_REDIRECT" \
-d "redirect_uri=YOUR_REDIRECT_URI" \
-d "code_verifier=CODE_VERIFIER_GENERATED_BY_CLIENT" \
-H "Content-Type: application/x-www-form-urlencoded"
The response typically includes an id_token
(JWT containing user information), an access_token
, and a refresh_token
.
Choosing the Right Flow
The choice of authentication flow depends on several factors:
- Application Type: Web app, mobile app, single-page application (SPA).
- Security Requirements: Sensitivity of the data being protected.
- User Experience: Ease of login and access.
- Third-Party Integrations: Need to access resources from other services.
Always refer to the specific documentation for the identity provider or service you are integrating with for detailed implementation guidelines.