简体   繁体   English

Nodejs:按顺序运行承诺

[英]Nodejs : run promises sequentially

For a job where I need to put in place an oauth client authentication with private_key_jwt on an F5 big-ip.对于我需要在 F5 大 IP 上使用private_key_jwt进行oauth客户端身份验证的工作。 Since the built-in module for oauth doesn't take in charge this kind of authentication, this have to be achieved via their iRuleLX module which is nodejs based.由于 oauth 的内置模块不负责这种身份验证,因此必须通过基于iRuleLX模块来实现。

I have the following code to encrypt the JWT , but on some system, the result of the first promise is not available before the second promise is executed, which leads to an error ofc.我有以下代码来加密JWT ,但是在某些系统上,在执行第二个 Promise 之前,第一个 Promise 的结果不可用,从而导致错误 ofc。

I made some google effort to find a way to process the two promises sequentially, but I was not able to find the correct way to achieve it (process asKey before executing the createEncrypt promise).我做了一些谷歌努力来找到一种方法来顺序处理这两个承诺,但我无法找到实现它的正确方法(在执行 createEncrypt 承诺之前处理 asKey)。

To be honest I'm not familiar with Node.js .老实说,我对Node.js并不熟悉。

var f5 = require("f5-nodejs");
const { JWE, JWK } = require("node-jose");
var ilx = new f5.ILXServer();
var contentAlg = "A128CBC-HS256";
var key = "nok";
var token = "nok";
const skey = {
  kty: "RSA",
  e: "AQAB",
  use: "enc",
  kid: "e1",
  n:
    "vVm75k4dzUw_iuG8NvIvGS8o3dMvlpXwBX44ZcGgBzCnzHKjY37T8newmRcfmFkpvTR0qgYqtPeev5RwOZXXDO9Seg6Zkc_6sZjfSpeiOBebwW1DeZlEiYCTWSg6Ri5H26S3j6R8H_b3BCrtcd3gcmD7OwY280QvJ8eDmbJaj4aAaXf_Ef9RTYz1qJHnehbNRlmRr-OJuuYpsH497Is-c7OvUSLfMkItj9mtRKuk4DQ0LY5c5MYiyx1NidCuQTSK4VZSA3l6zMq-WN1pRb61hjfI74OO7gT256vQZZSq0DrzMPxA0mGeNDBlj6J5cBcdwnTAhF9mojs-ZwcAAvbgQ",
  alg: "RSA-OAEP",
  key_ops: ["encrypt", "wrap", ""]
};

var options = {
  compact: true,
  contentAlg: contentAlg,
  fields: {
    alg: "RSA-OAEP",
    kid: "e1",
    cty: "JWT",
    enc: contentAlg
  }
};

ilx.addMethod("test_jwk", function(req, res) {
  var payload = req.params()[0].toString();

  JWK.asKey(skey)
    .then(function(result) {
      key = result;
    })
    .catch(function(error) {
      key = "nok";
    });

  if (key != "nok") {
    jose.JWE.createEncrypt(options, key)
      .update(payload, "utf8")
      .final()
      .then(function(result) {
        token = result;
      })
      .catch(function(error) {
        token = "nok";
      });
  }

  res.reply(token);
});
ilx.listen();

You could use async/Await.您可以使用异步/等待。

Try this,尝试这个,

var f5 = require('f5-nodejs');
const { JWE, JWK } = require('node-jose')
var ilx = new f5.ILXServer();
var contentAlg = "A128CBC-HS256";
var key = "nok";
var token = "nok";
const skey =
{
    "kty": "RSA",
    "e": "AQAB",
    "use": "enc",
    "kid": "e1",
    "n": "vVm75k4dzUw_iuG8NvIvGS8o3dMvlpXwBX44ZcGgBzCnzHKjY37T8newmRcfmFkpvTR0qgYqtPeev5RwOZXXDO9Seg6Zkc_6sZjfSpeiOBebwW1DeZlEiYCTWSg6Ri5H26S3j6R8H_b3BCrtcd3gcmD7OwY280QvJ8eDmbJaj4aAaXf_Ef9RTYz1qJHnehbNRlmRr-OJuuYpsH497Is-c7OvUSLfMkItj9mtRKuk4DQ0LY5c5MYiyx1NidCuQTSK4VZSA3l6zMq-WN1pRb61hjfI74OO7gT256vQZZSq0DrzMPxA0mGeNDBlj6J5cBcdwnTAhF9mojs-ZwcAAvbgQ",
    "alg": "RSA-OAEP",
    "key_ops": ["encrypt", "wrap", ""]
};
var options =
{
    compact: true,
    contentAlg: contentAlg,
    fields:
    {
        "alg": "RSA-OAEP",
        "kid": "e1",
        "cty": "JWT",
        "enc": contentAlg
    }
};
ilx.addMethod('test_jwk', async function (req, res) {
    var payload = req.params()[0].toString();
    try {
        key = await JWK.asKey(skey);
    } catch (error) {
        key = "nok";
    }

    if (key != "nok") {
        try {
            token = await jose.JWE.createEncrypt(options, key).update(payload, "utf8").final();
        } catch (error) {
            token = "nok";
        }
    }

    res.reply(token);
});
ilx.listen();

With then chaining .then chaining

ilx.addMethod('test_jwk',  function (req, res) {
    var payload = req.params()[0].toString();
        JWK.asKey(skey)
        .then( (result) => {
           return  result;
        })
        .then( key => {
            if(key !== "nok"){
                return jose.JWE.createEncrypt(options, key).update(payload, "utf8").final();
            } else {
                throw "Invalid key";
            }
        })
        .then( resToken => {
            token = resToken;
            res.reply(token);
        })
        .catch( error => {
            res.reply("nok");
        });
});

You can use async await:您可以使用异步等待:

ilx.addMethod('test_jwk', function async (req, res) {

var payload = req.params()[0].toString();


try {
      const key = await JWK.asKey(skey);

      const token = await jose.JWE.createEncrypt(options, key).update(payload, "utf8").final();

      res.reply(token); 
   } catch {
      res.reply('nok');
   }

});

You can surround the function body with try catch if you want.如果需要,您可以使用 try catch 包围函数体。

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

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