@@ -50,37 +50,37 @@ To prevent a direct call to your application without authentication, it is neces
5050
51513 . To use additional libraries, add the following lines of code below the line ` // secure the direct call to the application `
5252
53- ``` JavaScript
54- const passport = require (' passport' );
55- const { JWTStrategy } = require (' @sap/xssec' );
56- const xsenv = require (' @sap/xsenv' );
53+ ``` JavaScript
54+ const passport = require (' passport' );
55+ const { JWTStrategy } = require (' @sap/xssec' );
56+ const xsenv = require (' @sap/xsenv' );
5757
58- // XSUAA Middleware
59- passport .use (new JWTStrategy (xsenv .getServices ({uaa: {tag: ' xsuaa' }}).uaa ));
58+ // XSUAA Middleware
59+ passport .use (new JWTStrategy (xsenv .getServices ({uaa: {tag: ' xsuaa' }}).uaa ));
6060
61- app .use (passport .initialize ());
62- app .use (passport .authenticate (' JWT' , { session: false }));
63- ```
61+ app .use (passport .initialize ());
62+ app .use (passport .authenticate (' JWT' , { session: false }));
63+ ```
6464
65- This code prevents direct calls to the product list application without a valid JWT.
65+ This code prevents direct calls to the product list application without a valid JWT .
6666
67674. To secure the product list with authorization checks, replace the line ` app.get('/products', getProducts);` in the ` index.js` file with the following code:
6868
69- ``` JavaScript
70- app .get (' /products' , checkReadScope, getProducts);
71-
72- // Scope check
73- function checkReadScope (req , res , next ) {
74- if (req .authInfo .checkLocalScope (' read' )) {
75- return next ();
76- } else {
77- console .log (' Missing the expected scope' );
78- res .status (403 ).end (' Forbidden' );
79- }
80- }
81- ```
69+ ` ` ` JavaScript
70+ app.get('/products', checkReadScope, getProducts);
71+
72+ // Scope check
73+ function checkReadScope(req, res, next) {
74+ if (req.authInfo.checkLocalScope('read')) {
75+ return next();
76+ } else {
77+ console.log('Missing the expected scope');
78+ res.status(403).end('Forbidden');
79+ }
80+ }
81+ ` ` `
8282
83- The ` checkReadScope ` function ensures that only a user with the correct authorizations can look at the products.
83+ The ` checkReadScope` function ensures that only a user with the correct authorizations can look at the products.
8484
85855. Save the file.
8686
@@ -92,14 +92,14 @@ Since there are now more modules used beside the express module, you have to add
9292
93935. Add the following dependencies:
9494
95- ``` JSON
96- "dependencies" : {
97- "express" : " ^4.17.1" ,
98- "@sap/xsenv" : " ^3.1.0" ,
99- "@sap/xssec" : " ^3.0.10" ,
100- "passport" : " ^0.4.1"
101- }
102- ```
95+ ```JSON
96+ "dependencies": {
97+ "express": "^4.17.1",
98+ "@sap/xsenv": "^3.1.0",
99+ "@sap/xssec": "^3.0.10",
100+ "passport": "^0.4.1"
101+ }
102+ ```
103103
1041046. Save the file.
105105
@@ -113,44 +113,44 @@ To use the XSUAA service, a file named `xs-security.json` is necessary. The file
113113
1141148. Add the following content:
115115
116- ``` JSON
117- {
118- "xsappname" : " product-list" ,
119- "tenant-mode" : " dedicated" ,
120- "scopes" : [
121- {
122- "name" : " $XSAPPNAME.read" ,
123- "description" : " With this scope, USER can read products."
124- }
125- ],
126-
127- "role-templates" : [
128- {
129- "name" : " Viewer" ,
130- "description" : " Role to get the list of products" ,
131- "scope-references" : [
132- " $XSAPPNAME.read"
133- ]
134- }
135- ],
136- "role-collections" : [
137- {
138- "name" : " ProductListViewer" ,
139- "description" : " Product List Viewer" ,
140- "role-template-references" : [
141- " $XSAPPNAME.Viewer"
142- ]
143- }
144- ],
145- "oauth2-configuration" :
146- {
147- "redirect-uris" : [" https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/login/callback" ]
148- }
149- }
150- ```
151-
152- This creates a role collection with a role template and a role with a reading scope, so a user with this role can view the products.
153- It also adds the redirect URI parameter, which calls the URL of the application router that you will create in the next step. For more information, see [ Listing Allowed Redirect URIs] ( https://help.sap.com/docs/btp/sap-business-technology-platform/security-considerations-for-sap-authorization-and-trust-management-service#loio88b7d9d4c6ff4498b48dbc0b7be8a294 ) .
116+ ```JSON
117+ {
118+ "xsappname": "product-list",
119+ "tenant-mode": "dedicated",
120+ "scopes": [
121+ {
122+ "name": "$XSAPPNAME.read",
123+ "description": "With this scope, USER can read products."
124+ }
125+ ],
126+
127+ "role-templates": [
128+ {
129+ "name": "Viewer",
130+ "description": "Role to get the list of products",
131+ "scope-references": [
132+ "$XSAPPNAME.read"
133+ ]
134+ }
135+ ],
136+ "role-collections": [
137+ {
138+ "name": "ProductListViewer",
139+ "description": "Product List Viewer",
140+ "role-template-references": [
141+ "$XSAPPNAME.Viewer"
142+ ]
143+ }
144+ ],
145+ "oauth2-configuration":
146+ {
147+ "redirect-uris": ["https:// approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/login/callback"]
148+ }
149+ }
150+ ```
151+
152+ This creates a role collection with a role template and a role with a reading scope, so a user with this role can view the products.
153+ It also adds the redirect URI parameter, which calls the URL of the application router that you will create in the next step. For more information, see [Listing Allowed Redirect URIs](https : // help.sap.com/docs/btp/sap-business-technology-platform/security-considerations-for-sap-authorization-and-trust-management-service#loio88b7d9d4c6ff4498b48dbc0b7be8a294).
154154
1551559. Save the file
156156
@@ -246,69 +246,69 @@ In the manifest file you have to define a hostname for your application and add
246246
2472473. Give your application a specific host name with the parameter ` route` . ** The route has to be unique in the whole Cloud Foundry landscape ** , so make sure to add a random part to the route , for example your initials and your day of birth , like ` product-list-ap25` and ` approuter-product-list-ap25` . You also need the route to configure a destination later .
248248
249- ``` YAML
250- applications :
251- # Product List Application
252- - name : product-list
253- instances : 1
254- memory : 128M
255- routes :
256- - route : product-list-ap25.cfapps.eu10.hana.ondemand.com
257- path : myapp
258- buildpacks :
259- - nodejs_buildpack
260- timeout : 180
261- ` ` `
249+ ` ` ` YAML
250+ applications:
251+ # Product List Application
252+ - name: product-list
253+ instances: 1
254+ memory: 128M
255+ routes:
256+ - route: product-list-ap25.cfapps.eu10.hana.ondemand.com
257+ path: myapp
258+ buildpacks:
259+ - nodejs_buildpack
260+ timeout: 180
261+ ` ` `
262262
2632634. Add the binding for the XSUAA service to your application , in the same file .
264264
265- ` ` ` YAML
266- ...
267- services :
268- - xsuaa-service-tutorial
269- ` ` `
265+ ` ` ` YAML
266+ ...
267+ services:
268+ - xsuaa-service-tutorial
269+ ` ` `
270270
2712715. Add the configuration data for the approuter :
272272
273- ` ` ` YAML
274- applications :
275- ...
276-
277- # Application Router
278- - name : approuter
279- routes :
280- - route : approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com
281- path : approuter
282- buildpacks :
283- - nodejs_buildpack
284- memory : 128M
285- ` ` `
273+ ` ` ` YAML
274+ applications:
275+ ...
276+
277+ # Application Router
278+ - name: approuter
279+ routes:
280+ - route: approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com
281+ path: approuter
282+ buildpacks:
283+ - nodejs_buildpack
284+ memory: 128M
285+ ` ` `
286286
2872876. Add the bindings for the XSUAA service to the approuter .
288288
289- ` ` ` YAML
290- ...
291- services :
292- - xsuaa-service-tutorial
293- ` ` `
289+ ` ` ` YAML
290+ ...
291+ services:
292+ - xsuaa-service-tutorial
293+ ` ` `
294294
2952957. Add a destination to the approuter .
296296
297- ` ` ` YAML
298- # Application Router
299- ...
300- env :
301- destinations : >
302- [
303- {"name":"products-destination",
304- "url":"https://product-list-ap25.cfapps.eu10.hana.ondemand.com",
305- "forwardAuthToken": true}
306- ]
307- ` ` `
297+ ` ` ` YAML
298+ # Application Router
299+ ...
300+ env:
301+ destinations: >
302+ [
303+ {"name":"products-destination",
304+ "url":"https://product-list-ap25.cfapps.eu10.hana.ondemand.com",
305+ "forwardAuthToken": true}
306+ ]
307+ ` ` `
308308
309- The ` name` parameter is the same as previously defined in the file `xs-app.json`. the `url` parameter is the result of the host name of your application and the region of your Cloud Foundry landscape (`https://<hostname>.cfapps.<region>.hana.ondemand.com`). The `forwardAuthToken` parameter set to true ensures that the approuter forwards the JWT token to the destination.
309+ The ` name` parameter is the same as previously defined in the file ` xs-app.json` . the ` url` parameter is the result of the host name of your application and the region of your Cloud Foundry landscape (` https://<hostname>.cfapps.<region>.hana.ondemand.com` ). The ` forwardAuthToken` parameter set to true ensures that the approuter forwards the JWT token to the destination .
310310
311- Ensure that the landscape mentioned in the route is the same as in the previous steps.
311+ Ensure that the landscape mentioned in the route is the same as in the previous steps .
312312
3133138. Save the file .
314314
@@ -356,9 +356,9 @@ Because your are calling the product list over the approuter with `/products` yo
356356
3573572. Replace line 24 in the ` index.html` file with the following code .
358358
359- ` ` ` JavaScript
360- var productsUrl = "/products/products"; // contains path mapping which is specified in xs-app.json
361- ` ` `
359+ ` ` ` JavaScript
360+ var productsUrl = "/products/products"; // contains path mapping which is specified in xs-app.json
361+ ` ` `
362362
3633633. Save the file .
364364
@@ -372,9 +372,9 @@ Before you can deploy your application, you need to create the service instance
372372
3733733. Create the XSUAA service instance with the ` xs-security.json` security descriptor file .
374374
375- ` ` ` Bash
376- cf create-service xsuaa application xsuaa-service-tutorial -c security/xs-security.json
377- ` ` `
375+ ` ` ` Bash
376+ cf create-service xsuaa application xsuaa-service-tutorial -c security/xs-security.json
377+ ` ` `
378378
3793794. Deploy the application .
380380
@@ -388,17 +388,17 @@ Your application has two routes that are defined in the `manifest.yml`. The dire
388388
3893893. First make sure that your application can' t be reached on its direct URL :
390390
391- ` https://product-list-ap25.cfapps.eu10.hana.ondemand.com`
391+ ` https://product-list-ap25.cfapps.eu10.hana.ondemand.com`
392392
393- If everything is working correctly, this will result in an error message reading `unauthorized`.
393+ If everything is working correctly, this will result in an error message reading ` unauthorized` .
394394
3953951. Navigate to your application with the secure route of your application router:
396396
397- ` https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/products`
397+ ` https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/products`
398398
3993991. Enter the e- mail and password of your trial account.
400400
401- You should see the `no data` message. This is because you don't have the role assigned yet to view the products. You will do this in the next step.
401+ You should see the ` no data` message . This is because you don' t have the role assigned yet to view the products. You will do this in the next step.
402402
403403### Assign the role collection
404404
@@ -418,9 +418,9 @@ Assign your user the role collection that contains the necessary role to view th
418418
4194197. Call the URL of the approuter again (you might have to delete your cookies/cache before).
420420
421- ` https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/products`
421+ `https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/products`
422422
423- The application will now show you the products.
423+ The application will now show you the products.
424424
425425### Troubleshooting
426426
0 commit comments