@@ -50,37 +50,37 @@ To prevent a direct call to your application without authentication, it is neces
50
50
51
51
3 . To use additional libraries, add the following lines of code below the line ` // secure the direct call to the application `
52
52
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' );
57
57
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 ));
60
60
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
+ ```
64
64
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 .
66
66
67
67
4. To secure the product list with authorization checks, replace the line ` app.get('/products', getProducts);` in the ` index.js` file with the following code:
68
68
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
+ ` ` `
82
82
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.
84
84
85
85
5. Save the file.
86
86
@@ -92,14 +92,14 @@ Since there are now more modules used beside the express module, you have to add
92
92
93
93
5. Add the following dependencies:
94
94
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
+ ```
103
103
104
104
6. Save the file.
105
105
@@ -113,44 +113,44 @@ To use the XSUAA service, a file named `xs-security.json` is necessary. The file
113
113
114
114
8. Add the following content:
115
115
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).
154
154
155
155
9. Save the file
156
156
@@ -246,69 +246,69 @@ In the manifest file you have to define a hostname for your application and add
246
246
247
247
3. 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 .
248
248
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
+ ` ` `
262
262
263
263
4. Add the binding for the XSUAA service to your application , in the same file .
264
264
265
- ` ` ` YAML
266
- ...
267
- services :
268
- - xsuaa-service-tutorial
269
- ` ` `
265
+ ` ` ` YAML
266
+ ...
267
+ services:
268
+ - xsuaa-service-tutorial
269
+ ` ` `
270
270
271
271
5. Add the configuration data for the approuter :
272
272
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
+ ` ` `
286
286
287
287
6. Add the bindings for the XSUAA service to the approuter .
288
288
289
- ` ` ` YAML
290
- ...
291
- services :
292
- - xsuaa-service-tutorial
293
- ` ` `
289
+ ` ` ` YAML
290
+ ...
291
+ services:
292
+ - xsuaa-service-tutorial
293
+ ` ` `
294
294
295
295
7. Add a destination to the approuter .
296
296
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
+ ` ` `
308
308
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 .
310
310
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 .
312
312
313
313
8. Save the file .
314
314
@@ -356,9 +356,9 @@ Because your are calling the product list over the approuter with `/products` yo
356
356
357
357
2. Replace line 24 in the ` index.html` file with the following code .
358
358
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
+ ` ` `
362
362
363
363
3. Save the file .
364
364
@@ -372,9 +372,9 @@ Before you can deploy your application, you need to create the service instance
372
372
373
373
3. Create the XSUAA service instance with the ` xs-security.json` security descriptor file .
374
374
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
+ ` ` `
378
378
379
379
4. Deploy the application .
380
380
@@ -388,17 +388,17 @@ Your application has two routes that are defined in the `manifest.yml`. The dire
388
388
389
389
3. First make sure that your application can' t be reached on its direct URL :
390
390
391
- ` https://product-list-ap25.cfapps.eu10.hana.ondemand.com`
391
+ ` https://product-list-ap25.cfapps.eu10.hana.ondemand.com`
392
392
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` .
394
394
395
395
1. Navigate to your application with the secure route of your application router:
396
396
397
- ` https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/products`
397
+ ` https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/products`
398
398
399
399
1. Enter the e- mail and password of your trial account.
400
400
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.
402
402
403
403
### Assign the role collection
404
404
@@ -418,9 +418,9 @@ Assign your user the role collection that contains the necessary role to view th
418
418
419
419
7. Call the URL of the approuter again (you might have to delete your cookies/cache before).
420
420
421
- ` https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/products`
421
+ `https://approuter-product-list-ap25.cfapps.eu10.hana.ondemand.com/products`
422
422
423
- The application will now show you the products.
423
+ The application will now show you the products.
424
424
425
425
### Troubleshooting
426
426
0 commit comments