14
14
import java .util .ArrayList ;
15
15
import java .util .Arrays ;
16
16
import java .util .HashSet ;
17
- import java .util .Iterator ;
18
17
import java .util .List ;
19
18
import java .util .Set ;
20
19
import java .util .concurrent .CompletableFuture ;
@@ -54,12 +53,12 @@ public class OAuth2TokenExchangeProvider implements AuthRpcProvider<GrpcAuthRpc>
54
53
55
54
private static final Logger logger = LoggerFactory .getLogger (OAuth2TokenExchangeProvider .class );
56
55
private static final Gson GSON = new Gson ();
57
- private static final Set <String > SUPPORTED_JWT_ALGS = new HashSet <String >(Arrays .asList (new String []{
58
- "HS256" , "HS384" , "HS512" ,
59
- "RS256" , "RS384" , "RS512" ,
60
- "PS256" , "PS384" , "PS512" ,
61
- "ES256" , "ES384" , "ES512" ,
62
- } ));
56
+ private static final Set <String > SUPPORTED_JWT_ALGS = new HashSet <>(Arrays .asList (
57
+ "HS256" , "HS384" , "HS512" ,
58
+ "RS256" , "RS384" , "RS512" ,
59
+ "PS256" , "PS384" , "PS512" ,
60
+ "ES256" , "ES384" , "ES512"
61
+ ));
63
62
64
63
private final Clock clock ;
65
64
private final String endpoint ;
@@ -83,41 +82,40 @@ private OAuth2TokenExchangeProvider(Clock clock, String endpoint, String scope,
83
82
public static String [] getSupportedJwtAlgorithms () {
84
83
String [] result = new String [SUPPORTED_JWT_ALGS .size ()];
85
84
int i = 0 ;
86
- Iterator <String > it = SUPPORTED_JWT_ALGS .iterator ();
87
- while (it .hasNext ()) {
88
- result [i ++] = it .next ();
85
+ for (String supportedJwtAlg : SUPPORTED_JWT_ALGS ) {
86
+ result [i ++] = supportedJwtAlg ;
89
87
}
90
88
Arrays .sort (result );
91
89
return result ;
92
90
}
93
91
94
92
private static OAuth2TokenSource buildFixedTokenSourceFromConfig (TokenSourceJsonConfig cfg ) {
95
- if (cfg .getToken () == null || cfg .getToken ().length () == 0
96
- || cfg .getTokenType () == null || cfg .getTokenType ().length () == 0 ) {
93
+ if (cfg .getToken () == null || cfg .getToken ().isEmpty ()
94
+ || cfg .getTokenType () == null || cfg .getTokenType ().isEmpty () ) {
97
95
throw new RuntimeException ("Both token and token-type are required" );
98
96
}
99
97
return OAuth2TokenSource .fromValue (cfg .getToken (), cfg .getTokenType ());
100
98
}
101
99
102
100
private static OAuth2TokenSource buildJwtTokenSourceFromConfig (TokenSourceJsonConfig cfg ) {
103
- if (cfg .getAlg () == null || cfg .getAlg ().length () == 0 ) {
101
+ if (cfg .getAlg () == null || cfg .getAlg ().isEmpty () ) {
104
102
throw new RuntimeException ("Algorithm is required" );
105
103
}
106
- if (cfg .getPrivateKey () == null || cfg .getPrivateKey ().length () == 0 ) {
104
+ if (cfg .getPrivateKey () == null || cfg .getPrivateKey ().isEmpty () ) {
107
105
throw new RuntimeException ("Key is required" );
108
106
}
109
107
110
108
String alg = cfg .getAlg ().toUpperCase ();
111
109
if (!SUPPORTED_JWT_ALGS .contains (alg )) {
112
110
String [] supportedAlgs = getSupportedJwtAlgorithms ();
113
- String lstMsg = "" ;
111
+ StringBuilder lstMsg = new StringBuilder () ;
114
112
for (int i = 0 ; i < supportedAlgs .length ; i ++) {
115
113
if (lstMsg .length () > 0 ) {
116
- lstMsg += ", " ;
114
+ lstMsg . append ( ", " ) ;
117
115
}
118
- lstMsg += "\" " ;
119
- lstMsg += supportedAlgs [i ];
120
- lstMsg += "\" " ;
116
+ lstMsg . append ( "\" " ) ;
117
+ lstMsg . append ( supportedAlgs [i ]) ;
118
+ lstMsg . append ( "\" " ) ;
121
119
}
122
120
throw new RuntimeException (
123
121
String .format ("Algorithm \" %s\" is not supported. Supported algorithms: %s" ,
@@ -129,7 +127,7 @@ private static OAuth2TokenSource buildJwtTokenSourceFromConfig(TokenSourceJsonCo
129
127
boolean isHmac = "HS256" .equals (alg )
130
128
|| "HS384" .equals (alg )
131
129
|| "HS512" .equals (alg );
132
- OAuth2TokenSource .JWTTokenBuilder builder = null ;
130
+ OAuth2TokenSource .JWTTokenBuilder builder ;
133
131
if (isHmac ) {
134
132
builder = OAuth2TokenSource .withHmacPrivateKeyBase64 (cfg .getPrivateKey (), alg );
135
133
} else {
@@ -196,13 +194,13 @@ public static Builder fromFile(File configFile) {
196
194
builder .withCustomGrantType (cfg .getGrantType ());
197
195
}
198
196
199
- if (cfg .getResource () != null && cfg . getResource (). length != 0 ) {
197
+ if (cfg .getResource () != null ) {
200
198
for (String res : cfg .getResource ()) {
201
199
builder .withResource (res );
202
200
}
203
201
}
204
202
205
- if (cfg .getAudience () != null && cfg . getAudience (). length != 0 ) {
203
+ if (cfg .getAudience () != null ) {
206
204
for (String audience : cfg .getAudience ()) {
207
205
builder .withAudience (audience );
208
206
}
@@ -535,13 +533,13 @@ private static class SingleStringOrArrayOfStringsJsonConfigDeserializer implemen
535
533
public String [] deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) {
536
534
if (json .isJsonArray ()) {
537
535
JsonArray arr = json .getAsJsonArray ();
538
- if (arr .size () == 0 ) {
536
+ if (arr .isEmpty () ) {
539
537
return null ;
540
538
}
541
539
String [] result = new String [arr .size ()];
542
540
for (int i = 0 ; i < arr .size (); i ++) {
543
541
result [i ] = arr .get (i ).getAsJsonPrimitive ().getAsString ();
544
- if (result [i ].length () == 0 ) {
542
+ if (result [i ].isEmpty () ) {
545
543
throw new RuntimeException ("Cannot parse config from json: empty string" );
546
544
}
547
545
}
@@ -550,7 +548,7 @@ public String[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
550
548
if (json .isJsonPrimitive ()) {
551
549
String [] result = new String [1 ];
552
550
result [0 ] = json .getAsJsonPrimitive ().getAsString ();
553
- if (result [0 ].length () == 0 ) {
551
+ if (result [0 ].isEmpty () ) {
554
552
throw new RuntimeException ("Cannot parse config from json: empty string" );
555
553
}
556
554
return result ;
@@ -715,14 +713,14 @@ public String[] getScope() {
715
713
}
716
714
717
715
public String buildScope () {
718
- String result = new String ();
716
+ StringBuilder result = new StringBuilder ();
719
717
for (String s : this .scope ) {
720
718
if (result .length () != 0 ) {
721
- result += " " ;
719
+ result . append ( " " ) ;
722
720
}
723
- result += s ;
721
+ result . append ( s ) ;
724
722
}
725
- return result ;
723
+ return result . toString () ;
726
724
}
727
725
728
726
public String getRequestedTokenType () {
0 commit comments