Skip to content

Commit 498c350

Browse files
authored
[ISSUE #3515] Do some code optimization[AuthTokenUtils] (#3644)
* Code optimization * Remove unused imports * Added one forgotten import 'Objects' * Added Constants import * removed unnecessary space * fixed code optimization * corrected some small changes * redundant lines * redundant lines * optimized returned reply in subscribe method * reverted correct change * reverted back
1 parent fdac217 commit 498c350

File tree

1 file changed

+45
-69
lines changed
  • eventmesh-security-plugin/eventmesh-security-auth-token/src/main/java/org/apache/eventmesh/auth/token/impl/auth

1 file changed

+45
-69
lines changed

eventmesh-security-plugin/eventmesh-security-auth-token/src/main/java/org/apache/eventmesh/auth/token/impl/auth/AuthTokenUtils.java

+45-69
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.security.NoSuchAlgorithmException;
3434
import java.security.spec.InvalidKeySpecException;
3535
import java.security.spec.X509EncodedKeySpec;
36+
import java.util.Objects;
3637
import java.util.Set;
3738

3839
import io.jsonwebtoken.Claims;
@@ -51,41 +52,8 @@ public static void authTokenByPublicKey(AclProperties aclProperties) {
5152
throw new AclException("group:" + aclProperties.getExtendedField("group ") + " has no auth to access the topic:"
5253
+ aclProperties.getTopic());
5354
}
54-
String publicKeyUrl = null;
55-
token = token.replace("Bearer ", "");
56-
for (String key : ConfigurationContextUtil.KEYS) {
57-
CommonConfiguration commonConfiguration = ConfigurationContextUtil.get(key);
58-
if (commonConfiguration == null) {
59-
continue;
60-
}
61-
if (StringUtils.isBlank(commonConfiguration.getEventMeshSecurityPublickey())) {
62-
throw new AclException("publicKeyUrl cannot be null");
63-
}
64-
publicKeyUrl = commonConfiguration.getEventMeshSecurityPublickey();
65-
}
66-
byte[] validationKeyBytes = new byte[0];
67-
try {
68-
validationKeyBytes = Files.readAllBytes(Paths.get(publicKeyUrl));
69-
X509EncodedKeySpec spec = new X509EncodedKeySpec(validationKeyBytes);
70-
KeyFactory kf = KeyFactory.getInstance("RSA");
71-
Key validationKey = kf.generatePublic(spec);
72-
JwtParser signedParser = Jwts.parserBuilder().setSigningKey(validationKey).build();
73-
Jwt<?, Claims> signJwt = signedParser.parseClaimsJws(token);
74-
String sub = signJwt.getBody().get("sub", String.class);
75-
if (!sub.contains(aclProperties.getExtendedField("group").toString()) && !sub.contains("pulsar-admin")) {
76-
throw new AclException("group:" + aclProperties.getExtendedField("group ") + " has no auth to access eventMesh:"
77-
+ aclProperties.getTopic());
78-
}
79-
} catch (IOException e) {
80-
throw new AclException("public key read error!", e);
81-
} catch (NoSuchAlgorithmException e) {
82-
throw new AclException("no such RSA algorithm!", e);
83-
} catch (InvalidKeySpecException e) {
84-
throw new AclException("invalid public key spec!", e);
85-
} catch (JwtException e) {
86-
throw new AclException("invalid token!", e);
87-
}
88-
55+
String publicKeyUrl = getPublicKeyUrl();
56+
validateToken(token, publicKeyUrl, aclProperties);
8957
} else {
9058
throw new AclException("invalid token!");
9159
}
@@ -94,40 +62,7 @@ public static void authTokenByPublicKey(AclProperties aclProperties) {
9462
public static void helloTaskAuthTokenByPublicKey(AclProperties aclProperties) {
9563
String token = aclProperties.getToken();
9664
if (StringUtils.isNotBlank(token)) {
97-
String publicKeyUrl = null;
98-
token = token.replace("Bearer ", "");
99-
for (String key : ConfigurationContextUtil.KEYS) {
100-
CommonConfiguration commonConfiguration = ConfigurationContextUtil.get(key);
101-
if (commonConfiguration == null) {
102-
continue;
103-
}
104-
if (StringUtils.isBlank(commonConfiguration.getEventMeshSecurityPublickey())) {
105-
throw new AclException("publicKeyUrl cannot be null");
106-
}
107-
publicKeyUrl = commonConfiguration.getEventMeshSecurityPublickey();
108-
}
109-
byte[] validationKeyBytes = new byte[0];
110-
try {
111-
validationKeyBytes = Files.readAllBytes(Paths.get(publicKeyUrl));
112-
X509EncodedKeySpec spec = new X509EncodedKeySpec(validationKeyBytes);
113-
KeyFactory kf = KeyFactory.getInstance("RSA");
114-
Key validationKey = kf.generatePublic(spec);
115-
JwtParser signedParser = Jwts.parserBuilder().setSigningKey(validationKey).build();
116-
Jwt<?, Claims> signJwt = signedParser.parseClaimsJws(token);
117-
String sub = signJwt.getBody().get("sub", String.class);
118-
if (!sub.contains(aclProperties.getExtendedField("group").toString()) && !sub.contains("pulsar-admin")) {
119-
throw new AclException("group:" + aclProperties.getExtendedField("group ") + " has no auth to access eventMesh:"
120-
+ aclProperties.getTopic());
121-
}
122-
} catch (IOException e) {
123-
throw new AclException("public key read error!", e);
124-
} catch (NoSuchAlgorithmException e) {
125-
throw new AclException("no such RSA algorithm!", e);
126-
} catch (InvalidKeySpecException e) {
127-
throw new AclException("invalid public key spec!", e);
128-
} catch (JwtException e) {
129-
throw new AclException("invalid token!", e);
130-
}
65+
validateToken(token, getPublicKeyUrl(), aclProperties);
13166
} else {
13267
throw new AclException("invalid token!");
13368
}
@@ -148,4 +83,45 @@ public static boolean authAccess(AclProperties aclProperties) {
14883
return groupTopics.contains(topic);
14984
}
15085

86+
private static String getPublicKeyUrl() {
87+
String publicKeyUrl = null;
88+
for (String key : ConfigurationContextUtil.KEYS) {
89+
CommonConfiguration commonConfiguration = ConfigurationContextUtil.get(key);
90+
if (null == commonConfiguration) {
91+
continue;
92+
}
93+
if (StringUtils.isBlank(commonConfiguration.getEventMeshSecurityPublickey())) {
94+
throw new AclException("publicKeyUrl cannot be null");
95+
}
96+
publicKeyUrl = commonConfiguration.getEventMeshSecurityPublickey();
97+
}
98+
return publicKeyUrl;
99+
}
100+
101+
private static void validateToken(String token, String publicKeyUrl, AclProperties aclProperties) {
102+
String sub;
103+
token = token.replace("Bearer ", "");
104+
byte[] validationKeyBytes;
105+
try {
106+
validationKeyBytes = Files.readAllBytes(Paths.get(Objects.requireNonNull(publicKeyUrl)));
107+
X509EncodedKeySpec spec = new X509EncodedKeySpec(validationKeyBytes);
108+
KeyFactory kf = KeyFactory.getInstance("RSA");
109+
Key validationKey = kf.generatePublic(spec);
110+
JwtParser signedParser = Jwts.parserBuilder().setSigningKey(validationKey).build();
111+
Jwt<?, Claims> signJwt = signedParser.parseClaimsJws(token);
112+
sub = signJwt.getBody().get("sub", String.class);
113+
if (!sub.contains(aclProperties.getExtendedField("group").toString()) && !sub.contains("pulsar-admin")) {
114+
throw new AclException("group:" + aclProperties.getExtendedField("group ") + " has no auth to access eventMesh:"
115+
+ aclProperties.getTopic());
116+
}
117+
} catch (IOException e) {
118+
throw new AclException("public key read error!", e);
119+
} catch (NoSuchAlgorithmException e) {
120+
throw new AclException("no such RSA algorithm!", e);
121+
} catch (InvalidKeySpecException e) {
122+
throw new AclException("invalid public key spec!", e);
123+
} catch (JwtException e) {
124+
throw new AclException("invalid token!", e);
125+
}
126+
}
151127
}

0 commit comments

Comments
 (0)