You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[java-security-test](/java-security-test)|[Integration test code snippet](/samples/spring-security-hybrid-usage/src/test/java/sample/spring/security/junitjupiter/TestControllerIasTest.java) for Spring application <br/>[Integration test code snippet](/samples/java-security-usage/src/test/java/com/sap/cloud/security/samples/HelloJavaServletIntegrationTest.java) for Jakarta EE web.xml based servlets <br/> [Integration test code snippet](/samples/java-security-usage-ias/src/test/java/com/sap/cloud/security/samples/ias/HelloJavaServletIntegrationTest.java) for Jakarta EE annotation based servlets <br/> |
120
123
124
+
### 2.4 Token Exchange for Hybrid Authentication
125
+
126
+
In hybrid authentication setups, your application can accept tokens from both **SAP Identity Authentication Service (
127
+
IAS)** and **XSUAA** simultaneously. This approach eases migration from XSUAA to IAS by exchanging IAS user tokens for
128
+
XSUAA tokens behind the scenes.
129
+
130
+
**Goal**: Maintain backward compatibility during migration. Users authenticate via IAS, but the application continues
131
+
using XSUAA-based authorization (scopes, role collections).
132
+
133
+
#### How Token Exchange Works
134
+
135
+
When a JWT bearer token arrives:
136
+
137
+
1. The library identifies whether the token was issued by IAS or XSUAA
138
+
2. The token is validated using the appropriate validator
139
+
3. If the token is from IAS and exchange is enabled, an XSUAA token is obtained via OAuth2 JWT bearer flow
140
+
4. The XSUAA token contains the user's roles/scopes as defined in XSUAA
141
+
5. Authorization proceeds using familiar XSUAA token attributes
142
+
143
+
If the incoming token is already an XSUAA token, no exchange occurs—it's validated and used directly.
144
+
145
+
#### Token Exchange Modes
146
+
147
+
The [`TokenExchangeMode`](java-security/src/main/java/com/sap/cloud/security/token/TokenExchangeMode.java)enum controls
|**`DISABLED`**| No exchange. Each token type is validated and used as-is. |
154
+
|**`PROVIDE_XSUAA`**| IAS token is validated and exchanged for XSUAA, but the IAS token remains primary in the security context. The XSUAA token is accessible via [`SecurityContext.getXsuaaToken()`](java-api/src/main/java/com/sap/cloud/security/token/SecurityContext.java). |
155
+
|**`FORCE_XSUAA`**| IAS token is exchanged for XSUAA and the XSUAA token replaces the IAS token in the security context. The application treats all requests as XSUAA-authenticated. |
156
+
157
+
**Mode Selection Guide**:
158
+
159
+
- Use **`PROVIDE_XSUAA`** when gradually building IAS-based features while maintaining XSUAA authorization
160
+
- Use **`FORCE_XSUAA`** for maximum backward compatibility—your app operates as if it were XSUAA-only
161
+
- Use **`DISABLED`** after completing the migration to IAS
162
+
163
+
#### Prerequisites for Token Exchange
164
+
165
+
1. Both XSUAA and IAS service bindings must be configured
166
+
2. IAS service binding must include `xsuaa-cross-consumption: true` parameter (Cloud Foundry)
167
+
3. On Kubernetes/Kyma, ensure XSUAA trusts the IAS identity provider
168
+
169
+
#### Token Exchange Flow Summary
170
+
171
+
```
172
+
1. Request arrives with Authorization: Bearer <token>
173
+
2. Library identifies issuer (IAS vs XSUAA) from token claims
174
+
3. Token validated against appropriate identity service
175
+
4. [IF IAS token + exchange enabled]
176
+
├─ Obtain strong IAS ID token (if access token provided)
177
+
├─ Call XSUAA /oauth/token endpoint with JWT bearer grant
178
+
└─ Store exchanged XSUAA token in SecurityContext
179
+
5. [IF exchange disabled OR XSUAA token]
180
+
└─ Use validated token directly
181
+
6. Request proceeds with authenticated security context
182
+
```
183
+
184
+
**Failure Handling**: If token exchange fails (network issues, misconfiguration), authentication fails with 401
185
+
Unauthorized. No silent fallback occurs since IAS access tokens typically lack scopes needed for authorization.
186
+
187
+
#### 2.4.1 Jakarta Example: Using [`HybridTokenAuthenticator`](java-security/src/main/java/com/sap/cloud/security/servlet/HybridTokenAuthenticator.java)
188
+
189
+
For Jakarta EE applications, use [
190
+
`HybridTokenAuthenticator`](java-security/src/main/java/com/sap/cloud/security/servlet/HybridTokenAuthenticator.java) in
#### 2.4.2 Spring Boot Example: Using [`HybridJwtDecoder`](spring-security/src/main/java/com/sap/cloud/security/spring/token/authentication/HybridJwtDecoder.java)
| `Token exchange failed` exception | Missing XSUAA binding or invalid configuration | Verify both IAS and XSUAA service bindings exist |
382
+
| Exchange returns 400 | IAS binding missing `xsuaa-cross-consumption` | Add parameter to IAS service binding |
383
+
| Exchanged token has no scopes | Role mappings not configured in XSUAA | Configure role collections in XSUAA |
384
+
| Memory leak warnings | `SecurityContext` not cleared after request | Call `SecurityContext.clear()` in finally block |
385
+
386
+
**Enable Debug Logging**:
387
+
388
+
```yaml
389
+
logging:
390
+
level:
391
+
com.sap.cloud.security: DEBUG
392
+
```
393
+
121
394
## Installation
122
395
The SAP Cloud Security Services Integration is published to maven central: https://search.maven.org/search?q=com.sap.cloud.security and is available as a Maven dependency. Add the following BOM to your dependency management in your `pom.xml`:
0 commit comments