SAML IDP 发起的 SSO 事务

SAML IDP initiated SSO transaction

Webapp #1 有一个指向 webapp #2 的按钮。当我们的用户单击该按钮时,他们应该被重定向到 webapp #2,并自动 logged-in.

集成是 SAML,因此 webapp #1(IDP)向 webapp #2(SP)发送 SAML“请求”,returns 重定向 URL,webapp # 1 重定向到它。

SP 给了我一个 URL 到 HTTP POST 的断言,通过“电子邮件”属性识别用户,所以我生成了这个:

<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" IssueInstant="2021-12-22T16:59:43.999Z" Version="2.0"><saml2:Issuer>http://www.whatever.com</saml2:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"></ds:CanonicalizationMethod><ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"></ds:SignatureMethod><ds:Reference URI=""><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"></ds:Transform><ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"></ds:Transform></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod><ds:DigestValue>...</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>...</ds:SignatureValue><ds:KeyInfo><ds:X509Data><ds:X509Certificate>...</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><saml2:Conditions><saml2:OneTimeUse></saml2:OneTimeUse></saml2:Conditions><saml2:AuthnStatement AuthnInstant="2021-12-22T16:59:44.053Z" SessionIndex="cfb8f9b5-9616-47db-bc92-7588ce18cf62" SessionNotOnOrAfter="2021-12-22T16:59:44.068Z"><saml2:AuthnContext></saml2:AuthnContext></saml2:AuthnStatement><saml2:AttributeStatement><saml2:Attribute Name="email"><saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">si@captisintel.com</saml2:AttributeValue></saml2:Attribute></saml2:AttributeStatement></saml2:Assertion>

SP 以 302 和如下所示的位置 header 进行响应:

Location: ?SAMLRequest=nVNNj9owEP0rke%2FkS4VtLc...

解码:

<?xml version="1.0"?><samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="_f848f04c71671a745722" Version="2.0" IssueInstant="2021-12-22T18:36:19.337Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="http://auth.whatever2.com/saml/callback" Destination=""><saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">whatever2</saml:Issuer><samlp:NameIDPolicy xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" AllowCreate="true"/><samlp:RequestedAuthnContext xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" Comparison="exact"><saml:AuthnContextClassRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef></samlp:RequestedAuthnContext></samlp:AuthnRequest>

当我访问那个 URL 时,我看到了登录页面。 所以这是行不通的。

我对 SP 日志的可见性为 0,不知道哪里出了问题。我只能猜测我没有正确 post 这样做。那么这看起来正确吗,还是我应该 post 换个方式?我尝试 posting body as-is 中的断言,将其包装在“

另一个线索是 SP 总是 returns 一个 302,不管我发给他们什么。即使我发送“BLAHBLAH”而不是实际的 SAML 负载,他们仍然会以 302 响应。

有什么建议吗?

您应该向 SP 发送包含 SAML 断言的 SAML 响应,而不仅仅是 SAML 断言。您还需要确保根据 SAML v2.0 绑定规范对其进行正确编码。

确实需要SP帮忙调试。希望他们的日志能够详细说明他们为什么拒绝您的 HTTP Post.

在 IdP 发起的流程中,IdP 发送包含断言的 SAML 响应 POST 就像您一样。如果一切顺利,SP 现在应该认为用户已通过身份验证。

SAML 响应应在名为 SAMLResponse 的输入控件中发送,并且应采用 Base64 编码。

<input type="hidden" name="SAMLResponse" value="<Base64 encoded SAML Response>"/>

我猜想发生的事情是由于某种原因,SP 不接受您的 SAMLResponse,而是通过向 IdP 发送 SAML 请求以验证用户身份来启动正常的 SP 启动的 SAML 流。

您的回复未被接受的原因可能有很多。如果没有 SP 日志,我们基本上只能猜测。

马上我看到你的断言缺少一个强制性的 Bearer 主题确认。

来自SAML Profiles spec 4.1.4.2

Any assertion issued for consumption using this profile MUST contain a element with at least one element containing a Method of urn:oasis:names:tc:SAML:2.0:cm:bearer. Such an assertion is termed a bearer assertion. Bearer assertions MAY contain additional elements. There is also more requirements on the assertion in the same chapter

还有一些其他想法:

  • 您应该在 SAML 响应中包含断言,但有几个样板元素需要设置。所以确保它是有效的
  • 响应消息本身必须签名而不仅仅是断言,这并不罕见。

我建议查看 SAML 配置文件规范 4.1.4.2 以了解断言和响应必须包含的内容。

另外,再次尝试看看您是否可以从 SP 那里得到任何关于错误的反馈,以避免盲目尝试和失败让您的脑袋血流成河。

SAML 并不是目前最直接的协议,要使其正常工作并确保其安全,还有很多工作要做。正如我总是试图对刚开始使用它的人说的那样:不要自己构建它,有很多好的开源替代品可以用于执行 SAML,这将使您的系统更加安全和可维护。

如果您想更好地了解 SAML 以及如何实现它,我可以推荐我的书,SAML 2.0: Designing secure identity federation

祝你好运!