3

我必须为 Salesforce 创建单点登录 (SSO)。对于授权,我将使用用户的手机号码进行 OTP。验证 OTP 后,我只需使用 Go 创建 SAML 响应。

我可以看到Go 编程语言的go-samlgosamlgo-oauthgoauth有几个库,但是即使经过几个小时的搜索,我也无法确定哪个适合我. 我不必实现完整的 IDP,我只需动态创建 SAML 响应。

我找到了需要从https://www.samltool.com/generic_sso_res.php创建的 XML 响应模板。所以我必须创建如下 SAML 响应:-

<?xml version="1.0" encoding="UTF-8"?>
<saml1p:Response xmlns:saml1p="urn:oasis:names:tc:SAML:1.0:protocol" IssueInstant="2020-02-26T17:32:05.200Z" MajorVersion="1" MinorVersion="1" Recipient="https://im--partial.my.salesforce.com" ResponseID="_34ae7ce8-5f7fbd4d">
    <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#dsa-sha1"/>
            <ds:Reference URI="#_34ae7ce8-5f7fbd4d">
                <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>fxk4drd6yhVQrJCtdfvYOyYYGAM=</ds:DigestValue>
            </ds:Reference>
        </ds:SignedInfo>
        <ds:SignatureValue>EsjQYhFsL+Xvcgg59AkZja....8INZrTwyfLmo4+NMyYViDX6Q==</ds:SignatureValue>
        <ds:KeyInfo>
            <ds:X509Data>
                <ds:X509Certificate>MIID0zCCA5GgAwIBAgIEF...XWlGzJ3SdBlgRsdFgKyFtcxE=</ds:X509Certificate>
            </ds:X509Data>
        </ds:KeyInfo>
    </ds:Signature>
    <saml1p:Status>
        <saml1p:StatusCode Value="saml1p:Success"/>
    </saml1p:Status>
    <saml1:Assertion xmlns:saml1="urn:oasis:names:tc:SAML:1.0:assertion" AssertionID="_7f959448-5dbf311f" IssueInstant="2020-02-26T17:32:05.200Z" Issuer="AXIOM" MajorVersion="1" MinorVersion="1">
        <saml1:Conditions NotBefore="2020-02-26T17:32:05.199Z" NotOnOrAfter="2020-02-26T17:33:05.199Z">
            <saml1:AudienceRestrictionCondition>
                <saml1:Audience>https://saml.salesforce.com</saml1:Audience>
            </saml1:AudienceRestrictionCondition>
        </saml1:Conditions>
        <saml1:AuthenticationStatement AuthenticationInstant="2020-02-26T17:32:05.199Z" AuthenticationMethod="urn:oasis:names:tc:SAML:1.0:am:unspecified">
            <saml1:Subject>
                <saml1:NameIdentifier>rahul.satal@xyz.com</saml1:NameIdentifier>
                <saml1:SubjectConfirmation>
                    <saml1:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</saml1:ConfirmationMethod>
                </saml1:SubjectConfirmation>
            </saml1:Subject>
        </saml1:AuthenticationStatement>
    </saml1:Assertion>
</saml1p:Response>

其中我只需要更新几个参数,如 Assertion_Id、Issuer、Audience、Recipient、Subject、Not before/After date、Attribute Statement 等。我不确定我是否需要任何库或可以获得单个脚本。

4

1 回答 1

1

这个库似乎是一个简单SAML的 Go 库,它负责SAML Response生成和签名Response

我还看到使用“模板”完成此操作,SAML实际上没有进行任何处理。打开带有占位符的准备好的文本块,将占位符替换为所需的文本并发送响应。

saml1p:Response

可以是一个模板,IssueInstantResponseID在运行时创建。例如伪代码将是:

String samlTemplate = loadSAMLTemplate()
  .gsub("__IssueInstant__", "2020-02-26T17:32:05.200Z")
  .gsub("__ResponseID__", "9421878f98")
  .gsub( ... and so on ...)

这样做是可能的,如果不理想的话,它只适用于你只有一个Assertion.

您将使用上述模板并将可替换部分替换为占位符,例如

<saml1p:Response
  xmlns:saml1p="urn:oasis:names:tc:SAML:1.0:protocol"
  IssueInstant="__IssueInstant__"
  MajorVersion="1"
  MinorVersion="1"
  Recipient="https://im--partial.my.salesforce.com"
  ResponseID="__ResponseID__">

建立一个XML SAML Response. 您将有等的占位符AssertionID。任何需要唯一的东西SAML Response都需要一个占位符,其内容在运行时被替换。

然后你需要签署那个Response。你不能通过更换东西来做到这一点。您只能通过使用 XML 签名进行数字签名来做到这一点。你可以使用这个库来做到这一点。它仍在编码,Response但您只需要使用签名而不是整个SAML过程。

所以工作流程是:

  • 使用PlaceHolders为每个可替换文本创建可重复使用SAML Response的文本模板。
  • 加载模板,替换所有需要替换的位。这是你的SAML Response
  • 使用 XML 签名库对SAML Response. 这是您发送给 SP 的内容。
于 2020-03-02T13:05:15.183 回答