python - 使用带有 flask 的python-saml获取 "Signature validation failed. SAML Response rejected"

标签 python flask saml-2.0

尝试使用 python-saml 和 flask 处理 IdP 发起的 SAML2 响应时出现以下错误:

Signature validation failed. SAML Response rejected

我正在按照示例 here .我的代码是:

url_data = urlparse(request.url)
req = {
    "https": "on",
    "http_host": request.host,
    "server_port": url_data.port,
    "script_name": request.path,
    "get_data": request.args.copy(),
    "post_data": request.form.copy()
}
auth = OneLogin_Saml2_Auth(req, custom_base_path=app.config['SAML_PATH'])
auth.process_response()

在 SAML_PATH 中,我的 settings.json 文件中有以下内容:

{
    "strict": false,
    "debug": true,
    "sp": {
        "entityId": "[spEntityId]",
        "assertionConsumerService": {
            "url": "[acsUrl]",
            "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
        },
        "NameIDFormat": "urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified",
        "x509cert": "[x509cert]",
        "privateKey": "[privateKey]"
    },
    "idp": {
        "entityId": "[idpEntityId]",
        "singleSignOnService": {
            "url": "http://dummy.com/saml2",
            "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
        },
        "singleLogoutService": {
            "url": "http://dummy.com/saml2",
            "binding": "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
        },
        "x509cert": "[x509cert]"
    },
    "security": {
        "nameIdEncrypted": false,
        "authnRequestsSigned": false,
        "logoutRequestSigned": false,
        "logoutResponseSigned": false,
        "signMetadata": false,
        "wantMessagesSigned": true,
        "wantAssertionsSigned": true,
        "wantNameIdEncrypted": false,
        "requestedAuthnContext": false
    }
}

如您所见,我为 IdP singleSignOnService 和 singleLogoutService URL 使用了虚拟值。我认为我不需要它们,因为我只需要处理 SAML 响应。我还为 SP 和 IdP 使用相同的 x509cert。响应包含签名消息和加密断言:

    <?xml version="1.0" encoding="UTF-8"?>
<saml2p:Response xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" Destination="[Destination]" ID="[ID]" IssueInstant="2015-11-30T15:35:02.702Z" Version="2.0">
    <saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
        [Issuer]
    </saml2:Issuer>
    <saml2p:Status>
        <saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
    </saml2p:Status>
    <saml2:EncryptedAssertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
        <xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="[ID]" Type="http://www.w3.org/2001/04/xmlenc#Element">
            <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
            <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <xenc:EncryptedKey Id="[ID]" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                    <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
                    <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                        <xenc:CipherValue>
                            [CipherValue]
                        </xenc:CipherValue>
                    </xenc:CipherData>
                </xenc:EncryptedKey>
            </ds:KeyInfo>
            <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                <xenc:CipherValue>
                    [CipherValue]
                </xenc:CipherValue>
            </xenc:CipherData>
        </xenc:EncryptedData>
    </saml2:EncryptedAssertion>
</saml2p:Response>

我已验证 x509cert 和 privateKey 是正确的。我是 SAML2 的新手,所以我希望它很简单 :) 提前谢谢你!!

最佳答案

您在上面提供的响应未签名,但您已请求对该响应进行签名,因此您的软件拒绝该响应。

您的配置显示 wantMessagesSignedwantAssertionsSigned。断言已签名(也许——我们实际上无法从您的示例中看出),但消息(即整个响应)未签名。

你怎么知道?如果您的回复已签名,它看起来更像以下内容——请注意 ds:Signature block (我在其周围包含了一些空行以使其更易于查看)。该 block 包括消息的 DigestValue,然后是该摘要的签名。它包括证书,您可以使用它来解码签名并验证它是否与摘要匹配。

IdP 使用 私钥签署响应并向您发送证书。 IdP 使用您的 证书对断言进行加密,允许您(并且只有您)解密它们(加密的断言是否已签名?我不知道——需要先解密它们。)

<?xml version="1.0" encoding="UTF-8"?>
  <saml2p:Response xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" Destination="[Destination]" ID="[ID]" IssueInstant="2015-11-30T15:35:02.702Z" Version="2.0">
<saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
    [Issuer]
</saml2:Issuer>

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
  <ds:SignedInfo>
    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-cl14n#"/>
    <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    <ds:Reference URI="#[ID]">
      <ds:Transforms>
         ...
      </ds:Transforms>
      <ds:DigestMethod Algorithm="http://www/s3.org/2001/04/xmlenc#sha256"/>
      <ds:DigestValue>[DigestValue]</ds:DigestValue>
    </ds:Reference>
  </ds:SignedInfo>
  <ds:SignatureValue>
     [Signature]
  </ds:SignatureValue>
  <ds:KeyInfo>
    <ds:X509Data>
      <ds:X509Certificate>
         [Certificate]
      </ds:X509Certificate>
    </ds:X509Data>
  </ds:KeyInfo>
</ds:Signature>


<saml2p:Status>
    <saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
</saml2p:Status>
<saml2:EncryptedAssertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
    <xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="[ID]" Type="http://www.w3.org/2001/04/xmlenc#Element">
        <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
        <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
            <xenc:EncryptedKey Id="[ID]" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" />
                <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
                    <xenc:CipherValue>
                        [CipherValue]
                    </xenc:CipherValue>
                </xenc:CipherData>
            </xenc:EncryptedKey>
        </ds:KeyInfo>
        <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
            <xenc:CipherValue>
                [CipherValue]
            </xenc:CipherValue>
        </xenc:CipherData>
    </xenc:EncryptedData>
</saml2:EncryptedAssertion>

关于python - 使用带有 flask 的python-saml获取 "Signature validation failed. SAML Response rejected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34068208/

相关文章:

python - Jinja2 - 以 x 为一组呈现列表项

python - flask-oauth 永远不会在 oauth 身份验证后重定向

java - 用于签名证书的 SAML 样本身份提供者证书和 Java 应用程序

ruby-on-rails - 设计 + OmniAuth Saml:ActionController::InvalidAuthenticityToken

python - 修改 Python 模块的常见做法

Python 优化 : Choosing between dictionary or list of list

Python Regex 删除所有 HTML 数据

flask - 有什么方法可以为flask-admin生成的类别链接分配菜单图标?

python - 任何 __future__ 导入 range-xrange 不兼容?

public-key-encryption - 在签署 SAML 请求时使用哪个公钥(SP 或远程 IDP)