hassaan zia
hassaan zia

Reputation: 11

How to generate JWT token on JMETER using a RSA 256 private key Required library or jar file?

Currently, I am performing performance testing of API and it is required a dynamic JWT token of RSA 256 private key. I didn't find any solution. Can anyone provide me with any JWT jar file and groove code

Upvotes: 1

Views: 1508

Answers (1)

Dmitri T
Dmitri T

Reputation: 168002

I believe you will need to go for Groovy scripting for this

  1. Get a JWT client library, for instance this guy will be a good choice and drop it to JMeter Classpath (make sure to include all the dependencies)

  2. Restart JMeter to pick up the .jar

  3. Add JSR223 Sampler to your Test Plan

  4. The example code would be something like:

    def keyPayr = io.jsonwebtoken.security.Keys.keyPairFor(io.jsonwebtoken.SignatureAlgorithm.RS256)
    
    def now = java.time.Instant.now()
    
    def clientId = 'foo'
    
    def jwt = io.jsonwebtoken.Jwts.builder()
            .setAudience('https://example.com')
            .setIssuedAt(Date.from(now))
            .setExpiration(Date.from(now.plus(5L, java.time.temporal.ChronoUnit.MINUTES)))
            .setIssuer(clientId)
            .setSubject(clientId)
            .setId(UUID.randomUUID().toString())
            .signWith(keyPayr.private)
            .compact()
    
    log.info('Token: ' + jwt)
    

Demo:

enter image description here

Upvotes: 1

Related Questions