Sub topic 11
Authentication and User Propagation
This chapter assumes you know OAuth 2.0 basics, have read the previous chapter on destinations, and can read simple Node.js code. It covers how a side-by-side extension proves its identity to SAP S/4HANA and whose identity S/4HANA sees.
Two questions to answer
Every call from your extension to S/4HANA raises two separate questions.
- Who is the calling application? This is authentication of the technical client.
- On whose behalf is the call made? This is user propagation, and it decides which authorizations S/4HANA applies.
The answers differ by scenario. A nightly sync job has no human user. A Fiori app that shows a manager's own approvals needs that manager's identity.
Common authentication types
The Destination service stores the authentication type, so your CAP code does not handle secrets itself.
| Authentication type | Identity S/4HANA sees | Typical use |
|---|---|---|
| BasicAuthentication | One fixed technical user | Quick tests, legacy setups |
| OAuth2ClientCredentials | The client, mapped to a technical user | Background jobs, user-independent reads |
| OAuth2SAMLBearerAssertion | The logged-in business user | User-specific data and authorization checks |
| OAuth2UserTokenExchange | The logged-in business user | Calls where the user token is exchanged for a new one |
| PrincipalPropagation | The logged-in business user | On-premise systems reached through Cloud Connector |
Prefer OAuth-based types over Basic authentication. Basic authentication stores a password that you must rotate by hand.
Technical user or propagated user
With a technical user, S/4HANA sees the same identity for every request. Authorizations are broad, so your extension must enforce its own access rules. This is simple but a risk: a user who can reach your app may see data they could never see in S/4HANA.
With user propagation, the end user's identity travels with the request. S/4HANA applies that user's own roles. The extension cannot read more than the user is allowed to read. This is the safer default for interactive apps.
Selecting the mode in CAP
CAP lets you choose per call. By default, a remote service call uses the destination's configuration. For a destination that propagates the user, the request context supplies the user JWT.
{
"cds": {
"requires": {
"API_BUSINESS_PARTNER": {
"kind": "odata-v2",
"model": "srv/external/API_BUSINESS_PARTNER",
"credentials": {
"destination": "S4HC_BP",
"path": "/sap/opu/odata/sap/API_BUSINESS_PARTNER"
}
}
}
}
}This is the same configuration as before. The user-versus-technical decision lives in the destination, so you can change it without redeploying code. In the handler, bp.run(req.query) runs in the context of the current request. That is why the request's user token is available to the Destination service.
For a background job with no request, run the call in a technical context and use a destination configured for client credentials.
const cds = require('@sap/cds')
async function readPartnersAsSystem() {
const bp = await cds.connect.to('API_BUSINESS_PARTNER')
return cds.tx({ user: cds.User.privileged }, () =>
bp.run(SELECT.from('A_BusinessPartner').limit(10))
)
}Here the privileged user applies only inside your CAP app. The identity S/4HANA sees is still set by the destination.
Common errors
- 401 Unauthorized. The token was missing or rejected. Check that the destination type matches what S/4HANA expects.
- 403 Forbidden for one user only. Propagation works, and that user lacks an S/4HANA role. Fix the role, not the app.
- Everyone sees the same data. The destination uses a technical user. Check the authentication type.
- Propagation fails in a background job. No user token exists. Use client credentials there.
Exercise
A Fiori app shows each manager only the business partners they may see in S/4HANA. Which authentication type fits, and why would OAuth2ClientCredentials be a poor choice?
Show answer
Use a user-propagating type such as OAuth2SAMLBearerAssertion or OAuth2UserTokenExchange, so S/4HANA applies each manager's own roles. OAuth2ClientCredentials maps every call to one technical user, so all managers would get that user's broad access unless the extension re-implemented the authorization checks.