0

我正在为 SFCC Commerce Cloud (Demandware) 商店创建自定义控制器。

因为我需要与第三方系统进行通信,所以我创建了一个自定义 REST API 控制器,以便能够在 SFCC 中接收一些数据。

我创建了一个休息控制器,以便通过 POST 接收信息。如何为我的控制器提供身份验证机制?

OCAPI 提供了默认受保护的资源,您可以使用 OAuth 进行身份验证,但自定义控制器不受保护,我想知道如何添加 OAuth 或其他身份验证机制。

我的控制器:

server.post('Test', server.middleware.https, function (req, res, next) {
    
    //Some logic that should be protected...
}
4

2 回答 2

0

您可以使用私钥和证书来验证请求。如果请求始终来自特定域,您可以添加证书。或者添加一个公钥和私钥对。

 server.post('InboundHookRequest', server.middleware.https, function (req, res, next) {
    var payload = null,
        requestStored = false;
    if (verifySignature(req) === true) {
        try {
            payload = JSON.parse(req.body);
            // Do the logic here 
        } catch (e) {
            Logger.error(e);
        }
        if (requestStored === true) {
            okResponse(res);
            return next();
        }
    }
    notOkResponse(res);
    return next();
});

然后验证相同

function verifySignature(req) {
    var signature,
        algoSupported,
        result;
        signature = new Signature();
        algoSupported = signature.isDigestAlgorithmSupported("SHA256withRSA"); // or other algo
        if (algoSupported === true) {
            try {
                var certRef = new CertificateRef(WEBHOOK_CONFIG.CERT_NAME);
                result = signature.verifySignature("YOURINCOMINGREQHEADER", content, certRef, "SHA256withRSA");;
                if (result === true) {
                    return true;
                }
            } catch (e) {
                Logger.error(e); // Certificate doesn't exist or verification issue
            }
        }
    }
    return false;
}

签名:https ://documentation.b2c.commercecloud.salesforce.com/DOC2/topic/com.demandware.dochelp/DWAPI/scriptapi/html/api/class_dw_crypto_Signature.html?resultof=%22%53%69%67%6e %61%74%75%72%65%22%20%22%73%69%67%6e%61%74%75%72%22%20 证书和私钥:https://documentation.b2c.commercecloud .salesforce.com/DOC2/topic/com.demandware.dochelp/content/b2c_commerce/topics/b2c_security_best_practices/b2c_certificates_and_private_keys.html?resultof=%22%70%72%69%76%61%74%65%22%20% 22%70%72%69%76%61%74%22%20%22%6b%65%79%73%22%20%22%6b%65%69%22%20

有关 Web 服务安全的更多信息:https ://documentation.b2c.commercecloud.salesforce.com/DOC2/topic/com.demandware.dochelp/content/b2c_commerce/topics/web_services/b2c_webservice_security.html?resultof=%22%70%72 %69%76%61%74%65%22%20%22%70%72%69%76%61%74%22%20%22%6b%65%79%73%22%20%22%6b %65%69%22%20

于 2021-07-19T05:55:09.767 回答
0

您可以根据请求使用加密参数并添加逻辑以在控制器上解密。

于 2021-07-13T12:32:08.273 回答