Skip to content
This repository has been archived by the owner on Dec 31, 2021. It is now read-only.

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
flytreeleft committed Nov 9, 2020
1 parent f775859 commit 4bd91b6
Showing 1 changed file with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,23 @@ public boolean authenticate(UsernamePasswordToken token) {
public boolean authenticate(KeycloakHttpHeaderAuthToken token) {
String principal = token.getPrincipal();
String credentials = token.getCredentials().toString();
boolean authenticated = false;

UserInfo userInfo = this.keycloakAdminClient.obtainUserInfo(credentials);
if (userInfo == null) {
return false;
}
authenticated = userInfo.getPreferredUsername().equals(principal);
this.logger.info("principal is {} while pun is {}, so auth result is {}",
principal,
userInfo.getPreferredUsername(),
authenticated);

if (authenticated) {
return authenticated;

if (userInfo.getPreferredUsername().equals(principal)) {
return true;
}
boolean isEmail = this.keycloakAdminClient.isEmail(principal);
if (isEmail) {
logger.info("The principal {} doesn't match the preferred username, try to verify it as an email", principal);

if (this.keycloakAdminClient.isEmail(principal)) {
return userInfo.getEmailVerified() && principal.equals(userInfo.getEmail());
}
return authenticated;
logger.info("The principal {} isn't an email, authentication is failed", principal);

return false;
}

public Set<String> findRoleIdsByUserId(String userId) {
Expand Down

0 comments on commit 4bd91b6

Please sign in to comment.