You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- You have a trial or a productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
20
21
- You have created a subaccount and a space on Cloud Foundry Environment.
21
22
-[cf CLI] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/4ef907afb1254e8286882a2bdef0edf4.html) is installed locally.
22
-
-[Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally. Make sure you have the latest Node.js version. In this tutorial, we use v.18.
23
+
-[Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally. Make sure you have the latest Node.js version. In this tutorial, we use v.16.
23
24
- You have installed an integrated development environment, for example [Visual Studio Code] (https://code.visualstudio.com/).
24
25
25
26
## Intro
26
-
This tutorial will guide you through creating and setting up a simple Node.js application by using cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through another one - a web microservice (application router).
27
+
This tutorial will guide you through creating and setting up a simple Node.js application in cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through a web microservice (application router). Finally, you will set authentication checks and authorization roles to properly access and manage your web application.
28
+
29
+
---
27
30
28
31
### Log on to SAP BTP
32
+
33
+
29
34
First, you need to connect to the SAP BTP, Cloud Foundry environment with your productive subaccount. Your Cloud Foundry URL depends on the region where the API endpoint belongs to. To find out which one is yours, see: [Regions and API Endpoints Available for the CF Environment] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/f344a57233d34199b2123b9620d0bb41.html?version=Cloud)
30
35
31
36
In this tutorial, we use `eu20.hana.ondemand.com` as an example.
@@ -49,13 +54,18 @@ In this tutorial, we use `eu20.hana.ondemand.com` as an example.
49
54
50
55
51
56
5. Choose the org name and space where you want to create your application.
57
+
58
+
> This step is skipped if you're using a trial account.
52
59
53
60
54
61
#### RESULT
62
+
55
63
Details about your personal SAP BTP subaccount are displayed (API endpoint, user, organization, space).
56
64
57
65
58
66
### Create a Node.js application
67
+
68
+
59
69
You're going to create a simple Node.js application.
60
70
61
71
1. In your local file system, create a new directory (folder). For example: `node-tutorial`
@@ -127,7 +137,7 @@ You're going to create a simple Node.js application.
127
137
"description": "My simple Node.js app",
128
138
"main": "index.js",
129
139
"engines": {
130
-
"node": "14.x.x"
140
+
"node": "16.x.x"
131
141
},
132
142
"scripts": {
133
143
"start": "node start.js"
@@ -140,7 +150,7 @@ You're going to create a simple Node.js application.
140
150
}
141
151
```
142
152
143
-
8. Inside the `myapp` folder, create another file called `start.js` with the following content:
153
+
8. Inside the `myapp` folder, create a file `start.js` with the following content:
144
154
145
155
```JavaScript
146
156
const express = require('express');
@@ -173,12 +183,15 @@ You're going to create a simple Node.js application.
173
183
That is: `https://node-1234-aaaa-5678.cfapps.eu20.hana.ondemand.com`
174
184
175
185
#### RESULT
186
+
176
187
Your Node.js application is successfully deployed and running on the SAP BTP, Cloud Foundry environment. A **Hello World!** message is displayed in the browser.
177
188
178
189
179
190
180
191
181
192
### Run an Authentication Check
193
+
194
+
182
195
Authentication in the SAP BTP, Cloud Foundry environment is provided by the Authorization and Trust Management (XSUAA) service. In this example, OAuth 2.0 is used as the authentication mechanism. The simplest way to add authentication is to use the Node.js `@sap/approuter` package. To do that, a separate Node.js micro-service will be created, acting as an entry point for the application.
183
196
184
197
1. In the `node-tutorial` folder, create an `xs-security.json` file for your application with the following content:
@@ -190,7 +203,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
190
203
}
191
204
```
192
205
193
-
> **IMPORTANT**: For trial accounts, enter the following additional `oauth2-configuration` lines in your `xs-security.json` file:
206
+
> **NOTE**: For trial accounts, enter the following additional `oauth2-configuration` lines in your `xs-security.json` file:
194
207
195
208
196
209
```JSON
@@ -199,8 +212,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
@@ -374,6 +386,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
374
386
> Both the `myapp` and `web` applications are bound to the same Authorization and Trust Management (XSUAA) service instance `nodeuaa`. In this scenario, the authentication is handled by XSUAA through the application router.
375
387
376
388
#### RESULT
389
+
377
390
- Click the `My Node.js Application` link. The browser window displays **Application user:** `<e-mail>`, showing the email you have used for your Cloud Foundry logon.
378
391
379
392
- Check that the `myapp` application is not accessible without authentication. To do that, refresh its previously loaded URL in a web browser – you should get a response `401 Unauthorized`.
@@ -382,6 +395,8 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
382
395
383
396
384
397
### Run an Authorization Check
398
+
399
+
385
400
Authorization in the SAP BTP, Cloud Foundry environment is also provided by the XSUAA service. In the previous example, the `@sap/approuter` package was added to provide a central entry point for the business application and to enable authentication. Now to extend the example, authorization will be added through the implementation of a `users` REST service. Different authorization checks will be introduced for the GET and CREATE operations to demonstrate how authorization works. The authorization concept includes elements such as roles, scopes, and attributes provided in the security descriptor file `xs-security.json`. For more information, see: [Application Security Descriptor Configuration Syntax] (https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/517895a9612241259d6941dbf9ad81cb.html?version=Cloud)
386
401
387
402
1. To introduce application roles, open the `xs-security.json` in the `node-tutorial` folder, and add scopes and role templates as follows:
@@ -431,7 +446,7 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
431
446
cf update-service nodeuaa -c xs-security.json
432
447
```
433
448
434
-
3. In the `myapp` folder, create a new file called `users.json` with the following content:
449
+
3. In the `myapp` folder, create a file `users.json` with the following content:
435
450
436
451
```JSON
437
452
[{
@@ -501,7 +516,7 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
501
516
});
502
517
```
503
518
504
-
> Authorization checks are enforced by the `xssec` package in the `@sap` directory. To every request object, using `passport` and `xssec.JWTStrategy`, a security context is attached as an `authInfo` object. The resulting request object is initialized with the incoming JWT token. To check the full list of methods and properties of the security context, see: [Authentication for Node.js Applications] (https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/4902b6e66cbd42648b5d9eaddc6a363d.html?version=Cloud)
519
+
> **NOTE:** Authorization checks are enforced by the `xssec` package in the `@sap` directory. To every request object, using `passport` and `xssec.JWTStrategy`, a security context is attached as an `authInfo` object. The resulting request object is initialized with the incoming JWT token. To check the full list of methods and properties of the security context, see: [Authentication for Node.js Applications] (https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/4902b6e66cbd42648b5d9eaddc6a363d.html?version=Cloud)
505
520
506
521
As defined in the `start.js` file, for HTTP GET requests users need the `Display` scope to be authorized. For HTTP POST requests, they need to have the `Update` scope assigned.
507
522
@@ -576,13 +591,16 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
576
591
8. Try to access `myapp` again (in a browser) in both ways – directly and through the `web` application router.
577
592
578
593
#### RESULT
594
+
579
595
- When you access it directly, you should still get a response `401 Unauthorized`. This is a correct and expected behavior.
580
596
- When you access the `web` application and click the `Show users` link, it should result in a `403 Forbidden` response due to missing permissions. The same error is thrown if you try to add a new user.
581
597
582
598
To get permissions, you need to create a role collection containing the roles `Viewer` and `Manager` and assign these roles to your user. You can do this only from the SAP BTP cockpit.
583
599
584
600
585
601
### Assigning Roles to a User in SAP BTP Cockpit
602
+
603
+
586
604
1. Open the SAP BTP cockpit and go to your subaccount.
587
605
588
606
2. From the left-side menu, navigate to `Security` > `Role Collections`.
@@ -613,8 +631,12 @@ To get permissions, you need to create a role collection containing the roles `V
613
631
614
632
615
633
#### RESULT
634
+
616
635
Accessing the `myapp` application results in the following:
617
636
618
637
- If you try to access it directly, a response `401 Unauthorized` is still displayed due to lack or permissions (roles). This is a correct and expected behavior.
619
638
620
639
- If you try to access it through the `web` application router, the `Show users` link will show the list of users - **John** and **Paula**. If you enter a new name, it will be successfully recorded in the user database.
0 commit comments