Authorization Code Flow
The Authorization Code flow is the most commonly used OAuth 2.0 flow for web applications. It provides a secure way to authenticate users and obtain access tokens.Overview
This flow is designed for server-side applications where the client secret can be securely stored. The process involves redirecting the user to authenticate and authorize the application, then exchanging an authorization code for an access token.Flow Steps
- Authorization Request: The application redirects the user to the authorization server
- User Authentication: The user authenticates and authorizes the application
- Authorization Code: The authorization server redirects back with an authorization code
- Token Exchange: The application exchanges the authorization code for an access token
- Access Resources: Use the access token to access protected resources
Implementation
Prerequisites
Before starting, you’ll need:- Your application’s
client_idandclient_secret - A configured redirect URI (e.g.,
https://yourapp.com/callback) - The authorization endpoint:
https://login.camall.io/oauth/authorize - The token endpoint:
https://login.camall.io/oauth/token
Step 1: Generate PKCE Code Verifier and Challenge
For enhanced security, generate a PKCE code verifier and challenge:Step 2: Initiate Authorization Request
Redirect the user to the authorization endpoint with the following parameters:response_type=code: Indicates authorization code flowclient_id: Your application’s client IDredirect_uri: Where to redirect after authorizationstate: Random string to prevent CSRF attacksscope: Requested permissions (space-separated)code_challenge: PKCE code challengecode_challenge_method=S256: SHA256 hashing method
Step 3: Handle the Callback
After user authorization, Camall redirects to yourredirect_uri with the authorization code:
state parameter matches the one you sent to prevent CSRF attacks.
Step 4: Exchange Authorization Code for Access Token
Use the authorization code to request an access token:With PKCE (Recommended)
Without PKCE (Basic Flow)
grant_type=authorization_code: Specifies token exchange typecode: The authorization code received in the callbackredirect_uri: Must match the original requestclient_id: Your application’s client IDclient_secret: Your application’s client secretcode_verifier: The PKCE code verifier (when using PKCE)
Step 5: Use the Access Token
Make authenticated API requests using the access token:Step 6: Refresh the Access Token (Optional)
When the access token expires, use the refresh token to obtain a new one:Complete Example Script
Here’s a complete bash script demonstrating the entire flow:Security Best Practices
- Always use PKCE: Protects against authorization code interception attacks
- Validate state parameter: Prevents CSRF attacks
- Use HTTPS: All endpoints must use HTTPS in production
- Secure client secret: Never expose your client secret in client-side code
- Short-lived tokens: Access tokens should have limited lifetime
- Rotate refresh tokens: Request new refresh tokens with each refresh
- Validate redirect URIs: Only use pre-registered redirect URIs