Giter Club home page Giter Club logo

Comments (7)

thim81 avatar thim81 commented on May 20, 2024

Hi @pfunk1978

Normally the postman-to-k6 does some smart detection and insert the necessary libs, including the crypto-js.js.

Could you share your Postman collection (or simplified version of it), with the crypto-js usage included? That way we can review the output of the conversion and improve where necessary.

from postman-to-k6.

pfunk1978 avatar pfunk1978 commented on May 20, 2024

Hi @thim81
as requested

-$ cat outs.js
// Auto-generated by the postman-to-k6 converter

import "./libs/shim/core.js";
import "./libs/shim/expect.js";
import { group } from "k6";
import * as CryptoJS from "./libs/crypto-js.js"  // imported by me

export let options = { maxRedirects: 4 };


const Pre = Symbol.for("pre");
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
  options,
  collection: {
    JWT_TOKEN: "",
    APPROVER_TOKEN: "",
    UPLOADER_TOKEN: ""
  },
  environment: {
    host: "10.0.5.49:8081",
    v7: "kmes/v7",
    v6: "kmes/v6",
    jwtIssuer: redacted,
    hmacKey: redacted,
    testUser: "testUser"
  }
});

export default function() {
  postman[Pre].push(() => {

    function make_payload(name) {
      let payload = {
        iss: pm.environment.get("jwtIssuer"),
        iat: Math.floor(Date.now() / 1000),
        exp: Math.floor(Date.now() / 1000 + 100000),
        sub: name
      };
      return payload;
    }

    function base64url(source) {
      let encodedSource = CryptoJS.enc.Base64.stringify(source)
        .replace(/=+$/, "")
        .replace(/\+/g, "-")
        .replace(/\//g, "_");
      return encodedSource;
    }

    function build_token(payload) {
      let hmac_key = pm.environment.get("hmacKey");
      let header = {
        alg: "HS256",
        typ: "JWT"
      };
      let b64_header = base64url(
        CryptoJS.enc.Utf8.parse(JSON.stringify(header))
      );
      let b64_data = base64url(
        CryptoJS.enc.Utf8.parse(JSON.stringify(payload))
      );
      let message = b64_header + "." + b64_data;
      let b64_signature = base64url(CryptoJS.HmacSHA256(message, hmac_key));
      let jwt_token = [b64_header, b64_data, b64_signature].join(".");
      return jwt_token;
    }

    function build_token_from_name(name) {
      let payload = make_payload(name);
      let token = build_token(payload);
      return token;
    }

    let user = pm.environment.get("testUser");
    pm.collectionVariables.set("JWT_TOKEN", build_token_from_name(user));
    pm.collectionVariables.set(
      "APPROVER_TOKEN",
      build_token_from_name("approver")
    );
    pm.collectionVariables.set(
      "UPLOADER_TOKEN",
      build_token_from_name("uploader")
    );
  });

  group("SymmetricKeyGeneration", function() {
    postman[Request]({


      ...

from postman-to-k6.

thim81 avatar thim81 commented on May 20, 2024

Hi @pfunk1978, I was refering to the Postman collection. But I think I can build a Postman collection based on your output.

from postman-to-k6.

thim81 avatar thim81 commented on May 20, 2024

hi @pfunk1978

In the past you had to use "require" statements to use libraries like "crypto-js".
https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries
The postman-to-k6 convertor, scanned for these "require" statements to trigger the automatic loading of the libraries used.

It seems that these Postman supports these libraries without the need of using "require", which means that the postman-to-k6 converter is not aware of these libraries being used.

An example:
https://www.postman.com/postman/workspace/postman-answers/request/18070393-d1fd1172-daef-44d2-a993-5591d1f5d9e7?ctx=code

I'll do some exploration to see if the convertor can detect the usage of libraries in another way.

from postman-to-k6.

thim81 avatar thim81 commented on May 20, 2024

hi @pfunk1978

We have enhanced support for the commonly supported libs: crypto, cheerio, lodash, xml2json.

The PR will be merged in and this enhancement will be part of the next release.

from postman-to-k6.

thim81 avatar thim81 commented on May 20, 2024

Closing the issue, since the latest postman-to-k6 version (1.8.5) contains this enhancement.

from postman-to-k6.

pfunk1978 avatar pfunk1978 commented on May 20, 2024

ty

from postman-to-k6.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.