Ad

Nodejs : Run Promises Sequentially

- 1 answer

For a job where I need to put in place an oauth client authentication with private_key_jwt on an F5 big-ip. 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.

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.

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).

To be honest I'm not familiar with 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();

Ad

Answer

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.

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");
        });
});
Ad
source: stackoverflow.com
Ad