Skip to content

Commit 53e23da

Browse files
committed
Node and Python tutorials are updated
1 parent 583eccc commit 53e23da

2 files changed

Lines changed: 75 additions & 46 deletions

File tree

tutorials/btp-cf-buildpacks-node-create/btp-cf-buildpacks-node-create.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tags: [ tutorial>beginner, software-product>sap-btp--cloud-foundry-environment,
1010
primary_tag: programming-tool>node-js
1111
---
1212

13+
1314
## You will learn
1415
- How to create a simple "Hello World" application in Node.js
1516
- How to create an application router for it
@@ -19,13 +20,17 @@ primary_tag: programming-tool>node-js
1920
- 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).
2021
- You have created a subaccount and a space on Cloud Foundry Environment.
2122
- [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.
2324
- You have installed an integrated development environment, for example [Visual Studio Code] (https://code.visualstudio.com/).
2425

2526
## 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+
---
2730

2831
### Log on to SAP BTP
32+
33+
2934
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)
3035

3136
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.
4954
5055
5156
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.
5259
5360
5461
#### RESULT
62+
5563
Details about your personal SAP BTP subaccount are displayed (API endpoint, user, organization, space).
5664
5765
5866
### Create a Node.js application
67+
68+
5969
You're going to create a simple Node.js application.
6070
6171
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.
127137
"description": "My simple Node.js app",
128138
"main": "index.js",
129139
"engines": {
130-
"node": "14.x.x"
140+
"node": "16.x.x"
131141
},
132142
"scripts": {
133143
"start": "node start.js"
@@ -140,7 +150,7 @@ You're going to create a simple Node.js application.
140150
}
141151
```
142152
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:
144154
145155
```JavaScript
146156
const express = require('express');
@@ -173,12 +183,15 @@ You're going to create a simple Node.js application.
173183
That is: `https://node-1234-aaaa-5678.cfapps.eu20.hana.ondemand.com`
174184
175185
#### RESULT
186+
176187
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.
177188
178189
179190
180191
181192
### Run an Authentication Check
193+
194+
182195
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.
183196
184197
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
190203
}
191204
```
192205
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:
194207
195208
196209
```JSON
@@ -199,8 +212,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
199212
"tenant-mode" : "dedicated",
200213
"oauth2-configuration": {
201214
"redirect-uris": [
202-
"https://node-1234-aaaa-5678.cfapps.eu20.hana.ondemand.com/",
203-
"https://web-1234-aaaa-5678.cfapps.eu20.hana.ondemand.com/"
215+
"https://*.cfapps.eu20.hana.ondemand.com/**"
204216
]
205217
}
206218
}
@@ -374,6 +386,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
374386
> 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.
375387
376388
#### RESULT
389+
377390
- 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.
378391
379392
- 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
382395
383396
384397
### Run an Authorization Check
398+
399+
385400
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)
386401
387402
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
431446
cf update-service nodeuaa -c xs-security.json
432447
```
433448
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:
435450
436451
```JSON
437452
[{
@@ -501,7 +516,7 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
501516
});
502517
```
503518
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)
505520
506521
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.
507522
@@ -576,13 +591,16 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
576591
8. Try to access `myapp` again (in a browser) in both ways – directly and through the `web` application router.
577592
578593
#### RESULT
594+
579595
- When you access it directly, you should still get a response `401 Unauthorized`. This is a correct and expected behavior.
580596
- 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.
581597
582598
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.
583599
584600
585601
### Assigning Roles to a User in SAP BTP Cockpit
602+
603+
586604
1. Open the SAP BTP cockpit and go to your subaccount.
587605
588606
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
613631
614632
615633
#### RESULT
634+
616635
Accessing the `myapp` application results in the following:
617636
618637
- 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.
619638
620639
- 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.
640+
641+
642+
---

0 commit comments

Comments
 (0)