Skip to content

Commit

Permalink
fixed code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
kyooosukedn committed Jan 14, 2024
1 parent fd6518a commit 12b521c
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public static void authTokenByPublicKey(AclProperties aclProperties) {
throw new AclException("group:" + aclProperties.getExtendedField("group ") + " has no auth to access the topic:"
+ aclProperties.getTopic());
}
String publicKeyUrl = null;
String sub = validateTokenAndGetSub(token, publicKeyUrl, aclProperties);
token = token.replace("Bearer ", "");
String publicKeyUrl = getPublicKeyUrl();
validateToken(token, publicKeyUrl, aclProperties);
} else {
throw new AclException("invalid token!");
}
Expand All @@ -62,8 +63,8 @@ public static void authTokenByPublicKey(AclProperties aclProperties) {
public static void helloTaskAuthTokenByPublicKey(AclProperties aclProperties) {
String token = aclProperties.getToken();
if (StringUtils.isNotBlank(token)) {
String publicKeyUrl = null;
String sub = validateTokenAndGetSub(token, publicKeyUrl, aclProperties);
String publicKeyUrl = getPublicKeyUrl();
validateToken(token, publicKeyUrl, aclProperties);
} else {
throw new AclException("invalid token!");
}
Expand All @@ -84,8 +85,8 @@ public static boolean authAccess(AclProperties aclProperties) {

}

public static String validateTokenAndGetSub(String token, String publicKeyUrl, AclProperties aclProperties) {
String sub = null;
public static void validateToken(String token, String publicKeyUrl, AclProperties aclProperties) {
String sub;
token = token.replace("Bearer ", "");
byte[] validationKeyBytes;
try {
Expand All @@ -109,7 +110,22 @@ public static String validateTokenAndGetSub(String token, String publicKeyUrl, A
} catch (JwtException e) {
throw new AclException("invalid token!", e);
}
return sub;

}

public static String getPublicKeyUrl() {
String publicKeyUrl = null;
for (String key : ConfigurationContextUtil.KEYS) {
CommonConfiguration commonConfiguration = ConfigurationContextUtil.get(key);
if (null == commonConfiguration) {
continue;
}
if (StringUtils.isBlank(commonConfiguration.getEventMeshSecurityPublickey())) {
throw new AclException("publicKeyUrl cannot be null");
}
publicKeyUrl = commonConfiguration.getEventMeshSecurityPublickey();
}
return publicKeyUrl;

}

Expand Down

0 comments on commit 12b521c

Please sign in to comment.