@@ -564,77 +564,20 @@ private static String calculateHash(JsonObject payload) {
564564 */
565565 private static String generateOrGetCachedJwtToken () throws Exception {
566566 long currentTime = System .currentTimeMillis ();
567-
568567 // Return cached token if it's still valid (with 5 minute buffer)
569568 if (cachedJwtToken != null && currentTime < (jwtTokenExpiry - 300000 )) {
570569 return cachedJwtToken ;
571570 }
572-
573- // Generate new token
574- cachedJwtToken = generateJwtToken ();
575- jwtTokenExpiry = currentTime + (getJwtExpirySeconds () * 1000 );
576-
577- return cachedJwtToken ;
578- }
579-
580- /**
581- * Generates a JWT token signed with RS256 algorithm using built-in Java libraries.
582- */
583- private static String generateJwtToken () throws Exception {
584- String keystorePath = getConfigValue (ICP_JWT_KEYSTORE_PATH ,
585- "repository/resources/security/wso2carbon.jks" );
586- String keystorePassword = getConfigValue (ICP_JWT_KEYSTORE_PASSWORD , "ballerina" );
587- String keyAlias = getConfigValue (ICP_JWT_KEY_ALIAS , "ballerina" );
588- String keyPassword = getConfigValue (ICP_JWT_KEY_PASSWORD , keystorePassword );
571+ String jwtHmacSecret = getConfigValue (ICP_JWT_HMAC_SECRET , DEFAULT_JWT_HMAC_SECRET );
572+ HMACJWTTokenGenerator hmacJWTTokenGenerator = new HMACJWTTokenGenerator (jwtHmacSecret );
589573 String issuer = getConfigValue (ICP_JWT_ISSUER , DEFAULT_JWT_ISSUER );
590574 String audience = getConfigValue (ICP_JWT_AUDIENCE , DEFAULT_JWT_AUDIENCE );
575+ String scope = getConfigValue (ICP_JWT_SCOPE , DEFAULT_JWT_SCOPE );
591576 long expirySeconds = getJwtExpirySeconds ();
592-
593- // Resolve keystore path relative to carbon.home if not absolute
594- if (!keystorePath .startsWith ("/" )) {
595- String carbonHome = System .getProperty ("carbon.home" );
596- keystorePath = carbonHome + "/" + keystorePath ;
597- }
598-
599- // Load keystore
600- KeyStore keyStore = KeyStore .getInstance ("JKS" );
601- try (FileInputStream fis = new FileInputStream (keystorePath )) {
602- keyStore .load (fis , keystorePassword .toCharArray ());
603- }
604-
605- // Get private key
606- PrivateKey privateKey = (PrivateKey ) keyStore .getKey (keyAlias , keyPassword .toCharArray ());
607-
608- // Create JWT manually using built-in Java (matching Ballerina jwt:issue() output)
609- long currentTimeSeconds = System .currentTimeMillis () / 1000 ;
610- long expirationTime = currentTimeSeconds + expirySeconds ;
611-
612- // JWT Header
613- JsonObject header = new JsonObject ();
614- header .addProperty ("alg" , "RS256" );
615- header .addProperty ("typ" , "JWT" );
616- String encodedHeader = Base64 .getUrlEncoder ().withoutPadding ()
617- .encodeToString (header .toString ().getBytes ("UTF-8" ));
618-
619- // JWT Payload (matching Ballerina structure)
620- JsonObject payload = new JsonObject ();
621- payload .addProperty ("iss" , issuer );
622- payload .addProperty ("aud" , audience );
623- payload .addProperty ("scope" , "runtime_agent" );
624- payload .addProperty ("iat" , currentTimeSeconds );
625- payload .addProperty ("exp" , expirationTime );
626- String encodedPayload = Base64 .getUrlEncoder ().withoutPadding ()
627- .encodeToString (payload .toString ().getBytes ("UTF-8" ));
628-
629- // Create signature
630- String signingInput = encodedHeader + "." + encodedPayload ;
631- java .security .Signature signature = java .security .Signature .getInstance ("SHA256withRSA" );
632- signature .initSign (privateKey );
633- signature .update (signingInput .getBytes ("UTF-8" ));
634- byte [] signatureBytes = signature .sign ();
635- String encodedSignature = Base64 .getUrlEncoder ().withoutPadding ().encodeToString (signatureBytes );
636-
637- return signingInput + "." + encodedSignature ;
577+ // Generate new token
578+ cachedJwtToken = hmacJWTTokenGenerator .generateToken (issuer , audience , scope , expirySeconds );
579+ jwtTokenExpiry = currentTime + (expirySeconds * 1000 );
580+ return cachedJwtToken ;
638581 }
639582
640583 /**
@@ -1437,14 +1380,14 @@ private static JsonArray collectListeners() {
14371380 // HTTP Listener
14381381 JsonObject httpListener = new JsonObject ();
14391382 httpListener .addProperty ("protocol" , "http" );
1440- httpListener .addProperty ("port" , Integer . toString ( ConfigurationLoader .getInternalInboundHttpPort () ));
1383+ httpListener .addProperty ("port" , ConfigurationLoader .getInternalInboundHttpPort ());
14411384 httpListener .addProperty ("host" , "0.0.0.0" );
14421385 listeners .add (httpListener );
14431386
14441387 // HTTPS Listener
14451388 JsonObject httpsListener = new JsonObject ();
14461389 httpsListener .addProperty ("protocol" , "https" );
1447- httpsListener .addProperty ("port" , Integer . toString ( ConfigurationLoader .getInternalInboundHttpsPort () ));
1390+ httpsListener .addProperty ("port" , ConfigurationLoader .getInternalInboundHttpsPort ());
14481391 httpsListener .addProperty ("host" , "0.0.0.0" );
14491392 listeners .add (httpsListener );
14501393 } catch (Exception e ) {
0 commit comments