Technical Articles
[SAP Cloud Platform on Alibaba Cloud series] Develop Python App with Authentication and Authorization in Cloud Foundry – Part 2
This topic Develop Python App with Authentication and Authorization in Cloud Foundry will guide you through creating a Python application, setting up authentication checks and authorization checks in Cloud Foundry (for ease of reading “CF).
Since this is a large topic, in order to give you a better reading experience, I would like to divide it into 3 parts:
This blog post is Part 2.
. W
What is Application Router
- 1 Application Router for 1 business app
- The single entry point to the outside
- Serve as a reverse proxy to rewrite URL
Main functions of Application Router:
- Handles authentication for all apps of the application
- Authorization check
- Serves static resources
- Performs route mapping (URL mapping)
- Talk to XSUAA: In case of multi-tenancy, it derives the tenant information from the URL and provides it to the XSUAA, to redirect the authentication request to the tenant-specific IdP.
xs-security.json
{
"xsappname":"myapp",
"tenant-mode":"dedicated",
"oauth2-configuration":{
"redirect-uris":[
"https://*.<custom-domain>/**"
]
}
}
custom-domain> with the domain available in your org, you can check it by executing CF command cf domains
For example:
{
"xsappname":"myapp",
"tenant-mode":"dedicated",
"oauth2-configuration":{
"redirect-uris":[
"https://*.apps.sap-samples.scpcloud.top/**"
]
}
}
myuaa
cf create-service xsuaa application myuaa -c xs-security.json
You can check it either in the Cockpit or through CF command:
Step 2:
myuaa
service instance into the manifest.yml
---
applications:
- name: myapp
host: <host>
path: .
domain: <custom-domain>
memory: 128M
command: python server.py
services:
- myuaa
myuaa
service instance will be bound to the myapp
Step 3:
Create a directory called approuter
in the python-with-xsuaa
directory.
approuter
directory, create a sub-directory named resources
resources
, create an index.html
<html>
<head>
<title>Python with XSUAA</title>
</head>
<body>
<h1>Python with XSUAA</h1>
<a href="/myapp/">myapp</a>
</body>
</html>
package.json
file in the approuter
npm init
approuter
packages into the approuter/node_modules/@sap
npm install @sap/approuter --save
package.json
file in the approuter
"scripts": {
"start": "node node_modules/@sap/approuter/approuter.js"
}
For example:
{
"name": "approuter",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node node_modules/@sap/approuter/approuter.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@sap/approuter": "^8.0.0"
}
}
manifest.yml
file in the python-with-xsuaa
---
applications:
- name: myapp
host: <host>
path: .
domain: <custom-domain>
memory: 128M
command: python server.py
services:
- myuaa
- name: approuter
host: <host>
path: approuter
domain: <custom-domain>
memory: 128M
env:
destinations: >
[
{
"name":"myapp",
"url":"<myapp-url>",
"forwardAuthToken": true
}
]
services:
- myuaa
host> with a unique name, so it does not clash with other deployed applications.
custom-domain> with the domain available in your org, you can check it by executing CF command cf domains
For example:
---
applications:
- name: myapp
host: myapp-ixxxxxx
path: .
domain: apps.sap-samples.scpcloud.top
memory: 128M
command: python server.py
services:
- myuaa
- name: approuter
host: approuter-ixxxxxx
path: approuter
domain: apps.sap-samples.scpcloud.top
memory: 128M
env:
destinations: >
[
{
"name":"myapp",
"url":"https://myapp-ixxxxxx.apps.sap-samples.scpcloud.top",
"forwardAuthToken": true
}
]
services:
- myuaa
xs-app.json
file in the approuter
{
"routes": [
{
"source": "^/myapp/(.*)$",
"target": "$1",
"destination": "myapp"
}
]
}
With this configuration, the incoming request path is connected with the destination where the request should be forwarded to. By default, every route requires OAuth authentication, so the requests to this path will require an authenticated user.
Step 4:
python-with-xsuaa
cf push
myapp
application and deploy the new approuter
application as well.
approuter
application will be requested instead of the myapp
URL. It will then forward the requests to the myapp
Step 5: Access the application
approuter
cf apps
approuter
approuter
myapp
link, you can see your myapp
Conclusion
This blog post shared how to leverage the Application Router and the XSUAA service in SAP Cloud Platform to realize authentication onto an existed app.
Special point for Alibaba Cloud:
For Alibaba Cloud, custom domain must be consumed, so the redirect URL redirect-uris
needs to be specified in xs-security.json
file.
The next part 3 will be published later, it will not take a long time, let’s stay tuned.
Part 3:
If you would like to get more step-by-step hands-ons on SAP Cloud Platform Alibaba Cloud, please follow me!
Hi,
Just a mention. I think there miss a command for approuter. That should be npm run start. Thank you for your share!
Best Regards,
Kris
Thanks,my npm part failed,but very helpful!