OTP code generation in k6 UI Performance testing

Hi all, I’m just starting with UI Performance testing with k6 and I want to integrate MFA/OTP in one of my tests
I have a secret key, which I can use to generate the OTP code and use it in my tests
I can use the following function:
export function generateTOTP(secretKey) {
// Create TOTP instance
let totp = new OTPAuth.TOTP({
algorithm: ‘SHA1’,
digits: 6,
period: 30, //If this is changed, the OTP is not working anymore
secret: OTPAuth.Secret.fromBase32(secretKey)
});
// Generate and return OTP
return totp.generate();
}
But k6 does not support it:
ERRO[0000] GoError: The moduleSpecifier “otplib” couldn’t be recognised as something k6 supports. hint=“script exception”
ERRO[0000] GoError: The moduleSpecifier “otpauth” couldn’t be recognised as something k6 supports. hint=“script exception”
What can I use (or how)?
Also, if this is not a good channel to ask this question, please let me know in which channel
Thanks!

Hi @alexandrusimion, welcome to the community forum!

You are in the correct place to ask this questions :+1:

The error you are getting is because k6 doesn’t underestand what otplib and co are supposed to be. k6 isn’t based on node, deno or bun and consequently doesn’t support thigns such as npm. You can think of k6 as more of a browser in that.

On the other hand k6 does support importing by https urls, so the following seems to work.

import * as OTPAuth from "https://esm.sh/otpauth@9.4.0";

export function generateTOTP(secretKey) {
  // Create TOTP instance
  let totp = new OTPAuth.TOTP({
    algorithm: "SHA1",
    digits: 6,
    period: 30, //If this is changed, the OTP is not working anymore
    secret: OTPAuth.Secret.fromBase32(secretKey)
  });
  // Generate and return OTP
  return totp.generate();
}

export default function() {
  console.log(generateTOTP("wow"))
}

Hope this helps you!

Hi, thanks for the response!
Trying the code above, I get the following error message:
ERRO[0002] ReferenceError: BigInt is not defined
at https://esm.sh/otpauth@9.4.0/esm/npm/otpauth@9.4.0/node_modules/otpauth/dist/otpauth.esm.js:574:46(348) hint=“script exception”
And tried several items to solve but still stuck
What would be the next steps, from your side?