diff --git a/apitest-commons/src/main/java/io/mosip/testrig/apirig/testrunner/BaseTestCase.java b/apitest-commons/src/main/java/io/mosip/testrig/apirig/testrunner/BaseTestCase.java index d811ce2d43..8f4ce44656 100644 --- a/apitest-commons/src/main/java/io/mosip/testrig/apirig/testrunner/BaseTestCase.java +++ b/apitest-commons/src/main/java/io/mosip/testrig/apirig/testrunner/BaseTestCase.java @@ -65,6 +65,7 @@ public class BaseTestCase { public String regProcCookie = null; public String regProCookie = null; public String regAdminCookie = null; + public String invalidcookie = "anyRandomString"; public String registrationOfficerCookie = null; public String regSupervisorCookie = null; public String zonalAdminCookie = null; diff --git a/apitest-commons/src/main/java/io/mosip/testrig/apirig/utils/KernelAuthentication.java b/apitest-commons/src/main/java/io/mosip/testrig/apirig/utils/KernelAuthentication.java index a344b15d74..7afbc6e8c2 100644 --- a/apitest-commons/src/main/java/io/mosip/testrig/apirig/utils/KernelAuthentication.java +++ b/apitest-commons/src/main/java/io/mosip/testrig/apirig/utils/KernelAuthentication.java @@ -1,8 +1,15 @@ package io.mosip.testrig.apirig.utils; +import java.util.Base64; import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.Base64.Encoder; +import java.util.Date; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.interfaces.DecodedJWT; import org.json.simple.JSONObject; @@ -16,6 +23,7 @@ public class KernelAuthentication extends BaseTestCase { String folder = "kernel"; String cookie; static String dataKey = "response"; + static String encodeBase64 =""; static String errorKey = "errors"; static Map tokens = new HashMap<>(); CommonLibrary clib = new CommonLibrary(); @@ -171,9 +179,12 @@ public String getTokenByRole(String role, String tokenType) { if (!kernelCmnLib.isValidToken(invalidBatchJobToken)) invalidBatchJobToken = kernelAuthLib.getPreRegInvalidToken(); return invalidBatchJobToken; - case "invalid": return "anyRandomString"; + case "invalidtoken": + String encodingToken = "AnyRandomString-ToCreate-Jwt"; + invalidcookie = kernelAuthLib.encodeBase64(encodingToken); + return invalidcookie; case "regAdmin": if (!kernelCmnLib.isValidToken(regAdminCookie)) regAdminCookie = kernelAuthLib.getAuthForRegistrationAdmin(); @@ -781,5 +792,20 @@ public String getAuthForAutoUser() { cookie = reponse.getCookie(GlobalConstants.AUTHORIZATION); return cookie; } + + @SuppressWarnings("unchecked") + public String encodeBase64(String value) { + String secret = value; + // Create the token + String token = JWT.create() + .withSubject("user123") + .withIssuer("example.com") + .withClaim("role", "admin") + .withIssuedAt(new Date()) + .withExpiresAt(new Date(System.currentTimeMillis() + 3600000)) + .sign(Algorithm.HMAC256(secret)); + System.out.println("Generated Token: " + token); + return token; + } }