【问题标题】:How to Establish Python Connection with HANA using OAUTH/JWT如何使用 OAUTH/JWT 与 HANA 建立 Python 连接
【发布时间】:2021-08-02 07:59:37
【问题描述】:

我们目前在与 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 的连接器的资源。在这里的任何帮助将不胜感激。

【问题讨论】:

    标签: python oauth-2.0 sqlalchemy sap hana


    【解决方案1】:

    我是这样设置的..

    使用库。 下面 -- 您需要将这些属性 Identity Provider (IdP) 传递到 数据库。您的json config 通过xs-security 将允许权限范围。

    1. 首先下载 Python:sap_xsseclib. 它应该允许您获取 JWT 令牌的属性。

    2. 其次,设置你的服务和安全

    //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$env.user.value

    1. 设置你的@sap/hana-client 调用 用connection.session.XS_APPLICATIONUSER = &lt;JWT TOKEN&gt;;

    2. 不要忘记设置 sap-jwt/py-jwt 库以验证 jwt 令牌

    刚刚设置

    USE_SAP_PY_JWT = true
    

    你已经完成了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      相关资源
      最近更新 更多