Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 在NodeJS中对SAML2请求进行数字签名大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下要进行数字签名的SAML请求:

<samlp:Authnrequest Version="2.0" ID="_9FE393FB-1C9C-4EDD-86A5-1AE9F2192A60" IssueInstant="2014-10-22T11:22:56.676Z" DesTination="https://idp.ssocircle.com:443/sso/SSOPOST/MetaAlias/ssocircle" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
    <saml:Issuer>http://app.localhost</saml:Issuer>
    <samlp:NamEIDPolicy AllowCreate="true" />
</samlp:Authnrequest>

我使用以下依赖于nodejs xmlbuilderxmlcrypto模块的coffeescript代码

request = @xmlbuilder.create
    'samlp:Authnrequest':
        '@xmlns:samlp':'urn:oasis:names:tc:SAML:2.0:protocol'
        '@xmlns:saml': 'urn:oasis:names:tc:SAML:2.0:assertion'
        '@Version': '2.0'
        '@ID': requestId
        '@IssueInstant': (new Date()).toISOString()
        '@DesTination': idpUrl
        'saml:Issuer': '@@spEntityId',null,headless: true

request.comment 'insert-signature-here'
request.element 'samlp:NamEIDPolicy':
                    '@AllowCreate': 'true'
saml = request.end()

@fs.readFile "certs/my-cert.pem",(err,certificatE)=>
    return next @errorservice.readFileError certFilePath,err if err?
    signer = new @xmlcrypto.SignedXml()
    signer.signingKey = certificate
    signer.addReference "//*[local-name(.)='Authnrequest']",['http://www.w3.org/2000/09/xmldsig#enveloped-signature']
    signer.keyInfoProvider = new =>
        getKeyInfo: (key)=>
            public_key = /-----BEGIN CERTIFICATE-----([^-]*)-----END CERTIFICATE-----/g.exec(key)[1].replace /[\r\n|\n]/g,''
            "<X509Data><X509Certificate>#{public_key}</X509Certificate></X509Data>"
    signer.computeSignature saml
    signature = signer.getSignatureXml()
    signed = saml.replace '<!-- insert-signature-here -->',signature
    console.log signed

这会生成以下经过数字签名的SAML请求:

<samlp:Authnrequest Version="2.0" ID="_5FEB2162-F4D0-4900-BC28-F2940188E45B" IssueInstant="2014-10-28T13:07:14.007Z" DesTination="https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
    <saml:Issuer>http://app.localhost9de83841</saml:Issuer>
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
            <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
            <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
            <Reference URI="#_5FEB2162-F4D0-4900-BC28-F2940188E45B">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue>47MSlH9ipJf8vs37T3DnhZMZ7mo=</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>T0Uw...KZkm00A==</SignatureValue>
        <KeyInfo>
            <X509Data>
                <X509Certificate>MIIDg...ogMMxZ</X509Certificate>
            </X509Data>
        </KeyInfo>
    </Signature>
    <samlp:NamEIDPolicy AllowCreate="true" />
</samlp:Authnrequest>

这似乎是有效的.

但是,当我使用SSOCircleTestShib测试时,它们都报告摘要值不匹配.

我使用的证书是带有未加密私钥的自签名证书(pem).

我已经仔细检查以确定sp-Metadata中提供的公钥是从用于对SAML进行数字签名的相同pem文件获取的.

私钥是否需要加密?

如果没有,那么你可以建议为什么签名检查会失败吗?

谢谢.

解决方法

私钥不应加密.您可以使用此在线工具 https://www.samltool.com/validate_logout_req.php验证您的签名.如果它没有通过意味着您的签名以错误的方式创建

大佬总结

以上是大佬教程为你收集整理的node.js – 在NodeJS中对SAML2请求进行数字签名全部内容,希望文章能够帮你解决node.js – 在NodeJS中对SAML2请求进行数字签名所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。