How to create jwt and pass in JSON api tool

Confluence User - 11 Apr, 2023

I am integrating an api which needs a jwt bearer token to be passed in authorisation header
the jwt is generated with key signed with sceret.
So i tried to import io.jsonwebtoken and write the code in bean shell but no luck

i tried creating a hash variable plugin but the json api tool throws error

So how to write custom code and use it in various places in joget or

how to add external jars and use them and

finally how to dynamically set value in json api tool headers

plugins;beanshell;api

3


12 Apr, 2023
confluenceUser
confluenceUser

what is the error from your hash variable plugin? sometime it is just some class loading issue causing your plugin not working.

if you want to use external jar and import the class in beanshell. you can put the jar file in tomcat/lib folder then restart your tomcat.

12 Apr, 2023
confluenceUser
confluenceUser

for the hash variable plugin it is a jsp error seems to be json api tool is not able to recognise #beanshell.env_var#

i tried the puting the jar in lib folder io.jsonwebtoken, it needs fasterxml dependency i added that also but still in server logs it was failing to import the jar, the error in server logs was cannot load io.jsonwebtoken faster xml version should be >2.9.0 and 3.0.0 , i checked the version was 2.13.4.

12 Apr, 2023
confluenceUser
confluenceUser

I had no option but to create the JWT manually for now without using io.jsonwebtoken and drop using JSON API tool and call the external API from bean shell.

import java.nio.charset.StandardCharsets;

import java.time.LocalDateTime;

import java.time.ZoneOffset;

import java.util.UUID;

import java.util.Base64;

import javax.crypto.Mac;

import org.json.JSONObject;

import javax.crypto.spec.SecretKeySpec;

import org.joget.commons.util.LogUtil;

        try {            

int EXPIRY_DAYS = 1;  

JSONObject payload = new JSONObject();

String JWT_HEADER = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}";

String encodedHeader = Base64.getUrlEncoder().withoutPadding().encodeToString(JWT_HEADER.getBytes());   

String SECRET_KEY =  "Sceret Key";

String apiKey = "Api Key";

JSONObject jwtPayload = new JSONObject();

jwtPayload.put("status", 0);jwtPayload.put("sub", "admin");

LocalDateTime ldt = LocalDateTime.now().plusDays(EXPIRY_DAYS);

jwtPayload.put("exp", ldt.toEpochSecond(ZoneOffset.UTC));

payload.put("sub", jwtPayload.getString("sub")); payload.put("exp", jwtPayload.getLong("exp"));             payload.put("iss", apiKey);payload.put("iat", LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));             payload.put("jti", UUID.randomUUID().toString());

String secret = SECRET_KEY;

String data = encodedHeader + "." +Base64.getUrlEncoder().withoutPadding().encodeToString(payload.toString().getBytes());

byte[] hash = secret.getBytes(StandardCharsets.UTF_8);

Mac sha256Hmac = Mac.getInstance("HmacSHA256");

SecretKeySpec secretKey = new SecretKeySpec(hash, "HmacSHA256");

sha256Hmac.init(secretKey);

byte[] signedBytes = sha256Hmac.doFinal(data.getBytes(StandardCharsets.UTF_8));

String signature = Base64.getUrlEncoder().withoutPadding().encodeToString(signedBytes);            

String token = encodedHeader + "." +                     Base64.getUrlEncoder().withoutPadding().encodeToString(payload.toString().getBytes())+ "." + signature;             
LogUtil.info("token", token);        
//Code to call the external API
//=============================
}catch(Exception ex) {LogUtil.info("token catch", ex.toString());}

I am still looking forward to upload jar in lib folder and then able to create JWT and also some way to run custom code inside JSON API tool header value so to dynamically pass value to Authorization header

RELATED QUESTIONS

Your answer


To answer a question you'll need an account.

Print