Sub topic 11
Authentication and user propagation
This chapter assumes you have read the chapters on destinations and on consuming a released API with CAP. You should know what a BTP destination is and how a CAP remote service reads one. You should also know OAuth 2.0 basics: access tokens, client credentials, and JWTs.
Two questions every call must answer
When a side-by-side extension calls SAP S/4HANA, two separate questions come up:
- Who is the extension? The S/4HANA system must trust the calling application.
- On whose behalf does it act? The call runs either as a fixed technical user or as the person using the extension.
Question 1 is authentication of the application. Question 2 is the choice between a technical user and user propagation. Both are configured mostly in the destination, not in your code. That is the main benefit of the destination setup from the previous chapter. You can change the authentication type without changing the CAP handler.
Technical user versus propagated user
A technical user is one fixed identity. Every call from the extension reaches S/4HANA as that identity, whoever started it. A propagated user means the identity of the logged-in person is passed to S/4HANA. S/4HANA then applies that person's authorizations and writes that person's name into change documents and logs.
| Aspect | Technical user | Propagated user |
|---|---|---|
| Identity in S/4HANA | One shared service account | The actual end user |
| Authorizations checked | Those of the service account | Those of the end user |
| Audit trail | Shows the service account | Shows the person |
| Setup effort | Low | Higher: trust and user mapping are needed |
| Typical use | Background jobs, event handlers, read-only reference data | Interactive apps where S/4HANA authorizations must apply |
Choose a technical user when no human is involved, for example when an event handler runs after a business event. Choose propagation when the S/4HANA authorization model should decide what the person may see or change.
Authentication types in destinations
The Destination service supports several authentication types. These are the ones you meet most often with S/4HANA:
| Type | What it does | Identity |
|---|---|---|
| `BasicAuthentication` | Sends a user name and password | Technical |
| `OAuth2ClientCredentials` | Fetches a token with a client ID and secret | Technical |
| `ClientCertificateAuthentication` | Uses a client certificate for mutual TLS | Technical |
| `OAuth2SAMLBearerAssertion` | Exchanges the user's identity for an OAuth token through a SAML assertion | Propagated |
| `PrincipalPropagation` | Sends a short-lived certificate for the user through Cloud Connector | Propagated |
| `OAuth2UserTokenExchange` | Exchanges the user's JWT for a token for the target | Propagated |
PrincipalPropagation applies to S/4HANA systems reached through the Cloud Connector, such as on-premise or private cloud editions. For SAP S/4HANA Cloud public edition, the usual choices are OAuth2SAMLBearerAssertion for users and OAuth2ClientCredentials or BasicAuthentication for technical access.
Avoid BasicAuthentication in production if an OAuth option exists. A password does not expire by itself, and it carries no scope information.
The S/4HANA side: communication arrangements
In SAP S/4HANA Cloud, an administrator exposes a released API to an outside caller through three linked objects:
- A communication scenario, for example SAPCOM0008 for Business Partner, Customer and Supplier Integration. SAP delivers it and it lists the APIs it covers.
- A communication system, which represents your BTP application and holds its credentials or trust settings.
- A communication arrangement, which connects the scenario to the system and generates the API endpoint.
The arrangement also decides how the caller authenticates. You can define an inbound communication user with a password, or an OAuth 2.0 client. For user propagation, you register the BTP identity provider as a trusted SAML 2.0 identity provider in the OAuth 2.0 settings of S/4HANA. A user mapping is then needed: the name in the assertion must match a business user in S/4HANA.
Name mismatches are the most common reason propagation fails. If the SAML assertion carries an email address, but the S/4HANA business user is identified differently, the token request is rejected or the call runs as the wrong user.
How a propagated call flows
The next diagram shows an interactive request with OAuth2SAMLBearerAssertion.
Note that your application never handles the SAML assertion or the S/4HANA token directly. The Destination service builds the assertion from the user's JWT, which is why your app must pass that JWT along when it asks for the destination.
A destination for user propagation
This destination configuration uses the properties format of the BTP cockpit export. All values are placeholders.
Name=S4HC_BP_USER
Type=HTTP
ProxyType=Internet
URL=https://my300000-api.s4hana.cloud.sap
Authentication=OAuth2SAMLBearerAssertion
audience=https://my300000.s4hana.cloud.sap
authnContextClassRef=urn:oasis:names:tc:SAML:2.0:ac:classes:PreviousSession
clientKey=<oauth client id registered in S/4HANA>
tokenServiceURL=https://my300000.s4hana.cloud.sap/sap/bc/sec/oauth2/token
tokenServiceUser=<oauth client id>
tokenServicePassword=<oauth client secret>
nameIdFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddressKeep the secrets in the destination, never in package.json or in source control. The nameIdFormat must agree with how users are identified in S/4HANA, as discussed above.
Wiring it into CAP
The CAP configuration is almost the same as in the previous chapter. Only the destination name changes.
{
"cds": {
"requires": {
"API_BUSINESS_PARTNER": {
"kind": "odata-v2",
"model": "srv/external/API_BUSINESS_PARTNER",
"credentials": {
"destination": "S4HC_BP_USER",
"path": "/sap/opu/odata/sap/API_BUSINESS_PARTNER"
}
}
}
}
}In the handler, run the query inside the transaction of the incoming request. That ties the outbound call to the request's user context, so the user's JWT reaches the destination lookup.
const cds = require('@sap/cds')
module.exports = class PartnerReview extends cds.ApplicationService {
async init() {
const bp = await cds.connect.to('API_BUSINESS_PARTNER')
this.on('READ', 'BusinessPartners', req => bp.tx(req).run(req.query))
return super.init()
}
}Compare this with the earlier handler that called bp.run(req.query). Without a request context, CAP cannot attach the user's token. With a user-dependent destination, the lookup then fails or falls back to behavior you did not intend. Test with a real logged-in user, not only with mocked authentication.
For a technical user, point the same configuration at a destination of type OAuth2ClientCredentials. No handler change is needed.
Authorize in your own app too
User propagation does not replace authorization in the extension. Requests reach your CAP app before they reach S/4HANA, and the app must check them. Protect the service with a scope-based annotation:
using { API_BUSINESS_PARTNER as bp } from './external/API_BUSINESS_PARTNER';
service PartnerReview @(requires: 'PartnerReviewer') {
@readonly
entity BusinessPartners as projection on bp.A_BusinessPartner {
key BusinessPartner,
BusinessPartnerFullName,
BusinessPartnerIsBlocked
};
}The scope or role PartnerReviewer is defined in the application's security descriptor (xs-security.json) and assigned through role collections. With a technical user this check is the only barrier, because S/4HANA sees just the service account.
Common errors
- 401 from S/4HANA with a propagated user: the identity provider is not trusted in the OAuth 2.0 settings, or the assertion's audience does not match the configured value.
- 403 for one user and success for another: the user's business roles in S/4HANA lack the authorization. This is propagation working as designed.
- Every call appears under the same user: the destination is a technical type, or the handler ran without the request context.
- Calls work in testing but fail in a background job: no user JWT exists outside a request. Use a technical destination for that code path.
- Token expiry errors: the Destination service caches tokens only for their lifetime. Do not store the Authorization header yourself.
Exercise
An extension has two features. Feature 1 lets a purchaser edit a supplier record and the change must appear under the purchaser's name in S/4HANA. Feature 2 is an event handler that reads supplier data after a business event arrives. Which authentication type fits each feature, and why can't Feature 2 use user propagation?
Show answer
Feature 1 needs a propagated user, for example OAuth2SAMLBearerAssertion for S/4HANA Cloud public edition, or PrincipalPropagation through the Cloud Connector for a system reached that way. Then S/4HANA checks the purchaser's authorizations and records the purchaser. Feature 2 should use a technical user such as OAuth2ClientCredentials. The event handler runs without an interactive request, so there is no user JWT to exchange for a SAML assertion.
Exercise
A CAP app uses a destination of type OAuth2SAMLBearerAssertion, but all changes in S/4HANA show the same service account. Name two causes to check.
Show answer
First, the destination may actually be a technical type, or the app may point to the wrong destination name. Second, the handler may call bp.run(req.query) without the request context, so the user's JWT is not passed on. Using bp.tx(req).run(req.query) fixes this case.