-
Notifications
You must be signed in to change notification settings - Fork 131
XSUAA Token getter in SecurityContext #1889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a133a8a
get-Xsuaa-Token
NiklasHerrmann21 07c6b7c
get-Xsuaa-Token
NiklasHerrmann21 ebe3ef1
get-Xsuaa-Token
NiklasHerrmann21 14b2f06
get-Xsuaa-Token
NiklasHerrmann21 520bc2d
get-Xsuaa-Token
NiklasHerrmann21 c56b13c
get-Xsuaa-Token
NiklasHerrmann21 aa02725
get-Xsuaa-Token
NiklasHerrmann21 abcaf72
get-Xsuaa-Token
NiklasHerrmann21 e5ac1b8
get-Xsuaa-Token
NiklasHerrmann21 10e3f9e
get-Xsuaa-Token
NiklasHerrmann21 a20f411
Potential fix for code scanning alert no. 437: Insertion of sensitive…
NiklasHerrmann21 96aed2a
Potential fix for code scanning alert no. 438: Field masks field in s…
NiklasHerrmann21 2426004
get-Xsuaa-Token
NiklasHerrmann21 ac14899
get-Xsuaa-Token
NiklasHerrmann21 eeb66b4
get-Xsuaa-Token
NiklasHerrmann21 b1821cb
get-Xsuaa-Token
NiklasHerrmann21 732cc6f
get-Xsuaa-Token
NiklasHerrmann21 82c307e
get-Xsuaa-Token
NiklasHerrmann21 2ba7534
get-Xsuaa-Token
NiklasHerrmann21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 117 additions & 5 deletions
122
java-api/src/main/java/com/sap/cloud/security/token/IdTokenExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,127 @@ | ||
| package com.sap.cloud.security.token; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Extension interface for the {@link SecurityContext} to provide additional methods for extended | ||
| * security contexts. | ||
| * Extension interface for resolving and caching ID tokens from access tokens. | ||
| * | ||
| * <p>This interface defines the contract for automatic ID token resolution in {@link | ||
| * SecurityContext}. Implementations are responsible for: | ||
| * | ||
| * <ul> | ||
| * <li><b>Token Exchange:</b> Converting access tokens to ID tokens (e.g., via OAuth2 token | ||
| * exchange) | ||
| * <li><b>Caching Logic:</b> Determining when to return cached tokens vs. re-resolving | ||
| * <li><b>Expiration Handling:</b> Validating token expiration and triggering re-resolution when | ||
| * needed | ||
| * </ul> | ||
| * | ||
| * <p><b>Caching Strategy:</b> | ||
| * | ||
| * <p>The extension receives the currently cached ID token (if any) and decides whether to: | ||
| * | ||
| * <ol> | ||
| * <li><b>Return cached token:</b> If it exists and is still valid | ||
| * <li><b>Re-resolve token:</b> If cached token is expired, missing, or otherwise invalid | ||
| * </ol> | ||
| * | ||
| * This design decouples caching policy from {@link SecurityContext}, allowing implementations to | ||
| * customize expiration checks, implement token refresh logic, or add custom validation rules. | ||
| * | ||
| * <p><b>Thread Safety:</b> | ||
| * | ||
| * <p>Implementations must be thread-safe as the extension is registered globally via {@link | ||
| * SecurityContext#registerIdTokenExtension(IdTokenExtension)} but operates on thread-local tokens. | ||
| * Multiple threads may call {@link #resolveIdToken(Token)} concurrently. | ||
| * | ||
| * <p><b>Lifecycle:</b> | ||
| * | ||
| * <ol> | ||
| * <li><b>Registration:</b> Extension is registered once at application startup via {@link | ||
| * SecurityContext#registerIdTokenExtension(IdTokenExtension)} | ||
| * <li><b>Resolution:</b> Called by {@link SecurityContext#getIdToken()} when ID token is | ||
| * requested | ||
| * <li><b>Caching:</b> Returned token is cached in thread-local {@link SecurityContext} | ||
| * <li><b>Re-resolution:</b> Called again on next {@link SecurityContext#getIdToken()} if cached | ||
| * token expired | ||
| * </ol> | ||
| * | ||
| * <p><b>Usage Example (Spring Boot):</b> | ||
| * | ||
| * <pre>{@code | ||
| * @Configuration | ||
| * public class SecurityConfig { | ||
| * @PostConstruct | ||
| * public void registerExtensions() { | ||
| * SecurityContext.registerIdTokenExtension( | ||
| * new DefaultIdTokenExtension(tokenService, iasConfig) | ||
| * ); | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * <p><b>Error Handling:</b> | ||
| * | ||
| * <p>Implementations should handle errors gracefully and return {@code null} if resolution fails | ||
| * (network errors, invalid tokens, missing configuration, etc.). {@link SecurityContext} will | ||
| * propagate the {@code null} to callers, allowing them to handle missing ID tokens appropriately. | ||
| * | ||
| * @see SecurityContext#getIdToken() | ||
| * @see SecurityContext#registerIdTokenExtension(IdTokenExtension) | ||
| * @see SecurityContext#clearIdToken() | ||
| */ | ||
| public interface IdTokenExtension { | ||
|
|
||
| /** | ||
| * Resolves the ID token from the extended security context. | ||
| * Resolves an ID token from the current security context. | ||
| * | ||
| * <p>This method is called by {@link 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. | ||
| * | ||
| * <p><b>Caching Responsibility:</b> | ||
| * | ||
| * <p>The implementation is responsible for: | ||
| * | ||
| * <ol> | ||
| * <li><b>Checking cached token validity:</b> Inspect {@code cachedIdToken} expiration | ||
| * <li><b>Deciding whether to re-resolve:</b> Return cached token if valid, otherwise resolve | ||
| * new token | ||
| * <li><b>Token exchange:</b> If re-resolution needed, exchange access token for ID token | ||
| * </ol> | ||
| * | ||
| * <p><b>Access Token Availability:</b> | ||
| * | ||
| * <p>The access token is available via {@link SecurityContext#getToken()}. If no access token | ||
| * exists, the implementation should return {@code null} since token exchange is impossible. | ||
| * | ||
| * <p><b>Return Value Handling:</b> | ||
| * | ||
| * <ul> | ||
| * <li><b>Non-null token:</b> Cached in {@link SecurityContext} for subsequent {@link | ||
| * SecurityContext#getIdToken()} calls | ||
| * <li><b>{@code null}:</b> No caching occurs; subsequent calls will re-invoke this method | ||
| * </ul> | ||
| * | ||
| * <p><b>Thread Safety:</b> | ||
| * | ||
| * <p>This method may be called concurrently from multiple threads. Implementations must be | ||
| * stateless or use proper synchronization. | ||
| * | ||
| * @param cachedIdToken the currently cached ID token from thread-local {@link SecurityContext}, | ||
| * or {@code null} if: | ||
| * <ul> | ||
| * <li>No ID token has been resolved yet for this thread | ||
| * <li>The cached token was cleared via {@link SecurityContext#clearIdToken()} | ||
| * <li>The security context was reset via {@link SecurityContext#setToken(Token)} | ||
| * </ul> | ||
| * | ||
| * @return the ID token or null if not available. | ||
| * @return the resolved ID token (may be the cached token if still valid), or {@code null} if: | ||
| * <ul> | ||
| * <li>No access token is available in the security context | ||
| * <li>Token exchange fails (network error, invalid configuration, etc.) | ||
| * <li>The access token does not support ID token exchange | ||
| * </ul> | ||
| */ | ||
| Token resolveIdToken(); | ||
| Token resolveIdToken(@Nullable Token cachedIdToken); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.