CAP Security Essentials: XSUAA, JWT & Role-Based Access Control

Security is one of the most critical components of SAP CAP applications. Whether your app runs locally or on SAP BTP, applying authentication and authorization ensures that users access only the data and actions they are permitted to. CAP provides seamless integration with XSUAA, JWT-based authentication, and powerful role-based access control (RBAC).

This guide breaks down the essentials of CAP security and shows how to implement it correctly.

Why Security Matters in CAP

CAP applications typically expose:

  • OData services
  • REST endpoints
  • Business logic hooks
  • Sensitive enterprise data

Without strong authentication and authorization, these endpoints become vulnerable. CAP solves this using:

  • XSUAA for authentication
  • JWT tokens for identity and tenant awareness
  • RBAC for fine-grained authorization
  • Scopes & role collections through BTP

Understanding XSUAA Authentication

XSUAA (SAP Authorization & Trust Management) is the key identity provider for CAP applications running on SAP BTP.

It handles:

  • User authentication
  • Token issuance (JWT)
  • Role mappings
  • Trust with Identity Providers (IdPs)
  • Tenant-specific identity in multitenant scenarios

XSUAA Configuration

You define XSUAA rules in xs-security.json.

Example:

{
  "xsappname": "cap-security-app",
  "tenant-mode": "shared",
  "scopes": [
    { "name": "$XSAPPNAME.Admin", "description": "Admin Access" },
    { "name": "$XSAPPNAME.User", "description": "User Access" }
  ],
  "role-templates": [
    {
      "name": "Admin",
      "scope-references": [ "$XSAPPNAME.Admin" ]
    },
    {
      "name": "User",
      "scope-references": [ "$XSAPPNAME.User" ]
    }
  ]
}

JWT Tokens in CAP

XSUAA issues a JWT token after authentication.

The token contains:

  • User ID
  • Email
  • Roles & scopes
  • Tenant ID (for multitenancy)
  • Expiration time
  • Issuer

CAP automatically validates JWT tokens and maps claims to req.user.

Example token structure (decoded):

{
  "user_name": "hrishi",
  "scopes": ["cap-security-app.User"],
  "ext_attr": { "zdn": "tenant123" }
}

Accessing JWT claims in CAP

srv.before('*', req => {
  console.log(req.user.id);
  console.log(req.user.scopes);
});

Implementing Role-Based Access Control (RBAC) in CAP

RBAC ensures users access only authorized operations.

CAP supports RBAC at:

  • Entity level
  • Service level
  • Operation level
  • Custom events

1. Protecting Entities in CDS

entity Orders @(restrict: [
  { grant: ['READ'], to: ['User'] },
  { grant: ['WRITE', 'DELETE'], to: ['Admin'] }
]) {
  ID : UUID;
  amount : Integer;
}

2. Protecting Actions & Functions

action approveOrder @(restrict: [{ grant: ['WRITE'], to: ['Admin'] }]);

3. Protecting entire services

service AdminService @(requires: 'Admin') {
  entity Configurations as projection on my.Config;
}

The requires annotation ensures only Admins access the service.

Enforcing Authorization in Handlers

Use CAP’s built-in authorization checks:

this.before('UPDATE', 'Orders', req => {
  req.user.requires('Admin');
});

You can also check scopes:

if (!req.user.is('User')) req.reject(403, 'Not authorized');

Creating Roles in SAP BTP

After defining scopes in xs-security.json, you bind them to role collections in SAP BTP Cockpit.

Example role collections:

  • CAP-Admin → includes Admin role template
  • CAP-User → includes User role template

Assign these collections to users or groups to grant access.

Local Testing With Mock Users

CAP allows local testing without XSUAA using mock strategies.

Enable mock auth:

cds watch --with-mocks

Create a mock user:

{
  "user": "admin",
  "roles": ["Admin"],
  "tenant": "t1"
}

Securing CAP OData Services

OData endpoints can be protected by simply declaring RBAC in CDS.

Example:

service CatalogService @(requires: 'User') {
  entity Products as projection on db.Products;
}

CAP will reject unauthorized requests automatically.

Best Practices for CAP Security

  • Always use XSUAA in productive environments
  • Keep roles separated: Admin vs User vs Auditor
  • Never expose internal endpoints without RBAC
  • Avoid hardcoding roles in code
  • Test with mock users before deployment
  • Use HTTPS everywhere
  • Validate incoming JWT tokens
  • Apply principle of least privilege

Common Security Mistakes to Avoid

  • Skipping RBAC in early development
  • Mixing admin and business logic roles
  • Forgetting scope references in role templates
  • Not enabling authentication for custom actions
  • Using local JWTs for production

Conclusion

CAP security becomes powerful when you combine:

  • XSUAA for authentication
  • JWT for identity & tenant resolution
  • RBAC for granular authorization

By designing with security in mind, your CAP application becomes reliable, compliant, and ready for enterprise deployment on SAP BTP.

you may be interested in this blog here:-

Don’t Fear the Update: Navigating the Challenges of how to implement sap note

Five Top Technology Investment Drivers for 2024

How many dollars worth of RSU does Salesforce typically offer an MTS (experienced hire) on joining?

Integration cloud system to HANA Cloud Platform using Cloud Connector

₹25,000.00

SAP SD S4 HANA

SAP SD (Sales and Distribution) is a module in the SAP ERP (Enterprise Resource Planning) system that handles all aspects of sales and distribution processes. S4 HANA is the latest version of SAP’s ERP suite, built on the SAP HANA in-memory database platform. It provides real-time data processing capabilities, improved…
₹25,000.00

SAP HR HCM

SAP Human Capital Management (SAP HCM)  is an important module in SAP. It is also known as SAP Human Resource Management System (SAP HRMS) or SAP Human Resource (HR). SAP HR software allows you to automate record-keeping processes. It is an ideal framework for the HR department to take advantage…
₹25,000.00

Salesforce Administrator Training

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
₹25,000.00

Salesforce Developer Training

Salesforce Developer Training Overview Salesforce Developer training advances your skills and knowledge in building custom applications on the Salesforce platform using the programming capabilities of Apex code and the Visualforce UI framework. It covers all the fundamentals of application development through real-time projects and utilizes cases to help you clear…
₹25,000.00

SAP EWM

SAP EWM stands for Extended Warehouse Management. It is a best-of-breed WMS Warehouse Management System product offered by SAP. It was first released in 2007 as a part of SAP SCM meaning Supply Chain Management suite, but in subsequent releases, it was offered as a stand-alone product. The latest version…
₹25,000.00

Oracle PL-SQL Training Program

Oracle PL-SQL is actually the number one database. The demand in market is growing equally with the value of the database. It has become necessary for the Oracle PL-SQL certification to get the right job. eLearning Solutions is one of the renowned institutes for Oracle PL-SQL in Pune. We believe…
₹25,000.00

Pega Training Courses in Pune- Get Certified Now

Course details for Pega Training in Pune Elearning solution is the best PEGA training institute in Pune. PEGA is one of the Business Process Management tool (BPM), its development is based on Java and OOP concepts. The PAGA technology is mainly used to improve business purposes and cost reduction. PEGA…
₹27,000.00

SAP PP (Production Planning) Training Institute

SAP PP Training Institute in Pune SAP PP training (Production Planning) is one of the largest functional modules in SAP. This module mainly deals with the production process like capacity planning, Master production scheduling, Material requirement planning shop floor, etc. The PP module of SAP takes care of the Master…
X
WhatsApp WhatsApp us
Call Now Button