首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SAML2模块解密pysaml2响应

使用SAML2模块解密pysaml2响应
EN

Stack Overflow用户
提问于 2018-05-01 19:09:14
回答 1查看 2.6K关注 0票数 1

我正在集成我的应用程序和okta有一个单一的标志。Okta将在SAML响应中传递一些用户信息,我需要在我的应用程序中使用这些信息。因此,我们决定使用我的服务器(Apache)公钥在IDP上加密saml响应(Xml)。

现在,我正在尝试解密saml2响应,以便获得属性。

我的应用程序使用

  1. Python 3.5
  2. Django 1.11
  3. pysaml2 python模块

我正在使用下面的方法来验证/解析来自okta https://github.com/fangli/django-saml2-authhttps://github.com/fangli/django-saml2-auth响应

如果saml响应没有加密,我就能够处理响应,并从中获取用户标识和用户属性。但是,一旦在okta用我的服务器公钥加密了它,我就无法用我的私钥解密。

我在应用程序中拥有的saml设置如下:

代码语言:javascript
复制
saml_settings = { 'metadata': {
                 "local": [ metadat_xml
              ],           
},
'service': {
    'sp': {
        'endpoints': {
            'assertion_consumer_service': [
                (acs_url, BINDING_HTTP_REDIRECT),
                (acs_url, BINDING_HTTP_POST),
                (https_acs_url, BINDING_HTTP_REDIRECT),
                (https_acs_url, BINDING_HTTP_POST)
            ],
        },
        'allow_unsolicited': True,
        'authn_requests_signed': False,
        'logout_requests_signed': True,
        'want_assertions_signed': True,
        'want_response_signed': False,
    },
},

'key_file': "mykey.key",  # private part
'cert_file': "mykey.crt",  # public part
'xmlsec_binary': '/usr/bin/xmlsec1',
'encryption_keypairs': [{
    'key_file': 'mykey.key',
    'cert_file': 'mykey.crt',
       }]
      }
     if 'ENTITY_ID' in settings.SAML2_AUTH:
    saml_settings['entityid'] = settings.SAML2_AUTH['ENTITY_ID']

#print('entity id ' , settings.SAML2_AUTH['ENTITY_ID'])

if 'NAME_ID_FORMAT' in settings.SAML2_AUTH:
    saml_settings['service']['sp']['name_id_format'] = settings.SAML2_AUTH['NAME_ID_FORMAT']

# NOTE-'NAME_ID_FORMAT is set to None above

spConfig = Saml2Config()

spConfig.load(saml_settings)
spConfig.allow_unknown_attributes = True
saml_client = Saml2Client(config=spConfig)
return saml_client

那我就有

代码语言:javascript
复制
saml_client = _get_saml_client(get_current_domain(r))
resp = r.POST.get('SAMLResponse', None)
authn_response = saml_client.parse_authn_request_response(resp, entity.BINDING_HTTP_POST )

这个auth_response对象在消息加密时没有返回任何内容。

在日志中,我看到下面的错误

代码语言:javascript
复制
GbHvkJJM0WIsPYFGtiQ/0n+ux0tV/z/OKpT1AqEE74iRVHEHD7omP41iY/c4= 
</ns3:CipherValue></ns3:CipherData><ns3:ReferenceList><ns3:DataReference 
URI="#_648cdbd139564492f0bdfe4fbbda92f6" /></ns3:ReferenceList> 
</ns3:EncryptedKey></ns1:EncryptedAssertion></ns0:Response>
2018-04-30 18:21:09,232 [DEBUG] sigver saml2.sigver decrypt(): Decrypt input 
len: 15187
2018-04-30 18:21:09,233 [DEBUG] sigver saml2.sigver _run_xmlsec(): xmlsec 
command: /usr/bin/xmlsec1 --decrypt --privkey-pem 
/private.pem --id-attr:ID EncryptedKey --output /tmp/tmp7rt7g95u.xml 
/tmp/tmpkhxwo8s4
2018-04-30 18:21:09,247 [DEBUG] sigver saml2.sigver _run_xmlsec(): xmlsec 
p_out:
2018-04-30 18:21:09,247 [DEBUG] sigver saml2.sigver _run_xmlsec(): xmlsec 
p_erryy: 
func=xmlSecXPathDataExecute:file=xpath.c:line=273:obj=unknown:
subj=xmlXPtrEval:error=5:libxml2 library function 
failed:expr=xpointer(id('_841612fffac65343e73f8913eeecfb30'))
func=xmlSecXPathDataListExecute:file=xpath.c:line=373:obj=unknown:
subj=xmlSecXPathDataExecute:error=1:xmlsec library function failed:
func=xmlSecTransformXPathExecute:file=xpath.c:line=483:
obj=xpointer:subj=xmlSecXPathDataExecute:error=1:xmlsec library function 
failed:
func=xmlSecTransformDefaultPushXml:file=transforms.c:
line=2411:obj=xpointer:subj=xmlSecTransformExecute:error=1:xmlsec library 
function failed:
func=xmlSecTransformCtxExecute:file=transforms.c:line=1302:
obj=unknown:subj=xmlSecTransformCtxXmlExecute:error=1:xmlsec library 
function failed:
func=xmlSecKeyDataRetrievalMethodXmlRead:file=keyinfo.c:line=1178:
obj=retrieval-method:subj=xmlSecTransformCtxExecute:error=1:xmlsec library 
function failed:
func=xmlSecKeyInfoNodeRead:file=keyinfo.c:line=114:obj=retrieval-method:
subj=xmlSecKeyDataXmlRead:error=1:xmlsec library function 
failed:node=RetrievalMethod
func=xmlSecKeysMngrGetKey:file=keys.c:line=1349:obj=unknown:
subj=xmlSecKeyInfoNodeRead:error=1:xmlsec library function 
failed:node=KeyInfo
func=xmlSecEncCtxEncDataNodeRead:file=xmlenc.c:line=957:
obj=unknown:subj=unknown:error=45:key is not found:
func=xmlSecEncCtxDecryptToBuffer:file=xmlenc.c:line=715:
obj=unknown:subj=xmlSecEncCtxEncDataNodeRead:error=1:xmlsec library function 
failed:
func=xmlSecEncCtxDecrypt:file=xmlenc.c:line=623:
obj=unknown:subj=xmlSecEncCtxDecryptToBuffer:error=1:xmlsec library function 
failed:

Error: failed to decrypt file
Error: failed to decrypt file "/tmp/tmpkhxwo8s4"

我不知道为什么xmlsec1命令失败了,我在这里缺少了什么。我在这里尝试过用我的私钥(自签名私钥)解密https://www.samltool.com/decrypt.php,它可以工作。

,请你在这里帮助我,让我知道我做的不对吗?

EN

回答 1

Stack Overflow用户

发布于 2019-06-10 19:54:16

你需要添加

代码语言:javascript
复制
saml_settings['id_attr_name'] = 'Id'

默认的id attr是ID,但Okta使用Id。有关更多详细信息,请参阅xmlsec常见问题

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50122384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档