Skip to content

Commit

Permalink
Use Keycloak's StringUtil to check for blank strings
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverhaagh committed Aug 29, 2024
1 parent 6fdba32 commit ccf1a55
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.utils.StringUtil;

import java.security.cert.X509Certificate;
import java.util.Collections;
Expand Down Expand Up @@ -89,7 +90,7 @@ public void authenticate(AuthenticationFlowContext authenticationFlowContext) {

UserModel federatedUser = session.users().addUser(realm, mrn);
federatedUser.setEnabled(true);
if (email != null && !email.trim().isEmpty()) {
if (StringUtil.isNotBlank(email)) {
federatedUser.setEmail(email);
}
federatedUser.setFirstName(user.getFirstName());
Expand All @@ -99,7 +100,7 @@ public void authenticate(AuthenticationFlowContext authenticationFlowContext) {
} else {
log.debugf("Existing user detected with %s '%s' .", UserModel.USERNAME, existingUser.getUsername());

if (email != null && !email.trim().isEmpty()) {
if (StringUtil.isNotBlank(email)) {
existingUser.setEmail(email);
} else if (existingUser.getEmail() != null) {
existingUser.setEmail(null);
Expand All @@ -118,7 +119,7 @@ public void authenticate(AuthenticationFlowContext authenticationFlowContext) {
}

private void setUserAttributes(AuthenticationFlowContext authenticationFlowContext, PKIIdentity user, String mrn, String orgMrn, String permissions, String uid, String mrnSubsidiary, String homeMmsUrl, UserModel userModel) {
if (permissions != null && !permissions.trim().isEmpty()) {
if (StringUtil.isNotBlank(permissions)) {
log.debug("About to set permissions attr to: " + permissions);
userModel.setAttribute("permissions", Collections.singletonList(permissions));
log.debug("Just set permissions attr to: " + permissions);
Expand All @@ -132,19 +133,19 @@ private void setUserAttributes(AuthenticationFlowContext authenticationFlowConte
userModel.setAttribute("uid", Collections.singletonList(uid));
log.debug("Just set uid attr to: " + uid);

if (!orgMrn.trim().isEmpty()) {
if (StringUtil.isNotBlank(orgMrn)) {
log.debug("About to set org attr to: " + orgMrn);
userModel.setAttribute("org", Collections.singletonList(orgMrn));
log.debug("Just set org attr to: " + orgMrn);
}

if (mrnSubsidiary != null && !mrnSubsidiary.trim().isEmpty()) {
if (StringUtil.isNotBlank(mrnSubsidiary)) {
log.debug("About to set subsidiary_mrn attr to: " + mrnSubsidiary);
userModel.setAttribute("subsidiary_mrn", Collections.singletonList(mrnSubsidiary));
log.debug("Just set subsidiary_mrn attr to: " + mrnSubsidiary);
}

if (homeMmsUrl != null && !homeMmsUrl.trim().isEmpty()) {
if (StringUtil.isNotBlank(homeMmsUrl)) {
log.debug("About to set mms_url attr to: " + homeMmsUrl);
userModel.setAttribute("mms_url", Collections.singletonList(homeMmsUrl));
log.debug("Just set mms_url attr to: " + homeMmsUrl);
Expand All @@ -165,28 +166,28 @@ private void extractNonUserAttributes(PKIIdentity user, UserModel userModel) {
String shipMrn = user.getShipMrn();
String url = user.getUrl();

if (flagState != null && !flagState.trim().isEmpty()) {
if (StringUtil.isNotBlank(flagState)) {
userModel.setAttribute("flagstate", Collections.singletonList(flagState));
}
if (callSign != null && !callSign.trim().isEmpty()) {
if (StringUtil.isNotBlank(callSign)) {
userModel.setAttribute("callsign", Collections.singletonList(callSign));
}
if (imoNumber != null && !imoNumber.trim().isEmpty()) {
if (StringUtil.isNotBlank(imoNumber)) {
userModel.setAttribute("imo_number", Collections.singletonList(imoNumber));
}
if (mmsiNumber != null && !mmsiNumber.trim().isEmpty()) {
if (StringUtil.isNotBlank(mmsiNumber)) {
userModel.setAttribute("mmsi", Collections.singletonList(mmsiNumber));
}
if (aisShipType != null && !aisShipType.trim().isEmpty()) {
if (StringUtil.isNotBlank(aisShipType)) {
userModel.setAttribute("ais_type", Collections.singletonList(aisShipType));
}
if (portOfRegister != null && !portOfRegister.trim().isEmpty()) {
if (StringUtil.isNotBlank(portOfRegister)) {
userModel.setAttribute("registered_port", Collections.singletonList(portOfRegister));
}
if (shipMrn != null && !shipMrn.trim().isEmpty()) {
if (StringUtil.isNotBlank(shipMrn)) {
userModel.setAttribute("ship_mrn", Collections.singletonList(shipMrn));
}
if (url != null && !url.trim().isEmpty()) {
if (StringUtil.isNotBlank(url)) {
userModel.setAttribute("url", Collections.singletonList(url));
}
}
Expand Down

0 comments on commit ccf1a55

Please sign in to comment.