节点肥皂上的身份验证问题

时间:2018-10-07 01:03:50

标签: javascript node.js web-services soap ws-security

感谢您抽出宝贵的时间阅读本文章,

我在使用node-soap时遇到了一些问题,因此基本上,在按照文档找到了server.authenticate函数之后,我试图在发送响应之前验证客户端的身份。

server.authenticate = async function (security: any) {
                const binarySecurityTokenAsBase64String = security.BinarySecurityToken.$value;
                const pemKeyFromRequestAsString = "-----BEGIN CERTIFICATE-----" + "\n" + binarySecurityTokenAsBase64String.replace(/(.{64})/g, "$1\n") + "\n" + "-----END CERTIFICATE-----";
                const success =  await validateCertificate(pemKeyFromRequestAsString);
                if (success) {
                    return true;
                } else {
                    winston.warn("Failed to validate Certificate - Either Certificate Verification with CA Chain Failed or the system encountered an error");
                    return false;
                }
            };

那是我进行验证业务并根据结果返回true或false的地方:

const success =  await validateCertificate(pemKeyFromRequestAsString);

我的问题是,无论结果如何,我仍然会在日志中返回响应,一切都很好,并确认验证失败,也许是由于异步/同步问题造成的。到Javascript / Typescript World,我们将不胜感激。

这是我的代码预览:

try {
        const myService = {
            Calculate_Service: {
                Calculate_Port: {
                    multiply: function(args, callback) {
                        const a = 1;
                        try {
                            winston.debug("Reached the multiply Function");
                            const n = args.a * args.b;
                            callback({
                                multiplicationResult : n
                            });
                        } catch (e) {
                            winston.error(e);
                            throw {
                                Fault: {
                                    Code: {
                                        Value: "soap:Sender",
                                        Subcode: { value: "rpc:BadArguments" }
                                    },
                                    Reason: { Text: JSON.stringify(e) },
                                    statusCode: 500
                                }
                            };
                        }

                    },
                }
            }
        }

        const xml = fs.readFileSync(AppConfiguration.responseServerWsdlPath, "utf8");

        app.use(bodyParser.raw({
            type: function () {
                return true;
            }, limit: "5mb"
        }));

        app.listen(port, async function () {

            winston.info("Express server listening on port " + port);
            const server = ArcNodeSoap.listen(app, "/calculatorService", myService, xml);

            server.authenticate = async function (security: any) {
                const binarySecurityTokenAsBase64String = security.BinarySecurityToken.$value;
                const pemKeyFromRequestAsString = "-----BEGIN CERTIFICATE-----" + "\n" + binarySecurityTokenAsBase64String.replace(/(.{64})/g, "$1\n") + "\n" + "-----END CERTIFICATE-----";
                const success =  await validateCertificate(pemKeyFromRequestAsString);
                if (success) {
                    return true;
                } else {
                    winston.warn("Failed to validate Certificate - Either Certificate Verification with CA Chain Failed or the system encountered an error");
                    return false;
                }
            };

            server.log = function (type, data) {
                winston.debug("type: " + type);
                winston.debug(JSON.stringify(data));
            };

                server.on("headers", function (headers, methodName) {
                    //More debug stuff;
                    winston.debug("****** HEADERS **********");
                    winston.debug(JSON.stringify(headers));
                    winston.debug("methodName: " + methodName);
                    winston.debug("*************************")

                });
        });
    } catch (err) {
        winston.error(err);
    }

我很感谢你们的时间,谢谢!

1 个答案:

答案 0 :(得分:0)

我终于解决了我的问题,如果有人对异步/同步代码有同样的问题: 我通过使用文档

中的Async方法修复了该问题
server.authenticate =  function (security: any, callback): any {
                //Get the Binary Security Token coming from the request as a Base 64 String
            const binarySecurityTokenAsBase64String = security.BinarySecurityToken.$value;
            //Creating a new certificate with header, footer and line breaks from the binarySecurityTokenAsBase64String
            const pemKeyFromRequestAsString = "-----BEGIN CERTIFICATE-----" + "\n" + binarySecurityTokenAsBase64String.replace(/(.{64})/g, "$1\n") + "\n" + "-----END CERTIFICATE-----";
            //Validate the certificate

                //This is an async wrapper where I added all my verification steps;
                validateCertificateWrapper(pemKeyFromRequestAsString).then((authorized: boolean) => {
                    //If the certificate is valid
                    if (authorized) {
                        winston.info("Verification successfully Passed");
                        return callback(true);

                    } else {                     //If the certificate is invalid
                        winston.error("Failed to validate Certificate");
                        return callback(false);
                    }
                }, () => {
                    winston.error("Failed to validate Certificate");
                    return callback(false);
                } );
            };