【问题标题】:Run custom functions in express-gateway在 express-gateway 中运行自定义函数
【发布时间】:2019-09-26 13:00:23
【问题描述】:

我在 gateway.config.yml (Express-Gateway api) 中有这个配置:

 - bo
    policies:
      - jwt:
        - action:
            secretOrPublicKeyFile: './key.pem'
            checkCredentialExistence: false

一切正常,但我希望客户端对正在发送的令牌进行编码/加密,以确保即使我在本地存储中有令牌存储,也没有人可以使用它,因为它需要由客户。

唯一的问题是,如何在 Express-Gateway jwt 策略尝试验证令牌之前运行代码来解码/解密令牌?

因为 express-gateway 可以像任何其他 express 应用程序一样使用中间件,我认为这是可能的,但不知道如何做。

我创建了这个对我有帮助的策略,但是我如何将它与 express-gateway api 集成:

const cryptojs = require("crypto-js");
module.exports = {
    name: 'decode',
    policy: (actionParams) => {
      return (req, res, next) => {
        const tokenHeader = req.header('Authorization');
        const tokenArray = tokenHeader.split(' ');
        const tokenCifer = tokenArray[1];
        const bytes  = cryptojs.AES.decrypt(tokenCifer, 'superkeyperm'); //CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123');
        var token = bytes.toString(cryptojs.enc.Utf8);
        req.headers.authorization = `Bearer ${token}`;
        next() // calling next policy
      };
    }
};

【问题讨论】:

    标签: express express-gateway


    【解决方案1】:

    我认为您感兴趣的是编写一个插件,它只不过是您可以在 Express Gateway 中堆叠的附加中间件和条件的集合,您可以在其中放置自己的逻辑。

    https://www.express-gateway.io/docs/plugins/查看文档

    【讨论】:

    • @Vicenzo 感谢您的建议,我正在努力解决这个问题,我创建了一个在这种情况下对我有帮助的政策,检查我的代码是否已更新,但我没有知道如何将它与 Gateway-API 集成
    猜你喜欢
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 2022-06-11
    • 1970-01-01
    • 2020-04-09
    • 2017-02-16
    • 2020-11-23
    相关资源
    最近更新 更多