Security: XSUAA and Cloud Identity Services

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Every business application has to answer two questions for each request: who is calling, and what may this caller do. Think of a company building with a reception desk and a key cabinet. The desk checks your badge and learns who you are. The cabinet hands you only the keys that belong to your job. On SAP Business Technology Platform (SAP BTP) the badge check is done by an identity provider, and the key cabinet is the SAP Authorization and Trust Management service, known by its technical name XSUAA. The cabinet’s list of keys is written by the application developer in a file called xs-security.json, and the administrator hands out bundles of keys called role collections. This page explains those pieces in that order, shows the file that CAP generates for the claims application of Example Trading Co. (EX01), and runs the same authorization rules locally with mock users. SAP still documents the product as SAP BTP, and SAP now presents BTP capabilities as part of SAP Business AI Platform. The facts here apply to CAP Node.js 10.2 (@sap/cds 10.2.2, Node.js 22.12 or newer) and CAP Java 5.2 (Java 21 or newer, 25 recommended) running in the Cloud Foundry environment; the places where the Kyma environment differs are named.

Before you read

Key terms

  • XSUAA: the SAP Authorization and Trust Management service, which acts as the OAuth 2.0 authorization server for applications on SAP BTP.

  • Scope: a named permission that an application declares in its security descriptor and checks at runtime.

  • Role template: a blueprint in the security descriptor that bundles scopes and attributes into a role.

  • Role collection: a group of roles that an administrator assigns to users or user groups.

  • Identity provider: the system that holds the users and authenticates them, such as an SAP Cloud Identity Services tenant.

  • Identity Authentication: the SAP Cloud Identity Services service that provides authentication and single sign-on.

  • Trust configuration: the setting in a subaccount that makes it accept logins from one identity provider.

  • JSON Web Token (JWT): the signed token that carries the user’s identity and scopes to the application.

Two layers: who you are and what you may do

SAP separates the two questions into two services that cooperate.

  • Authentication proves who the user is. Users of a business application are held by an identity provider, and SAP recommends an SAP Cloud Identity Services tenant, an SAP on-premise system, or a custom corporate identity provider for this. SAP Cloud Identity Services is a group of services: Identity Authentication, Identity Provisioning, Identity Directory and Authorization Management. Identity Authentication offers authentication methods, single sign-on with OpenID Connect or SAML 2.0, and delegation of the actual password check to a third-party or on-premise identity provider.

  • Authorization decides what an authenticated user may do inside one application. This is the job of the SAP Authorization and Trust Management service. It manages user authorizations as technical roles at the application level, which can be grouped into business-level role collections, and it manages the trust to identity providers.

The two meet in a trust relationship. SAP describes it as crossover metadata configuration between the identity provider and XSUAA: each side learns the other’s endpoints. After the user signs in, the identity provider tells XSUAA who the user is, and XSUAA answers with a token that lists what the user may do.

How XSUAA works

The security of applications on SAP BTP is based on OAuth 2.0, and the other pages of this site on OAuth 2.0 explain the standard. SAP’s help page adds one twist worth knowing. SAP reuses the specification with its own implementation that exchanges the roles of two OAuth entities: the OAuth client, which is the application, holds all the authorizations, and a subset of them is assigned to the user after authentication. The application also acts as the resource server, because it contains the resource apps, and all its apps operate under one OAuth client. XSUAA is the authorization server.

An application is built from a gateway app, usually the application router, and one or more resource apps. The application router is the single point of entry: it serves static content, starts the authentication process, checks for cross-site request forgery and forwards requests to the resource apps while passing the user information on. The resource apps use a security client library to enforce the authorizations. Each application gets its own XSUAA service instance, created by the service broker, and every app that enforces authorizations is bound to it.

sequenceDiagram participant U as User in browser participant R as Application router participant X as XSUAA participant I as Identity provider participant A as Resource app (CAP service) U->>R: First request R->>X: Start authentication X->>I: Redirect the browser to log on I-->>X: SAML bearer assertion confirming the user X-->>R: JSON web token with scopes R->>A: Request with the token A->>A: Verify signature, check scopes A-->>U: Response

The token is a JSON Web Token as in RFC 7519, signed with the private key of the authorization server. The application trusts XSUAA through its VCAP_SERVICES environment variable, where the binding credentials include the public key that verifies the signature. Interactive users go through the redirects shown above. API clients, such as another application or a script, receive credentials from XSUAA and authenticate directly against it. For the Cloud Foundry environment SAP lists the supported token grant types as authorization code, client credentials and SAML 2.0 bearer, with refresh tokens also supported. The service is available in the Cloud Foundry, Kyma and Neo environments; this page covers Cloud Foundry, and SAP’s Kyma page on security is the starting point for the Kyma environment.

The descriptor: xs-security.json

The developer describes the application’s security in a JSON file called the application security descriptor, by convention xs-security.json. At deployment its content is passed to the XSUAA service broker, which creates or updates the service instance and the OAuth client from it. The table lists the properties used on this page, as SAP’s syntax page defines them.

Property Meaning

xsappname

The name of the application the descriptor applies to. It may be up to 100 characters and use letters, digits, hyphen, underscore, slash and backslash.

tenant-mode

How the tenant’s OAuth clients get their client secrets: dedicated (the default, a separate secret per subaccount), shared (one secret valid in all subaccounts; the application service plan uses it) or external.

scopes

The permissions the application defines, each with a name and a description. Local scopes are prefixed with the variable $XSAPPNAME, which is replaced by the application name at runtime.

attributes

User-specific values, such as a country or cost center, that can narrow a role. Each has a name, a description, a valueType (string or s, int, or date) and optionally valueRequired.

role-templates

Roles built from scopes and attributes: name, description, default-role-name, scope-references and attribute-references.

role-collections

Optional predefined bundles of roles for administrators: name, description and role-template-references.

oauth2-configuration

Custom values for the OAuth client: token-validity, refresh-token-validity, redirect-uris, credential-types, system-attributes and allowedproviders.

authorities

Scopes that this application, as a sender, accepts from another application. "$ACCEPT_GRANTED_AUTHORITIES" accepts every authority flagged as grantable.

Scopes, role templates, roles and role collections

The four building blocks form a chain, from the smallest to the largest.

  1. A scope is one named permission, for example Employee or FinanceApprover. The application checks scopes. In the file a local scope is written $XSAPPNAME.Employee.

  2. A role template lists the scopes (and attributes) that belong together. It is a blueprint, not yet a role the administrator can assign.

  3. A role is an instance of a role template. SAP’s page says a role template must be instantiated, mainly because attribute values depend on the customer and cannot be supplied automatically. A role template without attribute references can produce a default role at deployment.

  4. A role collection groups roles. Administrators assign role collections to users who log on with the SAP ID service, or to user groups of users who log on with SAML 2.0 assertions. Predefined role collections in xs-security.json can reference only role templates that create default roles automatically, that is, templates without attribute references, or with default attribute values, or with an attribute set to valueRequired: false.

Role collections are also what SAP uses for its own platform administrators. The default administrator collections include Global Account Administrator, Subaccount Administrator and Directory Administrator, and viewer collections only grant read access. SAP states that you can use these default collections but cannot change or delete them.

Attributes: instance-based restrictions

A scope says "may approve claims". An attribute says "may approve claims of cost center 4711". SAP calls this an instance-based authorization. The administrator can give an attribute a static value stored in the role, take its value from the identity provider’s SAML 2.0 assertion, or leave it unrestricted. SAP’s page also recommends implementing instance-based authorizations with SAP Cloud Identity Services Authorization Management, and says the attributes concept remains relevant for applications that use it.

Source: Attributes

What CAP generates

CAP can write the descriptor for you, because the authorization rules are already in the CDS model. The claims service of the lab has no roles, so the lab copy for this page adds a small teaching model, srv/authorization.cds, shown in full. It is not part of the lab. The role names Employee and FinanceApprover are invented for this page; @requires and @restrict and the pseudo role authenticated-user are CAP’s own annotations.

using { ExpenseService } from './expense-service';

annotate ExpenseService with @(requires: 'authenticated-user');

annotate ExpenseService.Claims with @(restrict: [
  { grant: ['READ', 'CREATE', 'UPDATE'], to: 'Employee' },
  { grant: '*', to: 'FinanceApprover' }
]);

The first annotation lets any authenticated user reach the service. The second grants an Employee to read, create and update claims and a FinanceApprover every event (*), which includes delete. The following commands ran for real in a copy of the lab with @sap/cds-dk 10.2.1 (cds add xsuaa writes xs-security.json and edits package.json, and the output of cds compile was identical to the file).

npx cds add xsuaa
npx cds compile srv --to xsuaa
Adding facet: xsuaa

Successfully added features to your project

The generated xs-security.json, unchanged, is below. CAP derives one scope per CAP role with the exact role name, one attribute per CAP attribute (none here) and one role template per scope with the same name. There is no xsappname in the file: CAP sets it in the deployment descriptor instead, as shown further down.

{
  "scopes": [
    {
      "name": "$XSAPPNAME.Employee",
      "description": "Employee"
    },
    {
      "name": "$XSAPPNAME.FinanceApprover",
      "description": "FinanceApprover"
    }
  ],
  "attributes": [],
  "role-templates": [
    {
      "name": "Employee",
      "description": "generated",
      "scope-references": [
        "$XSAPPNAME.Employee"
      ],
      "attribute-references": []
    },
    {
      "name": "FinanceApprover",
      "description": "generated",
      "scope-references": [
        "$XSAPPNAME.FinanceApprover"
      ],
      "attribute-references": []
    }
  ]
}

The CAP page states that scope names must match the CDS role names exactly, because the match is checked at runtime, where CAP strips the $XSAPPNAME prefix to find the CAP role. After a model change you regenerate the file with cds compile srv --to xsuaa > xs-security.json. The command also added @sap/xssec to package.json and set the production profile to use XSUAA, which the page on Node.js authentication describes as setting auth to xsuaa under cds.requires and installing @sap/xssec.

"cds": {
  "requires": {
    "[production]": {
      "auth": "xsuaa"
    }
  }
}

The deployment descriptor

XSUAA is created as a service instance when the application is deployed, so the descriptor also needs a place in the deployment file. A second command, cds add mta, produced mta.yaml in the lab copy (shortened to the resources part). It asks XSUAA for the plan application, points path at xs-security.json and adds role collections named after the roles. The CAP page explains the properties: service-plan is application for UI applications without API access and broker for everything else, upgrading from application to broker is not supported, and tenant-mode is dedicated for single-tenant applications and shared for multitenant ones.

resources:
  - name: expenses-auth
    type: org.cloudfoundry.managed-service
    parameters:
      service: xsuaa
      service-plan: application
      path: ./xs-security.json
      config:
        xsappname: expenses-${org}-${space}
        tenant-mode: dedicated
        oauth2-configuration:
          credential-types:
            - "binding-secret"
            - "x509"
        role-collections:
          - name: 'Employee (expenses ${org}-${space})'
            description: 'generated'
            role-template-references:
              - '$XSAPPNAME.Employee'
          - name: 'FinanceApprover (expenses ${org}-${space})'
            description: 'generated'
            role-template-references:
              - '$XSAPPNAME.FinanceApprover'

For CAP Java the same facet works without a configuration switch: the CAP page says the binding that cds add xsuaa adds activates XSUAA authentication automatically, and it lists a startup log line for each runtime so you can check which kind is active. Deploying the application, binding it to the instance and running the application router are covered on the page on deploying CAP.

A hand-written descriptor

The generated file is a starting point. A real application usually adds its own xsappname, an explicit role collection, the redirect addresses of its application router and a token lifetime. The listing below is a teaching example written for this page from the property definitions above, not generated output. It uses the names of the claims application, an invented attribute CostCenter, and invented host names under example.com.

requires an SAP BTP account; not run here

{
  "xsappname": "expenses",
  "tenant-mode": "dedicated",
  "scopes": [
    { "name": "$XSAPPNAME.Employee", "description": "Employee" },
    { "name": "$XSAPPNAME.FinanceApprover", "description": "FinanceApprover" }
  ],
  "attributes": [
    { "name": "CostCenter", "description": "Cost center", "valueType": "string", "valueRequired": false }
  ],
  "role-templates": [
    {
      "name": "Employee",
      "description": "Create and read own claims",
      "default-role-name": "Employee: Submit claims",
      "scope-references": ["$XSAPPNAME.Employee"]
    },
    {
      "name": "FinanceApprover",
      "description": "Approve claims of a cost center",
      "default-role-name": "FinanceApprover: Approve claims",
      "scope-references": ["$XSAPPNAME.FinanceApprover"],
      "attribute-references": ["CostCenter"]
    }
  ],
  "role-collections": [
    {
      "name": "Expenses Employee",
      "description": "Employees of Example Trading Co. who submit claims",
      "role-template-references": ["$XSAPPNAME.Employee"]
    }
  ],
  "oauth2-configuration": {
    "token-validity": 3600,
    "redirect-uris": ["https://expenses.example.com/**"],
    "credential-types": ["binding-secret", "x509"]
  }
}

Four details of this file come straight from SAP’s property documentation. token-validity is in seconds, between 60 and 86400 with a default of 43200 (12 hours), and SAP recommends keeping it as short as possible but not below 30 minutes. redirect-uris lists the addresses to which XSUAA may redirect after login or logout, with explicit wildcards such as https://*.mydomain.com/callback/** supported; SAP advises making them as specific as possible because wildcards open the redirect to several sites. The role collection references only the Employee template, because that template has no attribute reference and so produces a default role. The FinanceApprover template refers to CostCenter, which is valueRequired: false, so SAP’s table says a default role is generated and administrators need not set an attribute value, which creates a role not restricted by attributes.

Trying the same rules locally with mock users

The deployed setup needs an account, but the rules in srv/authorization.cds can be tested on a laptop, because CAP has a built-in mocked authentication for non-production profiles. The lab copy defines three users in the development profile of package.json. The passwords are throwaway test values for this local run.

"[development]": {
  "auth": {
    "kind": "mocked",
    "users": {
      "anna":  { "password": "anna",  "roles": ["Employee"] },
      "frank": { "password": "frank", "roles": ["FinanceApprover"] },
      "guest": { "password": "guest" }
    }
  }
}

The service started with npx cds-serve and the startup log said using auth strategy { kind: 'mocked' }. The requests below used curl against the claims service, with and without credentials. The DELETE addressed the one claim of the lab’s sample data, and was run last by anna and then by frank.

Request Caller HTTP status

GET claims

no credentials

401

GET claims

anna with a wrong password

401

GET claims

guest (authenticated, no role)

403

GET claims

anna (Employee)

200

GET claims

frank (FinanceApprover)

200

DELETE the claim

anna (Employee)

403

DELETE the claim

frank (FinanceApprover)

204

The result is the three layers of the model at work. Without a login the answer is 401. A logged-in user without a role gets past the service-level authenticated-user rule, but the entity restriction refuses with 403. An Employee may read but not delete, and only the FinanceApprover may delete. On SAP BTP the same decisions are made from the scopes inside the token that XSUAA issues, instead of from the mock users.

Testing against XSUAA after deployment

The CAP page describes a command-line test of a deployed application. The steps below follow it, with the names of the generated mta.yaml. They need a Cloud Foundry space in a subaccount, so they were not run for this page.

requires an SAP BTP account; not run here

cds add mta
cds add xsuaa
cds up
cf create-service-key expenses-auth expenses-auth-key
cf service-key expenses-auth expenses-auth-key
cf delete-service-key expenses-auth expenses-auth-key

The service key shows clientid, clientsecret and url. A token is requested from <url>/oauth/token. A technical user uses the grant type client_credentials with the client id and secret, and the CAP page shows that such a token reaches endpoints without role requirements but receives 403 on an endpoint that needs a role. A named user is tested with the grant type password, after the administrator has assigned the generated role collection to that user. The grants are the subject of Client Credentials and Client Authentication; the password grant is covered, with its drawbacks, in Legacy Grants: Implicit and Password, and it is used on the CAP page only as a convenient command-line test. For testing through a browser, the CAP page adds an application router with cds add portal and says to set forwardAuthToken: true on the srv-api destination.

Assigning authorizations

Deploying creates the roles; nobody can use them until an administrator connects users to role collections. SAP describes two ways.

  • Assign a role collection to a user directly, in the SAP BTP cockpit or with the btp command line.

  • Map a role collection to a user group that the identity provider supplies in the Groups attribute. SAP says this saves time: add a new employee to the right group and the employee gets every authorization of the collections mapped to that group. Role collections can also be mapped to attributes other than groups.

One consequence from SAP’s syntax page is worth remembering: if you rename a role collection in the descriptor and update the service instance, the old collection is deleted and a new one without user assignments is created.

Trust and identity providers

Before anyone can log on, the subaccount must trust an identity provider. SAP BTP supports identity federation: applications and the platform do not store or check passwords themselves, they reuse the user base of an identity provider. SAP ID service is the default identity provider with trust preconfigured. SAP strongly recommends a custom SAP Cloud Identity Services tenant instead, used as the identity provider or as a proxy to your corporate identity provider, so that you can enforce your own password and authentication policies, such as stronger passwords or multifactor authentication. For platform users, SAP states that SAP Cloud Identity Services is mandatory.

In the cockpit the trust is set up under Security, Trust Configuration, with the Establish Trust wizard. The SAP page on this method says it uses OpenID Connect and so avoids uploading and downloading SAML metadata. It lists the points a beginner should know.

  • You need subaccount administrator permissions and an SAP Cloud Identity Services tenant that is associated with the customer IDs of the global account.

  • Only one tenant of SAP Cloud Identity Services per subaccount can be trusted with this method, and an existing SAML trust to a tenant cannot get an additional OIDC trust to the same tenant.

  • The origin key of the trust configuration can have at most 36 characters.

  • Create Shadow Users on User Logon is kept on if unsure, because a logon fails when the shadow user does not exist, and an administrator can then assign role collections to the shadow user.

  • SAP Cloud Identity Services creates an application whose name starts with SAP BTP subaccount followed by the subaccount name, and applications for subscriptions become its child applications.

  • If the OIDC issuer changes, the trust breaks, and Refresh in the trust configuration repairs it.

The descriptor can also narrow the identity providers per application with allowedproviders, which holds origin keys; SAP recommends configuring allowed providers at subaccount level instead.

XSUAA or Identity Authentication for a new CAP project

CAP’s own pages put it plainly. They describe XSUAA as an OAuth 2.0 based platform service for individual BTP landscapes and say "Start new projects with IAS", the Identity Authentication service. They describe the ias kind as the preferred identity service, requiring mutual TLS and available only for single-tenant applications. The mocked kind is for local work, and a hybrid mode is described only as coming. So the choice for a new single-tenant application starts with Identity Authentication, while XSUAA remains the documented mechanism for multitenant applications (tenant-mode shared) and for many existing deployments, including everything SAP’s descriptor syntax describes. The scope, role and role collection ideas carry over in either case, because CAP roles in the model stay the same.

Where the OAuth pages of this site fit

The concepts on this page have general counterparts in the OAuth section. XSUAA’s token is a JWT, so JWT and the JOSE Family explains the signature checks the security client library performs. The OpenID Connect trust that the cockpit wizard sets up is covered in OpenID Connect. XSUAA’s SAML bearer step is an assertion grant, discussed in Token Exchange and Assertion Grants. The distinction between a scope and a role, which SAP’s model bundles into scopes, role templates and roles, is the subject of Scopes, Claims and Permissions. For outbound calls from a CAP application to another system with a destination, see Connectivity, Destinations and Cloud Connector.

What can go wrong

Symptom or mistake Cause according to SAP’s pages

A user gets 403 although the role collection exists

The scope name in xs-security.json must match the CDS role name exactly, because CAP checks the match at runtime; regenerate the file after model changes.

A role collection loses its user assignments after a deployment

Its name was changed in the descriptor, which deletes the old collection and creates a new one without assignments.

A predefined role collection stays empty

It references a role template that has attribute references without default values and a required attribute, so no default role is created.

Redirect after login is rejected

The address is not listed in redirect-uris; XSUAA rejects any other address, and only localhost is always allowed in cloud landscapes.

Switching the service plan fails

Upgrading from the plan application to broker is not supported, according to the CAP page.

Summary

  • Authentication and authorization are separate on SAP BTP: an identity provider, ideally an SAP Cloud Identity Services tenant, proves who the user is, and XSUAA decides and records what the user may do.

  • XSUAA is the OAuth 2.0 authorization server of an application. It issues a signed JWT whose scopes the application checks, and the application is bound to its own XSUAA service instance.

  • xs-security.json declares scopes, attributes, role templates and optionally role collections, and oauth2-configuration sets token lifetime and redirect addresses. Administrators assign role collections to users or map them to groups.

  • CAP generates the file from the roles in the CDS model with cds add xsuaa; the scope names must equal the CDS role names. The generated file for the claims lab was run and shown above, and the same rules were tested locally with mock users.

  • Trust between a subaccount and an identity provider is configured in the cockpit. SAP recommends SAP Cloud Identity Services, and CAP’s pages recommend Identity Authentication for new projects.

  • Everything that creates an XSUAA instance, a trust configuration or a role collection assignment needs an SAP BTP account and was described, not run.

Try it

To do this by hand, follow the guide Secure a CAP Service with Roles: it protects a service with roles and shows the 401 and 403 answers.