Authorization and identity — SAP Guided Tutorial
Free sample lesson · open to everyone
Securing CAP services declaratively with @requires and @restrict, generating xs-security. json for XSUAA, testing with mocked users, and enforcing instance-based rules such as 'users only see their own orders' on the server.
In this lessonShow sectionsHide sections
Visual overview
The situation: a real project scenario
A customer portal hid other customers' orders in the UI with a filter, but the OData service returned all orders when called directly with the same token. Adding @restrict with where: 'customer_ID = $user.customerID' moved the filter to the server, where every query — UI, $batch or direct API call — enforced it.
Explanation
Authentication proves who a user is, typically through an XSUAA or IAS token; authorization decides what that authenticated user may do, and in CAP this is handled through declarative annotations rather than ad hoc code. A service or entity can be locked down with @requires: 'authenticated-user' or a specific role name, which blocks all access until that condition is met.
Finer-grained rules come from @restrict, for example @restrict: [{ grant: 'READ', to: 'Viewer' }, { grant: '*', to: 'Admin' }], which lets you grant different operations to different roles on the same entity.
Beyond role checks, instance-based rules add a where condition such as where: 'createdBy = $user', which CAP enforces as a filter on every query for that entity, not just in the UI.
This last point matters because hiding a button or menu item in the UI is not authorization at all — a client that calls the OData service directly, with the same token, will see everything the service itself does not block. Every access rule has to be enforced in the service layer, where it cannot be bypassed.
On SAP BTP, these annotations drive the actual security configuration. Running cds compile srv --to xsuaa > xs-security.json (or the shortcut cds add xsuaa) generates the scopes and role templates straight from your @requires/@restrict annotations, so the two never drift apart as long as you regenerate rather than hand-edit.
In the BTP cockpit, those role templates become roles inside role collections, and role collections are what gets assigned to users or identity-provider groups. At runtime, the user's token carries scopes, and CAP maps those scopes back to the role names used in your annotations.
During development, cds.requires.auth.users in your configuration lets you define mocked users — for example "alice" with the "admin" role — so cds watch can simulate different roles without a real identity provider. Good testing practice is to exercise every role, including a user with no roles at all and a user restricted by instance-level attributes, and then to repeat key tests in a hybrid profile against a real XSUAA token before go-live, since mocked users can mask differences in how real tokens carry attributes.
Some rules cannot be expressed declaratively and need a programmatic check inside a handler, such as if (!req.user.is('Manager')) req.reject(403), with user attributes available via req.user.attr. These checks should be reserved for cases the annotations genuinely cannot cover, kept in one place rather than scattered across handlers, and any authorization failure you log should omit personal data so the audit trail itself does not become a privacy problem.
Working through this yourself: protect an AdminService with @requires: 'admin' and verify it rejects both an unauthenticated and a non-admin mocked user; add an instance restriction so customers can only read their own orders; generate xs-security.json and check that every scope maps to an annotation you wrote; then repeat the same tests in a hybrid profile with a real XSUAA token. You should end up able to show, for every role, one request that is allowed and one that is denied — against the service itself, not against the UI.
Code example
service OrdersService @(requires: 'authenticated-user') { entity Orders as projection on my.Orders; annotate Orders with @(restrict: [ { grant: 'READ', to: 'Customer', where: 'customer_ID = $user.customerID' }, { grant: '*', to: 'OrderAdmin' } ]);}Watch out
Common mistakes
- Relying on the UI to hide actions instead of restricting the service.
- Testing only with an all-powerful mocked user.
- Hand-maintaining xs-security.json so scopes drift from annotations.
- Trusting a user ID sent in the request body instead of req.user.
Do this
Best practices
- Default-deny: put @requires on every service and open up deliberately.
- Generate security descriptors from the CDS annotations.
- Automate positive and negative authorization tests per role in CI.
In the interview
Interview angle
Show the layers: authentication via XSUAA/IAS, role-based @requires and @restrict, instance-based where conditions with $user attributes, and how you test negative cases. Saying "the UI hides it" is a red flag in any CAP security discussion.
Practice this lesson
Original educational case studies for SAP consultants, not official SAP certification exam questions. Verify product edition and implementation details against the current SAP documentation.
Quick check
0 of 4 questions attempted
Question 1 of 4
Customers must only read their own orders. Where should this rule be enforced?
Question 2 of 4
How should xs-security.json be produced for a CAP app?
Question 3 of 4
A tester only uses a mocked user with every role. What risk remains?
Question 4 of 4
Which value should a handler trust as the current user's identity?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1How do you implement 'users only see data of their own company code' in a CAP service?
2What is the difference between authentication and authorization in a CAP app on BTP?
Was this lesson helpful?
Keep exploring
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.