问题描述
我们目前在连接到 SAP HANA 的 Python 连接器中使用基本身份验证。在我们当前的连接字符串中,我们使用 SQLAlchemy,它看起来像这样:
def get_engine(host_name):
return create_engine('hana://{user}:{password}@{host_name}:{port}/HUP'.format(
user=request.json['username'],password=base64.b64decode(bytes(request.json['password'],encoding='utf-8')).decode('utf-8'),host_name=host_name,port=current_app.config['HANA_PORT']
)
)
我们现在需要过渡到使用 HANA Oauth,因此不再需要在连接字符串中输入用户名和密码。理想情况下,应该有一种方法可以将 JWT 输入到连接详细信息中。我在网上找不到太多资源来真正说明如何使用 Oauth 的 HANA 创建基于 Python 的连接器。非常感谢这里的任何帮助。
解决方法
我是这样设置的..
使用库。 下面——您需要将这些属性从 number
(IdP) 传递到数据库。您通过 Identity Provider
的 json config
将允许权限范围。
-
首先下载 Python:
xs-security
lib. 它应该允许您获取 JWT 令牌的属性。 -
其次,设置您的服务和安全
sap_xssec
接下来配置您的 //import these lib. after downloading
from sap import xssec
from cfenv import AppEnv
// get your env.
myEnv = AppEnv()
// get your UAA service
myService = myEnv.get_service(name='<uaa_service_name>').credentials
// now your JWT access token for
contextWithAccessToken = xssec.create_security_context(access_token,myService)
文件
xs-security
// 为您的环境准备好用户值。 Example xs-security.json File
{
"xsappname" : "node-hello-world","scopes" : [ {
"name" : "$XSAPPNAME.Display","description" : "display" },{
"name" : "$XSAPPNAME.Edit","description" : "edit" },{
"name" : "$XSAPPNAME.Delete","description" : "delete" }
],"attributes" : [ {
"name" : "Country","description" : "Country","valueType" : "string" },{
"name" : "CostCenter","description" : "CostCenter","valueType" : "int" }
],"role-templates": [ {
"name" : "Viewer","description" : "View all books","scope-references" : [
"$XSAPPNAME.Display" ],"attribute-references": [ "Country" ]
},{
"name" : "Editor","description" : "Edit,delete books","scope-references" : [
"$XSAPPNAME.Edit","$XSAPPNAME.Delete" ],"attribute-references" : [
"Country","CostCenter"]
}
]
}
或 XS_APPLICATIONUSER
-
设置你@sap/hana-client 调用 使用
$env.user.value
-
不要忘记设置
connection.session.XS_APPLICATIONUSER = <JWT TOKEN>;
库来验证 jwt 令牌
只需设置
sap-jwt/py-jwt
大功告成!