Xk6-jose extension : Context error when trying to parse private key

Hello ,

I start using K6 3 days ago, I’m trying to make a signature to a JW token through the xk6-jose extension.

Json web key :

{
    "kty": "EC",
    "d": "mG7Sp0QJFcSPpvC0Jg-BPmOO4ALA4_cqlGaOvbg2ed0",
    "crv": "P-256",
    "kid": "kid",
    "x": "kAlnOBtvNp-42ZJAdYUwQZZfYXt88ARVvjnhH_DWcLk",
    "y": "2cDwDG2FXXGf5EA0F27WIdO6xA6WltYlPlpGGuxcOEs",
    "alg": "ES256",
    "use": "sign"
}

code

import {parse} from "k6/x/jose/jwk";
import {sign} from "k6/x/jose/jwt";

const file = open("private.json");
const pkey = JSON.stringify(JSON.parse(file));

export default function () {

    // Define the header and payload for the JWT token
    const header = {
        "alg": "ES256",
        "typ": "JWT",
        "kid": "Kid",
    };
    const payload = {
        "sub": "1234567890",
        "name": "John Doe",
        "iat": "1675951923",
        "nbf": "1675951923",
        "exp": "1896876621",
        "client_id": "clientID",
        "nonce": "nonce"
    };
    const privatekey= parse(pkey)
    const token = sign(privatekey, payload, header);

    console.log(token)

};

When running this code, it fails with `ERRO[0000] GoError: could not convert [PrivatekeyValue] to context.Context

Any solution as to how I should properly parse my Json private Key ?

Hi @Med

Welcome to the community forum :wave:

I’ve been testing your script and it would work with the k6 version that GitHub - szkiba/xk6-jose: A k6 extension for Javascript Object Signing and Encryption (JOSE) standards. was built with.

$ ./k6 version    
k6 v0.32.0 ((devel), go1.19.3, darwin/amd64)
./k6 run test.js

If we run with the more recent k6 versions, like the latest 0.43.1:

$ ./k6 version
k6 v0.43.1 ((devel), go1.19.3, darwin/amd64)
Extensions:
  github.com/szkiba/xk6-jose v0.1.2, k6/x/jose/jwk [js]
  github.com/szkiba/xk6-jose v0.1.2, k6/x/jose/jwt [js]

Is when we see errors:

This extension has not been updated in a while. From version 0.32.0 there have been k6 breaking changes that most probably had an impact. This is a community maintained extension, so I would open an issue in their repo to ask for an update.

I hope this helps.

Cheers!