Interface IdTokenExtension
This interface defines the contract for automatic ID token resolution in SecurityContext. Implementations are responsible for:
- Token Exchange: Converting access tokens to ID tokens (e.g., via OAuth2 token exchange)
- Caching Logic: Determining when to return cached tokens vs. re-resolving
- Expiration Handling: Validating token expiration and triggering re-resolution when needed
Caching Strategy:
The extension receives the currently cached ID token (if any) and decides whether to:
- Return cached token: If it exists and is still valid
- Re-resolve token: If cached token is expired, missing, or otherwise invalid
SecurityContext, allowing implementations to
customize expiration checks, implement token refresh logic, or add custom validation rules.
Thread Safety:
Implementations must be thread-safe as the extension is registered globally via SecurityContext.registerIdTokenExtension(IdTokenExtension) but operates on thread-local tokens.
Multiple threads may call resolveIdToken(Token) concurrently.
Lifecycle:
- Registration: Extension is registered once at application startup via
SecurityContext.registerIdTokenExtension(IdTokenExtension) - Resolution: Called by
SecurityContext.getIdToken()when ID token is requested - Caching: Returned token is cached in thread-local
SecurityContext - Re-resolution: Called again on next
SecurityContext.getIdToken()if cached token expired
Usage Example (Spring Boot):
@Configuration
public class SecurityConfig {
@PostConstruct
public void registerExtensions() {
SecurityContext.registerIdTokenExtension(
new DefaultIdTokenExtension(tokenService, iasConfig)
);
}
}
Error Handling:
Implementations should handle errors gracefully and return null if resolution fails
(network errors, invalid tokens, missing configuration, etc.). SecurityContext will
propagate the null to callers, allowing them to handle missing ID tokens appropriately.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionresolveIdToken(Token cachedIdToken) Resolves an ID token from the current security context.
-
Method Details
-
resolveIdToken
Resolves an ID token from the current security context.This method is called by
SecurityContext.getIdToken()to lazily resolve ID tokens when needed. The implementation receives the currently cached ID token (if any) and decides whether to return it or resolve a new one.Caching Responsibility:
The implementation is responsible for:
- Checking cached token validity: Inspect
cachedIdTokenexpiration - Deciding whether to re-resolve: Return cached token if valid, otherwise resolve new token
- Token exchange: If re-resolution needed, exchange access token for ID token
Access Token Availability:
The access token is available via
SecurityContext.getToken(). If no access token exists, the implementation should returnnullsince token exchange is impossible.Return Value Handling:
- Non-null token: Cached in
SecurityContextfor subsequentSecurityContext.getIdToken()calls null: No caching occurs; subsequent calls will re-invoke this method
Thread Safety:
This method may be called concurrently from multiple threads. Implementations must be stateless or use proper synchronization.
- Parameters:
cachedIdToken- the currently cached ID token from thread-localSecurityContext, ornullif:- No ID token has been resolved yet for this thread
- The cached token was cleared via
SecurityContext.clearIdToken() - The security context was reset via
SecurityContext.setToken(Token)
- Returns:
- the resolved ID token (may be the cached token if still valid), or
nullif:- No access token is available in the security context
- Token exchange fails (network error, invalid configuration, etc.)
- The access token does not support ID token exchange
- Checking cached token validity: Inspect
-