SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Add User Authentication to Your Application (SAP HANA Cloud)

Define security and enable user authentication and authorization for your SAP HANA Cloud CAP application.

Overview

🎓 intermediate 20 min. SAP HANA CloudIntermediateSAP HANASAP Cloud Application Programming ModelSAP Business Application Studio

You will learn

  • โœ”How to create an instance of the User Authentication and Authorization service
  • โœ”How to incorporate security into the routing endpoint of your application
  • โœ”How to configure Cloud Application Programming (CAP) service authentication

Prerequisites

Prerequisites

  • This tutorial is designed for SAP HANA Cloud. It is not designed for SAP HANA on premise or SAP HANA, express edition.
  • You have created database artifacts, loaded data, and added basic UI as explained in the previous tutorial.

Steps

Video Version

Video tutorial version:

Step 1 Create XSUAA configuration
โ€”

We are going to set up production level security using the SAP Authorization and Trust Management service for SAP BTP in the Cloud Foundry environment and more specifically the User Account and Authorization or UAA Service. By default CAP allows you to mock your security for testing during development (which we used in the last tutorial). However we also want to teach you how to setup the full production security and test that during development as well.

The UAA will provide user identity, as well as assigned roles and user attributes. This is done in the form of a JWT token in the Authorization header of the incoming HTTP request. We will need the Application Router we added to our application in the last tutorial to perform the redirect to the UAA Login Page and then forward this JWT token to your CAP service. Therefore this will be a multiple step process.

  1. In the previous tutorial, we used an Application Router in our project. When we did, the wizard created an xs-security.json file in the root of the project. This file is used during the creation or update of the XSUAA service instance and controls the roles, scopes, attributes and role templates that will be part of the security for your application. What was generated was a basic version of the xs-security.json that will only require authentication but not specific roles.

    Basic xs-security.json
    Basic xs-security.json

  2. To really test the impact of roles in our application, lets add some security to our services. Open the interaction_srv.cds from the srv folder. Adjust the code as follows to make Interactions_Header service only available to authenticated users and Interactions_Items only available to users with the Admin role and restrict the results during certain read operations to only those records where the Country column has the value of German (DE).

    CDS
    using app.interactions from '../db/interactions';
    using {sap} from '@sap/cds-common-content';
    
    service CatalogService {
    
    @requires           : 'authenticated-user'
    @cds.redirection.target
    @odata.draft.enabled: true
    entity Interactions_Header as projection on interactions.Headers;
    
    @requires: 'Admin'
    entity Interactions_Items  as projection on interactions.Items;
    
    @readonly
    entity Languages           as projection on sap.common.Languages;
    
    @readonly
    @restrict: [{ grant: 'READ', where: 'country_code = ''DE'''}]
    entity HeaderView as projection on interactions.Headers;
    
    }
  3. When you do add scopes to the services as we did in the previous step you can generate a sample xs-security.json using the following command and merge that into the basics xs-security.json file generated by the Application Router wizard.

    Shell
    cds compile srv/ --to xsuaa > xs-security.json

    Updated xs-security.json
    Updated xs-security.json

  4. Since we want to test the security setup from the Business Application Studio, we are going to have add some additional configuration to the xs-security.json. You need to add another property to the xs-security.json to configure which redirect URIs are allowed by the OAuth configuration. Also while editing, add an xsappname with the value myhanaapp (plus your group number or intials if you in a group workshop) and a tenant-mode of dedicated as well. We can also add credential-types as a security best practice. You can read more about the Credential Types in this blog post by Dinu PAVITHRAN

    JSON
    {
    "xsappname": "myhanaapp",
    "tenant-mode": "dedicated",
    "scopes": [
        {
            "name": "$XSAPPNAME.Admin",
            "description": "Admin"
        }
    ],
    "attributes": [],
    "role-templates": [
        {
            "name": "Admin",
            "description": "generated",
            "scope-references": [
                "$XSAPPNAME.Admin"
            ],
            "attribute-references": []
        }
    ],
    "oauth2-configuration": {
        "credential-types": [
            "binding-secret",
            "x509"
        ],
        "redirect-uris": [
            "https://*.applicationstudio.cloud.sap/**"
        ]
    }
    }

    oauth2-configuration in the xs-security.json
    oauth2-configuration in the xs-security.json

    This wild card will allow testing from the Application Studio by telling the XSUAA it should allow authentication requests from this URL. See section Application Security Descriptor Configuration Syntax for more details on configuration options.

  5. Open a terminal and create the XSUAA services instance with the xs-security.json configuration using the following command (adjusting MyHANAApp-auth to include your group number or intitials if you are in a group workshop like we did earlier):

    Shell
    cf create-service xsuaa application MyHANAApp-auth -c xs-security.json

    Create XSUAA service
    Create XSUAA service

  6. Finally return the package.json file in the root. In the last tutorial we changed the authentication configuration to mocked. Now we can change it back to xsuaa.

    XSUAA back in the package.json
    XSUAA back in the package.json

Step 2 Configure the application
+
Step 3 Create and grant roles for application
+
Step 4 Adjust Application Router
+
Step 5 Test
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 5
1. Create XSUAA configuration 2. Configure the application 3. Create and grant roles for application 4. Adjust Application Router 5. Test

Learn more →