跳至内容

如何安全地将 Amazon Cognito 与外部 SAML 身份提供者集成和配置?

2 分钟阅读
0

我想将我的 Amazon Cognito 用户池配置为使用来自我的外部 SAML 身份提供者 (IdP) 的加密 SAML 断言。我希望我的应用程序的用户身份验证是安全的。

解决方法

将 SAML IdP 添加到您的用户池

如果您没有 Amazon Cognito 用户池,请参阅用户池入门。创建用户池后,在 IdP 端配置 SAML 应用程序。要将 SAML IdP 添加到您的 Amazon Cognito 用户池,请参阅在用户池中添加和管理 SAML 身份提供者

向您的 SAML IdP 提供加密证书,以便将加密的 SAML 断言发送到 Amazon Cognito

首先,从您的 Amazon Cognito 控制台下载加密证书。然后,在 SAML IdP 的配置界面中,导入加密证书。有关如何导入和激活加密证书的说明,请参阅 IdP 的文档。例如,请参阅 Microsoft Learn 网站上的在 Microsoft Entra 管理中心内配置令牌加密

验证加密的 SAML 断言流程

完成以下步骤:

  1. 打开浏览器的开发人员工具,然后创建 HTTP 存档 (HAR) 文件
  2. 导航到您的 Amazon Cognito 用户池的托管登录页面。
  3. 选择 SAML IdP。Amazon Cognito 将重定向到 IdP 的登录页面。
  4. 在 HAR 文件中,检索您的 IdP 发送到 saml2/idpresponse 端点的 SAML 断言请求。
  5. 在浏览器中,查看 HAR 文件中的 SAML 断言响应

带有加密 SAML 断言的解码 SAML 响应示例:

<saml:EncryptedAssertion>
    <xenc:EncryptedData
        xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
        Type="http://www.w3.org/2001/04/xmlenc#Element">
        <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-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>
                        <!-- Encrypted key data -->
                        hY7PK8L9eM+2Uw7....[abbreviated]....4nmB2gTfLwqX=
                    </xenc:CipherValue>
                </xenc:CipherData>
            </xenc:EncryptedKey>
        </ds:KeyInfo>
        <xenc:CipherData>
            <xenc:CipherValue>
                <!-- Encrypted assertion data -->
                kB4urcHh7K5HHJ....[abbreviated]....8JpWGpfTj=
            </xenc:CipherValue>
        </xenc:CipherData>
    </xenc:EncryptedData>
</saml:EncryptedAssertion>

未加密的解码 SAML 响应示例:

<saml:Assertion
    ID="ASRT#########"
    Version="2.0"
    IssueInstant="2024-07-10T10:00:00Z">

    <saml:Issuer>https://idp.example.com/saml</saml:Issuer>

    <saml:Subject>
        <saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">
            USER123456789
        </saml:NameID>
        <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
            <saml:SubjectConfirmationData
                NotOnOrAfter="2024-07-10T11:00:00Z"
                Recipient="https://app.example.com/saml/acs"/>
        </saml:SubjectConfirmation>
    </saml:Subject>

    <saml:Conditions
        NotBefore="2024-07-10T10:00:00Z"
        NotOnOrAfter="2024-07-10T11:00:00Z">
        <saml:AudienceRestriction>
            <saml:Audience>https://app.example.com/saml</saml:Audience>
        </saml:AudienceRestriction>
    </saml:Conditions>

    <saml:AuthnStatement
        AuthnInstant="2024-07-10T10:00:00Z"
        SessionNotOnOrAfter="2024-07-10T18:00:00Z"
        SessionIndex="SESSION123456789">
        <saml:AuthnContext>
            <saml:AuthnContextClassRef>
                urn:oasis:names:tc:SAML:2.0:ac:classes:Password
            </saml:AuthnContextClassRef>
        </saml:AuthnContext>
    </saml:AuthnStatement>

    <saml:AttributeStatement>
        <saml:Attribute Name="uid"
            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
            <saml:AttributeValue>user123</saml:AttributeValue>
        </saml:Attribute>
        <saml:Attribute Name="email"
            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
            <saml:AttributeValue>user@example.com</saml:AttributeValue>
        </saml:Attribute>
        <saml:Attribute Name="roles"
            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
            <saml:AttributeValue>user</saml:AttributeValue>
            <saml:AttributeValue>admin</saml:AttributeValue>
        </saml:Attribute>
    </saml:AttributeStatement>
</saml:Assertion>

**注意:**响应中的 saml:EncryptedAssertion 元素确认您拥有加密的 SAML 断言。只有您的 IdP 才能使用正确的私钥解密和读取断言内容。如果响应中没有 saml:EncryptedAssertion,则您有一个未加密的 SAML 断言,它以明文形式显示用户和身份验证的详细信息。

相关信息

将 SAML 身份提供者与用户池配合使用

How to set up Amazon Cognito for federated authentication using Azure AD(如何使用 Azure AD 设置 Amazon Cognito 以进行联合身份验证)

为身份验证响应配置 SAML 断言

AWS 官方已更新 2 个月前