我必须为Salesforce创建单一登录(SSO)。为了获得授权,我将在OTP上使用用户的手机号码。验证OTP后,我只需要使用Go创建SAML响应。
我可以看到Go编程语言的go-saml,gosaml,go-oauth和goauth有两个库,但是即使经过几个小时的搜索,我仍然无法确定哪一个适合我。我不必实现完整的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>[email protected]</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 date after / after date,Attribute Statement等。我不确定是否需要任何库来获取或仅获取一个库脚本。
[This library似乎是Go的简单SAML
库,它负责SAML
Response
的生成以及对Response
的签名。
[我也已经看到使用“模板”完成了此操作,实际上没有完成SAML
处理。已打开带有占位符的准备好的文本块,将占位符替换为所需的文本并发送了响应。
saml1p:Response
可以是在运行时创建IssueInstant
和ResponseID
的模板。例如,伪代码为:
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签名进行数字签名来做到这一点。您可以使用this library执行此操作。它仍在对Response
进行编码,但是您只需要使用签名即可,而不是整个SAML
流程。
所以工作流程将是:
SAML
Response
作为文本模板。SAML
Response
。SAML
Response
进行签名。这就是您发送给SP的内容。