简体   繁体   English

在 express-gateway 中运行自定义函数

[英]Run custom functions in express-gateway

I have this configuration in the gateway.config.yml (Express-Gateway api):我在 gateway.config.yml (Express-Gateway api)中有这个配置:

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

Everything works fine, but I want the client to encode/encrypt a token that it is being sent to make sure even if I have the token storage on the localstorage no one can use it because it will need to be signed by the client.一切正常,但我希望客户端对正在发送的令牌进行编码/加密,以确保即使我在本地存储中有令牌存储,也没有人可以使用它,因为它需要由客户端签名。

The only problem with this is, how can I run a code to decode/decrypt the token before Express-Gateway jwt policy try to validate the token?唯一的问题是,如何在 Express-Gateway jwt 策略尝试验证令牌之前运行代码来解码/解密令牌?

Because express-gateway can use middlewares like any other express application I think this is possible, but not an idea on how to do it.因为 express-gateway 可以像任何其他 express 应用程序一样使用中间件,我认为这是可能的,但不知道如何去做。

I created this policy that will help me, but how can I integrate it with the express-gateway api:我创建了这个对我有帮助的策略,但是如何将它与 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
      };
    }
};

I think what you're interested is writing a plugin which is nothing more than a collection of additional middleware and condition you can stack in Express Gateway, where you can put your own logic.我认为您感兴趣的是编写一个插件,它只不过是您可以在 Express Gateway 中堆叠的附加中间件和条件的集合,您可以在其中放置自己的逻辑。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM