无法为 SAML 响应创建正确的签名

Unable to create the correct signature for SAML response

我在我们的项目中使用 go-saml 库来启用 SSO,其中服务提供商将是 Salesforce,身份提供商将是 Golang 代码。 Golang 代码将首先验证用户,然后创建 SAML 响应以允许用户登录 Salesforce。 我是 Golang 的新手,正在关注此库的 Creating a SAML Response (if acting as an IdP)。所以,到目前为止,我能够使用它创建 SAML 响应,但在根据要求自定义它时面临一些挑战。

  1. 我面临的第一个挑战是在条件[=]中添加AudienceRestriction 47=]块如下:-

<saml:Conditions NotBefore="2020-03-15T16:33:16.23103491Z" NotOnOrAfter="2020-03-15T16:43:16.23104017Z"> <saml:AudienceRestriction> <saml:Audience>https://saml.salesforce.com</saml:Audience> </saml:AudienceRestriction> </saml:Conditions>

我试过在代码中像下面那样添加它,但似乎 AudienceRestrictions 没有在 Conditions 对象中定义。

authnResponse := saml.NewSignedResponse() authnResponse.Conditions.AudienceRestrictions = "https://saml.salesforce.com"

我找不到任何方法将上述块添加到 Salesforce 强制要求的条件块中。请给我一些建议。

  1. 我还必须在条件块下方添加 AuthnStatement,如下所示:-

<saml:AuthnStatement AuthnInstant="2020-03-01T11:28:31.396Z"> <saml:AuthnContext> <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified</saml:AuthnContextClassRef> </saml:AuthnContext> </saml:AuthnStatement>

  1. 即使在 SAML 响应中手动添加上述块后,我在使用 Salesforce SAML 验证器验证 SAML 响应时仍收到以下错误

Validating the Signature... Is the response signed? true
Is the assertion signed? false
The reference in the response signature is valid
Is the correct certificate supplied in the keyinfo? true

Signature or certificate problems
The signature in the response is not valid

我已经生成了一个 Public 密钥(自签名 .pem 证书)和一个私钥。之后,我将 public 密钥上传到 Salesforce,并在代码中使用私有密钥和 public 密钥来生成 SAML 响应。我不知道为什么会收到 Signature Invalid 错误。如果您对我有什么建议,请告诉我。

如果你想查看我的 Golang 代码 - https://play.golang.org/p/U9dXZblTHG1

  1. 可以通过提供的 function:
  2. 添加观众
authnResponse := saml.NewSignedResponse()
authnResponse.AddAudienceRestriction("https://saml.salesforce.com")
  1. 可以通过另一个添加身份验证上下文 function:
authnContext := "urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified"
sessionIndex := "session_1"
authnResponse.AddAuthnStatement(authnContext,sessionIndex)

作为建议,尝试使用更成熟、成熟的库(例如 OpenSAML 或 simpleSAMLphp)来生成您的 SAML 响应。如果这行得通但 go-saml 不行,那么您的后续步骤还有待继续。另一方面,如果您通过另一种方法生成的断言也失败了,那么无论您使用密钥或断言内容而不是库,都可能存在问题。