Skip to content

Commit ad6f304

Browse files
committed
2 parents 4a085d9 + 784a923 commit ad6f304

30 files changed

+775
-166
lines changed

tutorials/cp-cf-security-xsuaa-create/cp-cf-security-xsuaa-create.md

Lines changed: 131 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,37 @@ To prevent a direct call to your application without authentication, it is neces
5050

5151
3. 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

6767
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:
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

8585
5. Save the file.
8686

@@ -92,14 +92,14 @@ Since there are now more modules used beside the express module, you have to add
9292

9393
5. 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

104104
6. Save the file.
105105

@@ -113,44 +113,44 @@ To use the XSUAA service, a file named `xs-security.json` is necessary. The file
113113

114114
8. 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

155155
9. Save the file
156156

@@ -246,69 +246,69 @@ In the manifest file you have to define a hostname for your application and add
246246

247247
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.
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

263263
4. 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

271271
5. 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

287287
6. 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

295295
7. 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

313313
8. Save the file.
314314

@@ -356,9 +356,9 @@ Because your are calling the product list over the approuter with `/products` yo
356356

357357
2. 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

363363
3. Save the file.
364364

@@ -372,9 +372,9 @@ Before you can deploy your application, you need to create the service instance
372372

373373
3. 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

379379
4. Deploy the application.
380380

@@ -388,17 +388,17 @@ Your application has two routes that are defined in the `manifest.yml`. The dire
388388
389389
3. 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

395395
1. 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

399399
1. 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
419419
7. 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

Comments
 (0)