首页 > 解决方案 > 使用 pysaml2 Python 模块解密 SAML2 响应

问题描述

我正在将我的应用程序与 okta 集成以进行单点登录。Okta 将在我需要在我的应用程序中使用的 SAML 响应中传递一些用户信息。因此,我们决定使用我的服务器(apache)公钥加密 IDP 的 saml 响应(xml)。

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

我的应用程序使用

  1. 蟒蛇 3.5

  2. 姜戈 1.11

  3. pysaml2 python 模块

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

如果 saml 响应未加密,我可以处理响应并从中获取用户身份和用户属性。但是,一旦使用我的服务器公钥在 okta 结束时对其进行加密,我就无法使用我的私钥解密。

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

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

然后我有

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 对象没有返回任何东西。

在日志中我看到以下错误

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使用我的私钥(自签名私钥)解密 ,它可以工作

你能在这里帮助我,让我知道我做错了什么吗?

标签: djangopython-3.xsingle-sign-onokta

解决方案


您需要添加

saml_settings['id_attr_name'] = 'Id'

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


推荐阅读