Skip to content

Commit c891d8e

Browse files
authored
Merge pull request #499 from Myllyenko/code_cleanup
Code cleanup
2 parents 50b241a + bca8ab5 commit c891d8e

File tree

83 files changed

+554
-482
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+554
-482
lines changed

auth-providers/oauth2-provider/src/main/java/tech/ydb/auth/OAuth2TokenExchangeProvider.java

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.ArrayList;
1515
import java.util.Arrays;
1616
import java.util.HashSet;
17-
import java.util.Iterator;
1817
import java.util.List;
1918
import java.util.Set;
2019
import java.util.concurrent.CompletableFuture;
@@ -54,12 +53,12 @@ public class OAuth2TokenExchangeProvider implements AuthRpcProvider<GrpcAuthRpc>
5453

5554
private static final Logger logger = LoggerFactory.getLogger(OAuth2TokenExchangeProvider.class);
5655
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+
));
6362

6463
private final Clock clock;
6564
private final String endpoint;
@@ -83,41 +82,40 @@ private OAuth2TokenExchangeProvider(Clock clock, String endpoint, String scope,
8382
public static String[] getSupportedJwtAlgorithms() {
8483
String[] result = new String[SUPPORTED_JWT_ALGS.size()];
8584
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;
8987
}
9088
Arrays.sort(result);
9189
return result;
9290
}
9391

9492
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()) {
9795
throw new RuntimeException("Both token and token-type are required");
9896
}
9997
return OAuth2TokenSource.fromValue(cfg.getToken(), cfg.getTokenType());
10098
}
10199

102100
private static OAuth2TokenSource buildJwtTokenSourceFromConfig(TokenSourceJsonConfig cfg) {
103-
if (cfg.getAlg() == null || cfg.getAlg().length() == 0) {
101+
if (cfg.getAlg() == null || cfg.getAlg().isEmpty()) {
104102
throw new RuntimeException("Algorithm is required");
105103
}
106-
if (cfg.getPrivateKey() == null || cfg.getPrivateKey().length() == 0) {
104+
if (cfg.getPrivateKey() == null || cfg.getPrivateKey().isEmpty()) {
107105
throw new RuntimeException("Key is required");
108106
}
109107

110108
String alg = cfg.getAlg().toUpperCase();
111109
if (!SUPPORTED_JWT_ALGS.contains(alg)) {
112110
String[] supportedAlgs = getSupportedJwtAlgorithms();
113-
String lstMsg = "";
111+
StringBuilder lstMsg = new StringBuilder();
114112
for (int i = 0; i < supportedAlgs.length; i++) {
115113
if (lstMsg.length() > 0) {
116-
lstMsg += ", ";
114+
lstMsg.append(", ");
117115
}
118-
lstMsg += "\"";
119-
lstMsg += supportedAlgs[i];
120-
lstMsg += "\"";
116+
lstMsg.append("\"");
117+
lstMsg.append(supportedAlgs[i]);
118+
lstMsg.append("\"");
121119
}
122120
throw new RuntimeException(
123121
String.format("Algorithm \"%s\" is not supported. Supported algorithms: %s",
@@ -129,7 +127,7 @@ private static OAuth2TokenSource buildJwtTokenSourceFromConfig(TokenSourceJsonCo
129127
boolean isHmac = "HS256".equals(alg)
130128
|| "HS384".equals(alg)
131129
|| "HS512".equals(alg);
132-
OAuth2TokenSource.JWTTokenBuilder builder = null;
130+
OAuth2TokenSource.JWTTokenBuilder builder;
133131
if (isHmac) {
134132
builder = OAuth2TokenSource.withHmacPrivateKeyBase64(cfg.getPrivateKey(), alg);
135133
} else {
@@ -196,13 +194,13 @@ public static Builder fromFile(File configFile) {
196194
builder.withCustomGrantType(cfg.getGrantType());
197195
}
198196

199-
if (cfg.getResource() != null && cfg.getResource().length != 0) {
197+
if (cfg.getResource() != null) {
200198
for (String res: cfg.getResource()) {
201199
builder.withResource(res);
202200
}
203201
}
204202

205-
if (cfg.getAudience() != null && cfg.getAudience().length != 0) {
203+
if (cfg.getAudience() != null) {
206204
for (String audience: cfg.getAudience()) {
207205
builder.withAudience(audience);
208206
}
@@ -535,13 +533,13 @@ private static class SingleStringOrArrayOfStringsJsonConfigDeserializer implemen
535533
public String[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
536534
if (json.isJsonArray()) {
537535
JsonArray arr = json.getAsJsonArray();
538-
if (arr.size() == 0) {
536+
if (arr.isEmpty()) {
539537
return null;
540538
}
541539
String[] result = new String[arr.size()];
542540
for (int i = 0; i < arr.size(); i++) {
543541
result[i] = arr.get(i).getAsJsonPrimitive().getAsString();
544-
if (result[i].length() == 0) {
542+
if (result[i].isEmpty()) {
545543
throw new RuntimeException("Cannot parse config from json: empty string");
546544
}
547545
}
@@ -550,7 +548,7 @@ public String[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
550548
if (json.isJsonPrimitive()) {
551549
String[] result = new String[1];
552550
result[0] = json.getAsJsonPrimitive().getAsString();
553-
if (result[0].length() == 0) {
551+
if (result[0].isEmpty()) {
554552
throw new RuntimeException("Cannot parse config from json: empty string");
555553
}
556554
return result;
@@ -715,14 +713,14 @@ public String[] getScope() {
715713
}
716714

717715
public String buildScope() {
718-
String result = new String();
716+
StringBuilder result = new StringBuilder();
719717
for (String s: this.scope) {
720718
if (result.length() != 0) {
721-
result += " ";
719+
result.append(" ");
722720
}
723-
result += s;
721+
result.append(s);
724722
}
725-
return result;
723+
return result.toString();
726724
}
727725

728726
public String getRequestedTokenType() {

auth-providers/oauth2-provider/src/main/java/tech/ydb/auth/OAuth2TokenSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static JWTTokenBuilder withPrivateKeyPem(Reader data, String alg) {
106106
throw new RuntimeException("Failed to parse PEM key");
107107
}
108108

109-
PrivateKeyInfo info = null;
109+
PrivateKeyInfo info;
110110
if (parsed instanceof PrivateKeyInfo) {
111111
info = (PrivateKeyInfo) parsed;
112112
} else if (parsed instanceof PEMKeyPair) {

0 commit comments

Comments
 (0)