Create an xs-security.json, bind XSUAA, obtain a service key, and configure local .env for development.
xs-security.json file — basic structureXSUAA is the gate between the internet and your CAP app. Setting it up correctly — with the right scopes, the right oauth2-configuration, and the right service key — is the foundation for all authorization work. One misconfiguration here causes 401/403 errors everywhere.
application (for browser + CAP apps)bookshop-xsuaa)xs-security.json as service parameters{
"xsappname": "bookshop",
"tenant-mode": "dedicated",
"scopes": [
{ "name": "$XSAPPNAME.Admin", "description": "Full access" },
{ "name": "$XSAPPNAME.Buyer", "description": "Buy books" },
{ "name": "$XSAPPNAME.Reader", "description": "Browse catalog" }
],
"role-templates": [
{
"name": "Admin",
"description": "Administrator role",
"scope-references": ["$XSAPPNAME.Admin"]
},
{
"name": "Buyer",
"description": "Buyer role",
"scope-references": ["$XSAPPNAME.Buyer", "$XSAPPNAME.Reader"]
}
],
"oauth2-configuration": {
"token-validity": 43200,
"redirect-uris": ["https://your-app.cfapps.eu10.hana.ondemand.com/**"]
}
}
# Create XSUAA service instance
cf create-service xsuaa application bookshop-xsuaa \
-c xs-security.json
# Update after changing xs-security.json
cf update-service bookshop-xsuaa -c xs-security.json
# Create a service key for local development
cf create-service-key bookshop-xsuaa bookshop-xsuaa-key
cf service-key bookshop-xsuaa bookshop-xsuaa-key
# Copy the credentials JSON
# mta.yaml
modules:
- name: bookshop-srv
type: nodejs
requires:
- name: bookshop-xsuaa # binds the XSUAA service
resources:
- name: bookshop-xsuaa
type: org.cloudfoundry.managed-service
parameters:
service: xsuaa
service-plan: application
path: ./xs-security.json
# Create .env with VCAP_SERVICES from the service key
export XSUAA_CREDENTIALS=$(cf service-key bookshop-xsuaa bookshop-xsuaa-key | tail -n +2)
# Or write to .env (add to .gitignore!)
echo "VCAP_SERVICES=$(cf service-key bookshop-xsuaa bookshop-xsuaa-key | tail -n +2 | \
python3 -c 'import sys,json; creds=json.load(sys.stdin); print(json.dumps({"xsuaa":[{"credentials":creds}]}))')" > .env
// .cdsrc.json
{
"requires": {
"auth": {
"[production]": {
"kind": "xsuaa",
"vcap": { "label": "xsuaa" }
}
}
}
}
# Get a JWT for service-to-service testing
CLIENT_ID=<from service key>
CLIENT_SECRET=<from service key>
TOKEN_URL=<from service key uaadomain>/oauth/token
curl -s -X POST "$TOKEN_URL" \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-d "grant_type=client_credentials&response_type=token" \
| jq .access_token
### Use the token in a request
GET http://localhost:4004/catalog/Books
Authorization: Bearer <access_token>
# Regenerate the client secret (rotates credentials)
cf delete-service-key bookshop-xsuaa bookshop-xsuaa-key
cf create-service-key bookshop-xsuaa bookshop-xsuaa-key
# Re-create .env with new credentials
.envcds watch with production auth profile and request a JWT via client credentialsreq.user is populatedxsappname in xs-security.json is the prefix for all scope namesVCAP_SERVICEScf update-service -c xs-security.json applies changes to an existing instance