1717import java .io .File ;
1818import java .io .FileNotFoundException ;
1919import java .io .FileReader ;
20+ import java .net .MalformedURLException ;
2021import java .security .PrivateKey ;
2122import java .util .ArrayList ;
2223import java .util .Date ;
@@ -30,6 +31,7 @@ public class SignedDataTokens {
3031 private final String ctx ;
3132 private final ArrayList <String > dataTokens ;
3233 private final Integer timeToLive ;
34+ private final String tokenUri ;
3335
3436 private SignedDataTokens (SignedDataTokensBuilder builder ) {
3537 this .credentialsFile = builder .credentialsFile ;
@@ -38,14 +40,15 @@ private SignedDataTokens(SignedDataTokensBuilder builder) {
3840 this .ctx = builder .ctx ;
3941 this .dataTokens = builder .dataTokens ;
4042 this .timeToLive = builder .timeToLive ;
43+ this .tokenUri = builder .tokenUri ;
4144 }
4245
4346 public static SignedDataTokensBuilder builder () {
4447 return new SignedDataTokensBuilder ();
4548 }
4649
4750 private static List <SignedDataTokenResponse > generateSignedTokenFromCredentialsFile (
48- File credentialsFile , ArrayList <String > dataTokens , Integer timeToLive , String context
51+ File credentialsFile , ArrayList <String > dataTokens , Integer timeToLive , String context , String overrideTokenUri
4952 ) throws SkyflowException {
5053 LogUtil .printInfoLog (InfoLogs .GENERATE_SIGNED_TOKENS_FROM_CREDENTIALS_FILE_TRIGGERED .getLog ());
5154 List <SignedDataTokenResponse > responseToken ;
@@ -56,7 +59,7 @@ private static List<SignedDataTokenResponse> generateSignedTokenFromCredentialsF
5659 }
5760 FileReader reader = new FileReader (String .valueOf (credentialsFile ));
5861 JsonObject serviceAccountCredentials = JsonParser .parseReader (reader ).getAsJsonObject ();
59- responseToken = generateSignedTokensFromCredentials (serviceAccountCredentials , dataTokens , timeToLive , context );
62+ responseToken = generateSignedTokensFromCredentials (serviceAccountCredentials , dataTokens , timeToLive , context , overrideTokenUri );
6063 } catch (JsonSyntaxException e ) {
6164 LogUtil .printErrorLog (ErrorLogs .INVALID_CREDENTIALS_FILE_FORMAT .getLog ());
6265 throw new SkyflowException (ErrorCode .INVALID_INPUT .getCode (), Utils .parameterizedString (
@@ -70,7 +73,7 @@ private static List<SignedDataTokenResponse> generateSignedTokenFromCredentialsF
7073 }
7174
7275 private static List <SignedDataTokenResponse > generateSignedTokensFromCredentialsString (
73- String credentials , ArrayList <String > dataTokens , Integer timeToLive , String context
76+ String credentials , ArrayList <String > dataTokens , Integer timeToLive , String context , String overrideTokenUri
7477 ) throws SkyflowException {
7578 LogUtil .printInfoLog (InfoLogs .GENERATE_SIGNED_TOKENS_FROM_CREDENTIALS_STRING_TRIGGERED .getLog ());
7679 List <SignedDataTokenResponse > responseToken ;
@@ -80,7 +83,7 @@ private static List<SignedDataTokenResponse> generateSignedTokensFromCredentials
8083 throw new SkyflowException (ErrorCode .INVALID_INPUT .getCode (), ErrorMessage .InvalidCredentials .getMessage ());
8184 }
8285 JsonObject serviceAccountCredentials = JsonParser .parseString (credentials ).getAsJsonObject ();
83- responseToken = generateSignedTokensFromCredentials (serviceAccountCredentials , dataTokens , timeToLive , context );
86+ responseToken = generateSignedTokensFromCredentials (serviceAccountCredentials , dataTokens , timeToLive , context , overrideTokenUri );
8487 } catch (JsonSyntaxException e ) {
8588 LogUtil .printErrorLog (ErrorLogs .INVALID_CREDENTIALS_STRING_FORMAT .getLog ());
8689 throw new SkyflowException (ErrorCode .INVALID_INPUT .getCode (),
@@ -90,7 +93,7 @@ private static List<SignedDataTokenResponse> generateSignedTokensFromCredentials
9093 }
9194
9295 private static List <SignedDataTokenResponse > generateSignedTokensFromCredentials (
93- JsonObject credentials , ArrayList <String > dataTokens , Integer timeToLive , String context
96+ JsonObject credentials , ArrayList <String > dataTokens , Integer timeToLive , String context , String overrideTokenUri
9497 ) throws SkyflowException {
9598 List <SignedDataTokenResponse > signedDataTokens = null ;
9699 try {
@@ -113,7 +116,7 @@ private static List<SignedDataTokenResponse> generateSignedTokensFromCredentials
113116 }
114117 PrivateKey pvtKey = Utils .getPrivateKeyFromPem (privateKey .getAsString ());
115118 signedDataTokens = getSignedToken (
116- clientID .getAsString (), keyID .getAsString (), pvtKey , dataTokens , timeToLive , context );
119+ clientID .getAsString (), keyID .getAsString (), pvtKey , dataTokens , timeToLive , context , overrideTokenUri );
117120 } catch (RuntimeException e ) {
118121 LogUtil .printErrorLog (ErrorLogs .SIGNED_DATA_TOKENS_REJECTED .getLog ());
119122 throw new SkyflowException (e );
@@ -123,7 +126,7 @@ private static List<SignedDataTokenResponse> generateSignedTokensFromCredentials
123126
124127 private static List <SignedDataTokenResponse > getSignedToken (
125128 String clientID , String keyID , PrivateKey pvtKey ,
126- ArrayList <String > dataTokens , Integer timeToLive , String context
129+ ArrayList <String > dataTokens , Integer timeToLive , String context , String overrideTokenUri
127130 ) {
128131 final Date createdDate = new Date ();
129132 final Date expirationDate ;
@@ -134,15 +137,21 @@ private static List<SignedDataTokenResponse> getSignedToken(
134137 expirationDate = new Date (createdDate .getTime () + 60000 ); // Valid for 60 seconds
135138 }
136139
140+ String finalTokenUri = null ;
141+ if (overrideTokenUri != null && !overrideTokenUri .isEmpty ()) {
142+ finalTokenUri = overrideTokenUri ;
143+ }
144+
137145 List <SignedDataTokenResponse > list = new ArrayList <>();
138146 for (String dataToken : dataTokens ) {
139147 String eachSignedDataToken = Jwts .builder ()
140- .claim (Constants .JwtClaims .ISS , Constants . JwtClaims . SDK )
148+ .claim (Constants .JwtClaims .ISS , "sdk" )
141149 .claim (Constants .JwtClaims .IAT , (createdDate .getTime () / 1000 ))
142150 .claim (Constants .JwtClaims .KEY , keyID )
143151 .claim (Constants .JwtClaims .SUB , clientID )
144152 .claim (Constants .JwtClaims .CTX , context )
145153 .claim (Constants .JwtClaims .TOK , dataToken )
154+ .claim (Constants .JwtClaims .AUD , finalTokenUri )
146155 .expiration (expirationDate )
147156 .signWith (pvtKey , Jwts .SIG .RS256 )
148157 .compact ();
@@ -156,9 +165,9 @@ public synchronized List<SignedDataTokenResponse> getSignedDataTokens() throws S
156165 LogUtil .printInfoLog (InfoLogs .GET_SIGNED_DATA_TOKENS_TRIGGERED .getLog ());
157166 List <SignedDataTokenResponse > signedToken = new ArrayList <>();
158167 if (this .credentialsFile != null && Objects .equals (this .credentialsType , Constants .CredentialTypeValues .FILE )) {
159- signedToken = generateSignedTokenFromCredentialsFile (this .credentialsFile , this .dataTokens , this .timeToLive , this .ctx );
168+ signedToken = generateSignedTokenFromCredentialsFile (this .credentialsFile , this .dataTokens , this .timeToLive , this .ctx , this . tokenUri );
160169 } else if (this .credentialsString != null && Objects .equals (this .credentialsType , Constants .CredentialTypeValues .STRING )) {
161- signedToken = generateSignedTokensFromCredentialsString (this .credentialsString , this .dataTokens , this .timeToLive , this .ctx );
170+ signedToken = generateSignedTokensFromCredentialsString (this .credentialsString , this .dataTokens , this .timeToLive , this .ctx , this . tokenUri );
162171 }
163172 LogUtil .printInfoLog (InfoLogs .GET_SIGNED_DATA_TOKEN_SUCCESS .getLog ());
164173 return signedToken ;
@@ -171,6 +180,7 @@ public static class SignedDataTokensBuilder {
171180 private String credentialsString ;
172181 private String ctx ;
173182 private String credentialsType ;
183+ private String tokenUri ;
174184
175185 private SignedDataTokensBuilder () {
176186 }
@@ -206,6 +216,19 @@ public SignedDataTokensBuilder setTimeToLive(Integer timeToLive) {
206216 return this ;
207217 }
208218
219+ public SignedDataTokensBuilder setTokenUri (String tokenUri ) throws SkyflowException {
220+ if (tokenUri != null && !tokenUri .isEmpty ()) {
221+ try {
222+ new java .net .URL (tokenUri );
223+ this .tokenUri = tokenUri ;
224+ } catch (MalformedURLException e ) {
225+ LogUtil .printErrorLog (ErrorLogs .INVALID_TOKEN_URI .getLog ());
226+ throw new SkyflowException (ErrorCode .INVALID_INPUT .getCode (), ErrorMessage .InvalidTokenUri .getMessage ());
227+ }
228+ }
229+ return this ;
230+ }
231+
209232 public SignedDataTokens build () {
210233 return new SignedDataTokens (this );
211234 }
0 commit comments