首页 > 解决方案 > 在 express-gateway 中运行自定义函数

问题描述

我在 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
      };
    }
};

标签: expressexpress-gateway

解决方案


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

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


推荐阅读