After users authenticate with Oten IDP, they're redirected back to your application with an authorization code. This step shows you how to handle the callback, validate parameters, and exchange the code for tokens.
📖 Need context? Check the Integration Flow Overview to see how this step fits into the complete process.
🎯 What You'll Learn
In this step, you will:
Handle the OAuth callback request
Validate state parameter and other security checks
Exchange authorization code for tokens
Handle errors and edge cases
Implement proper error handling and logging
🔄 Callback Flow Overview
🔍 Callback Request Structure
Successful Callback
When authentication succeeds, Oten redirects to your callback URL with:
Parameters:
code: Authorization code (10-minute expiry)
state: The state value you sent (for CSRF protection)
Error Callback
When authentication fails, the callback includes error information:
function validateState(receivedState, req) {
const storedState = req.session.oauthState;
// Clear stored state (one-time use)
delete req.session.oauthState;
if (!receivedState) {
throw new Error('Missing state parameter');
}
if (!storedState) {
throw new Error('No stored state found - possible session timeout');
}
if (receivedState !== storedState) {
throw new Error('Invalid state parameter - possible CSRF attack');
}
return true;
}
function validateAuthorizationCode(code) {
if (!code) {
throw new Error('Missing authorization code');
}
if (typeof code !== 'string') {
throw new Error('Invalid authorization code format');
}
// Basic length validation (codes are typically 20-512 characters)
if (code.length < 10 || code.length > 512) {
throw new Error('Authorization code length invalid');
}
// Check for valid characters (base64url)
if (!/^[A-Za-z0-9_-]+$/.test(code)) {
throw new Error('Authorization code contains invalid characters');
}
return true;
}