Improve handling of exceptions thrown from picketlink

This commit is contained in:
mposolda 2014-04-04 16:35:15 +02:00
parent b7826be103
commit be320e11a5

View file

@ -14,6 +14,7 @@ import org.keycloak.spi.authentication.AuthenticationProvider;
import org.keycloak.spi.authentication.AuthenticationProviderException; import org.keycloak.spi.authentication.AuthenticationProviderException;
import org.keycloak.spi.picketlink.PartitionManagerProvider; import org.keycloak.spi.picketlink.PartitionManagerProvider;
import org.keycloak.util.ProviderLoader; import org.keycloak.util.ProviderLoader;
import org.picketlink.idm.IdentityManagementException;
import org.picketlink.idm.IdentityManager; import org.picketlink.idm.IdentityManager;
import org.picketlink.idm.PartitionManager; import org.picketlink.idm.PartitionManager;
import org.picketlink.idm.credential.Credentials; import org.picketlink.idm.credential.Credentials;
@ -44,17 +45,23 @@ public class PicketlinkAuthenticationProvider implements AuthenticationProvider
@Override @Override
public AuthUser getUser(RealmModel realm, Map<String, String> configuration, String username) throws AuthenticationProviderException { public AuthUser getUser(RealmModel realm, Map<String, String> configuration, String username) throws AuthenticationProviderException {
IdentityManager identityManager = getIdentityManager(realm); IdentityManager identityManager = getIdentityManager(realm);
try {
User picketlinkUser = BasicModel.getUser(identityManager, username); User picketlinkUser = BasicModel.getUser(identityManager, username);
return picketlinkUser == null ? null : new AuthUser(picketlinkUser.getId(), picketlinkUser.getLoginName(), getName()) return picketlinkUser == null ? null : new AuthUser(picketlinkUser.getId(), picketlinkUser.getLoginName(), getName())
.setName(picketlinkUser.getFirstName(), picketlinkUser.getLastName()) .setName(picketlinkUser.getFirstName(), picketlinkUser.getLastName())
.setEmail(picketlinkUser.getEmail()) .setEmail(picketlinkUser.getEmail())
.setProviderName(getName()); .setProviderName(getName());
} catch (IdentityManagementException ie) {
throw convertIDMException(ie);
}
} }
@Override @Override
public AuthProviderStatus validatePassword(RealmModel realm, Map<String, String> configuration, String username, String password) throws AuthenticationProviderException { public AuthProviderStatus validatePassword(RealmModel realm, Map<String, String> configuration, String username, String password) throws AuthenticationProviderException {
IdentityManager identityManager = getIdentityManager(realm); IdentityManager identityManager = getIdentityManager(realm);
try {
UsernamePasswordCredentials credential = new UsernamePasswordCredentials(); UsernamePasswordCredentials credential = new UsernamePasswordCredentials();
credential.setUsername(username); credential.setUsername(username);
credential.setPassword(new Password(password.toCharArray())); credential.setPassword(new Password(password.toCharArray()));
@ -64,12 +71,16 @@ public class PicketlinkAuthenticationProvider implements AuthenticationProvider
} else { } else {
return AuthProviderStatus.INVALID_CREDENTIALS; return AuthProviderStatus.INVALID_CREDENTIALS;
} }
} catch (IdentityManagementException ie) {
throw convertIDMException(ie);
}
} }
@Override @Override
public boolean updateCredential(RealmModel realm, Map<String, String> configuration, String username, String password) throws AuthenticationProviderException { public boolean updateCredential(RealmModel realm, Map<String, String> configuration, String username, String password) throws AuthenticationProviderException {
IdentityManager identityManager = getIdentityManager(realm); IdentityManager identityManager = getIdentityManager(realm);
try {
User picketlinkUser = BasicModel.getUser(identityManager, username); User picketlinkUser = BasicModel.getUser(identityManager, username);
if (picketlinkUser == null) { if (picketlinkUser == null) {
logger.debugf("User '%s' doesn't exists. Skip password update", username); logger.debugf("User '%s' doesn't exists. Skip password update", username);
@ -78,6 +89,9 @@ public class PicketlinkAuthenticationProvider implements AuthenticationProvider
identityManager.updateCredential(picketlinkUser, new Password(password.toCharArray())); identityManager.updateCredential(picketlinkUser, new Password(password.toCharArray()));
return true; return true;
} catch (IdentityManagementException ie) {
throw convertIDMException(ie);
}
} }
public IdentityManager getIdentityManager(RealmModel realm) throws AuthenticationProviderException { public IdentityManager getIdentityManager(RealmModel realm) throws AuthenticationProviderException {
@ -103,4 +117,14 @@ public class PicketlinkAuthenticationProvider implements AuthenticationProvider
} }
return identityManager; return identityManager;
} }
private AuthenticationProviderException convertIDMException(IdentityManagementException ie) {
Throwable realCause = ie;
while (realCause.getCause() != null) {
realCause = realCause.getCause();
}
// Use the message from the realCause
return new AuthenticationProviderException(realCause.getMessage(), ie);
}
} }