Hi @robert12
Welcome to the Auth0 Community!
You are asking how to validate the intended API audience inside an Auth0 Post-Login Action, confirm whether the optional event.resource_server and event.request.query.audience fields are populated during Google social login, and identify the minimum secure correction for the errors CUSTOMER_RESOURCE_BINDING_INVALID and AUTHENTICATION_REJECTED.
[Summary]
Do not depend on optional event fields for authorization decisions. Read the requested audience defensively, fail closed when it is absent, and use the Auth0 logs to confirm which field values each login actually produces.
[Root Cause]
The optional fields are not guaranteed to be present for every connection and flow type, so an Action that assumes they are populated can reject or mis-scope a valid Google login. The errors reflect a mismatch between the requested audience and the resource server configuration, not a defect in the backend JWT validation.
[Solution]
1. Supported Method for Validating API Audience in Post-Login Actions:
The canonical, supported property to inspect the intended API audience in an Auth0 Post-Login Action (onExecutePostLogin) is:
const requestedAudience = event.resource_server?.identifier;
Key Considerations:
-
Optionality (undefined handling): event.resource_server is populated only when a valid API audience is requested that maps to a registered Resource Server in the tenant (or when a tenant Default Audience is configured). If an authorization request omits audience (e.g., standard OIDC login requesting only an ID token or /userinfo), event.resource_server is undefined. Accessing event.resource_server.identifier without optional chaining (?.) throws a runtime TypeError, causing Auth0 to fail closed with an unhandled extensibility error.
-
Denying Disallowed Audiences: Use api.access.deny(errorCode, userMessage) to halt the pipeline:
if (requestedAudience && requestedAudience !== EXPECTED_AUDIENCE) {
return api.access.deny('unauthorized_client_audience', 'Client is not authorized for this API audience.');
}
- Native Platform Controls: You can also complement or enforce audience boundaries natively via Client Grants / Per-App Authorization in the Auth0 Dashboard (Applications > APIs > [Your API] > Non-Interactive / Application Grants or Application Access Permissions), which reject unauthorized client-audience pairs at the authorization layer before token generation.
2. Availability of** event.resource_server vs. event.request.query.audience (Google OAuth):
- event.request.query.audience → During external IdP flows (Google OAuth), the user is redirected away to Google and returns to Auth0’s callback URL (
/login/callback). When onExecutePostLogin executes, event.request.query represents the query parameters of the incoming callback request (code, state), not the initial /authorize query parameters. Any logic checking event.request.query.audience will always see undefined during Google logins.
- event.resource_server → Auth0 captures the original
/authorize parameters in the internal authorization transaction session. Across the external Google OAuth redirect, Auth0 preserves the transaction state. Upon return, if audience was included in the initial /authorize request and matches a registered Resource Server, event.resource_server.identifier is reliably populated.
If your Action checks event.request.query.audience, or if it fails closed when event.resource_server is missing/undefined, Google OAuth transactions will fail while direct database logins might appear to behave differently.
3. Reviewing Tenant Authentication and Action Execution Logs
To pinpoint the exact failure point between Auth0 and your backend:
-
Auth0 Tenant Logs (Dashboard > Monitoring > Logs):
-
Action Real-Time Logs:
console.log('Post-Login Diagnostic:', {
connection: event.connection.name,
clientId: event.client.client_id,
resourceServer: event.resource_server,
roles: event.authorization?.roles
});
-
Backend vs. Auth0 Boundary Verification:
- If Auth0 tenant logs show an
s (Success Login) event, Auth0 issued the token successfully. In that case, CUSTOMER_RESOURCE_BINDING_INVALID and AUTHENTICATION_REJECTED are being generated downstream by your backend API during access token consumption (e.g., missing expected namespaced claims, audience mismatch, or customer role binding failure).
4. Minimum Secure Correction (Preserving Role Isolation)
To fix the issue safely without weakening customer/admin boundary isolation:
- Defensive Audience and Role Evaluation: Ensure the Action safely checks
event.resource_server?.identifier without assuming it is always present, while enforcing that customer tokens cannot access admin audiences:
exports.onExecutePostLogin = async (event, api) => {
const targetAudience = event.resource_server?.identifier;
const userRoles = event.authorization?.roles || [];
const CUSTOMER_API_AUDIENCE = 'https://api.example.com/customer';
const ADMIN_API_AUDIENCE = 'https://api.example.com/admin';
// 1. If an API audience was targeted in the request
if (targetAudience) {
if (targetAudience === CUSTOMER_API_AUDIENCE) {
if (!userRoles.includes('customer')) {
return api.access.deny('CUSTOMER_RESOURCE_BINDING_INVALID', 'User lacks customer role for target API.');
}
} else if (targetAudience === ADMIN_API_AUDIENCE) {
if (!userRoles.includes('admin')) {
return api.access.deny('ADMIN_RESOURCE_BINDING_INVALID', 'User lacks admin role for target API.');
}
} else {
return api.access.deny('UNAUTHORIZED_AUDIENCE', 'Target audience not allowed.');
}
// 2. Inject role/binding claims into the Access Token using a custom namespaced claim
api.accessToken.setCustomClaim('https://api.example.com/roles', userRoles);
} else {
// 3. Handle pure OIDC (ID-token only) requests if your client allows basic login without API access.
// If API audience is strictly mandatory for all interactive logins from this client:
// return api.access.deny('MISSING_AUDIENCE', 'API audience is required for this client.');
}
};
-
Frontend Authorization Request Check:
- Verify that your client application explicitly passes
audience: 'https://api.example.com/customer' in its initial /authorize call (or Auth0 SDK initialization), not just scope or connection parameters.
-
Exact String Equality:
- Confirm there is no mismatch (such as a trailing slash
/) between the registered Auth0 API identifier, the client’s audience parameter, and the constant in your Action.
Kind Regards,
Nik