Java: JWT token creation and verification

Once you have installed the Setu Serializer SDK in your Maven/Gradle project, you can use it to generate or verify JWT tokens when working with Setu.

Token Generation

Use the following snippet to generate the bearer token when making calls to Setu when you have the secret key and the scheme ID.

String secret = "043bc09c-d3b8-11e9-b29c-acde48001122";  // Your secret Key
String schemeID = "09191f4c-d3b8-11e9-812d-acde48001122"; // Your Scheme ID
SetuJwtHelper jwtHelper = new SetuJwtHelper(secret, schemeID);
String bearerToken = jwtHelper.yieldBearerToken();

bearerToken will look like this:

Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIwOTE5MWY0Yy1kM2I4LTExZTktODEyZC1hY2RlNDgwMDExMjIiLCJpYXQiOjE1NjgxMTI0MTksImp0aSI6IjBhNTkzY2QzLWEwMDItNDEzZC05NWI4LWIzOTAwOThlNDRjZSJ9.0dF7_zBGxBioYMpr7foyMI5Kt08Iymaq60eznIsWCBw

You can send use this token as the value for the Authorization http header when making calls to Setu.

Token Verification

When Setu is making calls to your APIs and you want to verify the token sent over to you by Setu, you can do something like this.

String secret = "043bc09c-d3b8-11e9-b29c-acde48001122";  // Your secret Key
String schemeID = "09191f4c-d3b8-11e9-812d-acde48001122"; // Your Scheme ID
SetuJwtHelper jwtHelper = new SetuJwtHelper(secret, schemeID);
String bearerToken = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIwOTE5MWY0Yy1kM2I4LTExZTktODEyZC1hY2RlNDgwMDExMjIiLCJpYXQiOjE1NjgxMTI0MTksImp0aSI6IjBhNTkzY2QzLWEwMDItNDEzZC05NWI4LWIzOTAwOThlNDRjZSJ9.0dF7_zBGxBioYMpr7foyMI5Kt08Iymaq60eznIsWCBw";
boolean isValidToken = false;
try {
    jwtHelper.verifyBearerToken(bearerToken);
    isValidToken = true;
} catch (JWTVerificationException e) {
    //error situation
}

Based on the value of isValidToken you can take further action.