KEYCLOAK-1169 Skip DB write at each broker login if 3rd party token is not required
This commit is contained in:
parent
58068946e4
commit
a5567ead7a
2 changed files with 35 additions and 4 deletions
27
core/src/main/java/org/keycloak/util/ObjectUtil.java
Normal file
27
core/src/main/java/org/keycloak/util/ObjectUtil.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package org.keycloak.util;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||
*/
|
||||
public class ObjectUtil {
|
||||
|
||||
private ObjectUtil() {}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param str1
|
||||
* @param str2
|
||||
* @return true if both strings are null or equal
|
||||
*/
|
||||
public static boolean isEqualOrNull(Object str1, Object str2) {
|
||||
if (str1 == null && str2 == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((str1 != null && str2 == null) || (str1 == null && str2 != null)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str1.equals(str2);
|
||||
}
|
||||
}
|
|
@ -50,6 +50,7 @@ import org.keycloak.services.resources.flows.Flows;
|
|||
import org.keycloak.services.resources.flows.Urls;
|
||||
import org.keycloak.services.validation.Validation;
|
||||
import org.keycloak.social.SocialIdentityProvider;
|
||||
import org.keycloak.util.ObjectUtil;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
|
@ -342,12 +343,15 @@ public class IdentityBrokerService implements IdentityProvider.AuthenticationCal
|
|||
private void updateFederatedIdentity(FederatedIdentity updatedIdentity, UserModel federatedUser) {
|
||||
FederatedIdentityModel federatedIdentityModel = this.session.users().getFederatedIdentity(federatedUser, updatedIdentity.getIdentityProviderId(), this.realmModel);
|
||||
|
||||
federatedIdentityModel.setToken(updatedIdentity.getToken());
|
||||
// Skip DB write if tokens are null or equal
|
||||
if (!ObjectUtil.isEqualOrNull(updatedIdentity.getToken(), federatedIdentityModel.getToken())) {
|
||||
federatedIdentityModel.setToken(updatedIdentity.getToken());
|
||||
|
||||
this.session.users().updateFederatedIdentity(this.realmModel, federatedUser, federatedIdentityModel);
|
||||
this.session.users().updateFederatedIdentity(this.realmModel, federatedUser, federatedIdentityModel);
|
||||
|
||||
if (isDebugEnabled()) {
|
||||
LOGGER.debugf("Identity [%s] update with response from identity provider [%s].", federatedUser, updatedIdentity.getIdentityProviderId());
|
||||
if (isDebugEnabled()) {
|
||||
LOGGER.debugf("Identity [%s] update with response from identity provider [%s].", federatedUser, updatedIdentity.getIdentityProviderId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue