如何解码 SAML 响应并获取 PHP 中 Idp 发送的属性值

How to decode SAML Response and get the attribute values sent by Idp in PHP

我正在实施 SAML 单点登录并使用 idp 发起的登录请求方法。登录后,它将用户重定向到使用 base64 编码的 SAML 响应在请求中定义的登录 url。

现在,我已提取此响应,验证它并获取 Idp 发送的属性值,如电子邮件地址、姓名等。

我已经从 base64 解码并得到以下 XML。此 XML 已加密签名、密码数据、证书等。但不知道如何验证和 decode/extract 属性值,以便我们可以进一步使用它。

我通过提供私钥搜索它并喜欢正在解码它的 online tool。我试了一下,得到了属性值。

我必须在我们的应用程序中使用 PHP 做同样的事情,并且必须使用这个属性值。如果有人做过同样的事情并且可以对此有所了解,那么将不胜感激。

请检查下面的 SAML 响应。

<samlp:Response Version="2.0" ID="vafmraxdfkermx" IssueInstant="2015-07-31T07:26:00.180Z" Destination="http://domain.com/saml/SSO" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">MI-TEST-SAML2-EntityID</saml: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-c14n#"/>
        <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
        <ds:Reference URI="#DLSr8z03t7WZ-F7ZFwbxUw91vQF">
            <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
            <ds:DigestValue>gnvnDwu/eDBpLnPtiaHeOI7UCD4=</ds:DigestValue>
        </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue>
        Signature Value
    </ds:SignatureValue>
    <ds:KeyInfo>
        <ds:X509Data>
            <ds:X509Certificate>
                Certifivate Contents
            </ds:X509Certificate>
        </ds:X509Data>
    </ds:KeyInfo>
</ds:Signature>
<samlp:Status>
    <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:EncryptedAssertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
    <xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
        <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
        <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
            <xenc:EncryptedKey>
                <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
                <xenc:CipherData>
                    <xenc:CipherValue>CIPHER VALUE</xenc:CipherValue>
                </xenc:CipherData>
            </xenc:EncryptedKey>
        </ds:KeyInfo>
        <xenc:CipherData>
            <xenc:CipherValue>CIPHER CONTETNS</xenc:CipherValue>
        </xenc:CipherData>
    </xenc:EncryptedData>
</saml:EncryptedAssertion>

您使用的在线工具基于 OneLogin 的 PHP SAML 工具包 [1]。我都实现了。

阅读文档。 processResponse 和 getAttributes 做你想做的事。

[1] https://github.com/onelogin/php-saml

根据您想执行的操作,让现有代码为您处理所有 SAML 可能更容易。对于 PHP,您有 SimpleSAMLphp,它可以配置为服务提供商。

SimpleSamlPHP 将为您生成元数据并使用 SAML 断言并验证它们。可以使用函数检索属性。有关详细信息,请参阅 SimpleSAMLphp Service Provider QuickStart

您可以使用LightSAML to receive the encoded SAML Response directly from http request, de-serialize it into a data model, and decrypt it. Here's an example on how to receive a SAML message from HTTP request http://www.lightsaml.com/LightSAML-Core/Cookbook/How-to-receive-SAML-message/ and here's an example on how to decrypt an Assertion http://www.lightsaml.com/LightSAML-Core/Cookbook/How-to-decrypt-Assertion/