
@Sisn/approuter
package was added to provide a central entry point for the business application and enable authentication. Now to extend our sample app, authorization will be added. The authorization concept includes elements such as Roles, Scopes, and Attributes provided in the security descriptor file xs-security.json
of XSUAA, more details can be found here: What Is Authorization and Trust Management.cfenv
. sap_xssec
security library can do this.requirements.txt
file as below (you can specify the version number or not):Flask==1.1.0
cfenv==0.5.3
sap_xssec
sap_xssec
and other SAP developed Python modules:Access downloads
located under Support Packages and Patches
,XS PYTHON
,XS PYTHON 1.0
component.XS_PYTHON
archive and extract it in a local directory, for example: sap_dependencies
sap_xssec
by executing the following command from the root of the application:pip download -d vendor -r requirements.txt --find-links ./sap_dependencies
xs-security.json
file in the python-with-xsuaa
directory with scopes
and role-templates
section as below:{
"xsappname":"myapp",
"tenant-mode":"dedicated",
"scopes":[
{
"name":"$XSAPPNAME.Display",
"description":"display"
}
],
"role-templates":[
{
"name":"Viewer",
"description":"View Hello World",
"scope-references":[
"$XSAPPNAME.Display"
]
}
],
"oauth2-configuration":{
"redirect-uris":[
"https://*.<custom-domain>/**"
]
}
}
myuaa
via the following command:cf update-service myuaa -c xs-security.json
xs-app.json
file in the approuter
directory with scope
as below:{
"routes": [
{
"source": "^/myapp/(.*)$",
"target": "$1",
"destination": "myapp",
"scope": "$XSAPPNAME.Display"
}
]
}
cf push
approuter
application and click into the myapp
link, you should see 403 Forbidden
since you haven't assigned the role you defined to yourself yet.Security
, click on Role Collections
, create a new Role Collection named Myapp Administrator
by clicking on the New Role Collection
button:Myapp Administrator
Role Collection, add roles you defined by clicking on the Add Role
button:Trust Configuration
under Security
, then click on sap.default
:E-Mail Address
field, and click on the Show Assignment
button. Assign Role Collection
button:approuter
application and click into the myapp
link again. You will find you got the access permission to myapp
finally.myapp
application directly without approuter
, which doesn't make sense:server.py
to use the security library and make authorization check as below:import os
from flask import Flask
from flask import request
from flask import abort
from cfenv import AppEnv
from sap import xssec
app = Flask(__name__)
env = AppEnv()
port = int(os.environ.get('PORT', 3000))
uaa_service = env.get_service(name='myuaa').credentials
@app.route('/')
def hello():
if 'authorization' not in request.headers:
abort(403)
access_token = request.headers.get('authorization')[7:]
security_context = xssec.create_security_context(access_token, uaa_service)
isAuthorized = security_context.check_scope('openid')
if not isAuthorized:
abort(403)
return "Hello World"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=port)
cf push
myapp
application directly again, you should see 403 Forbidden error:Hello World
.You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
23 | |
22 | |
15 | |
13 | |
10 | |
9 | |
7 | |
7 | |
6 |