Skip to content
Permalink
Browse files
[FIXED JENKINS-11948] report all the causes, not just one.
  • Loading branch information
kohsuke committed Dec 16, 2011
1 parent 2e285cc commit b73fbb9e6773214e02e068741b703d2c1fb33e4c
Showing 3 changed files with 53 additions and 5 deletions.
@@ -58,22 +58,24 @@ public ActiveDirectoryUnixAuthenticationProvider(ActiveDirectorySecurityRealm re

protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
UserDetails userDetails = null;
BadCredentialsException e = null;
List<BadCredentialsException> causes = new ArrayList<BadCredentialsException>();
for (String domainName : domainNames) {
try {
userDetails = retrieveUser(username, authentication, domainName);
} catch (BadCredentialsException bce) {
LOGGER.log(Level.WARNING, "Credential exception tying to authenticate against "+domainName+" domain", bce);
e = bce;
causes.add(bce);
}
if (userDetails!=null) {
break;
}
}
if (userDetails==null) {
LOGGER.log(Level.WARNING, "Exhausted all configured domains and could not authenticate against any.");
if (e!=null) throw e;
throw new BadCredentialsException("Either no such user '"+username+"' or incorrect password");
if (causes.isEmpty())
throw new BadCredentialsException("Either no such user '"+username+"' or incorrect password");
else
throw new MultiCauseBadCredentialsException("Either no such user '"+username+"' or incorrect password",causes);
}
return userDetails;
}
@@ -63,8 +63,10 @@ public hudson.model.User getJenkinsUser() {
* @return this
*/
public UserDetails updateUserInfo() {
// the challenge here is to set the name if it's not set, but if the user overrides that
//
hudson.model.User u = getJenkinsUser();
if (getDisplayName()!=null)
if (getDisplayName()!=null && u.getId().equals(u.getFullName()))
u.setFullName(getDisplayName());

if (getMail()!=null)
@@ -0,0 +1,44 @@
package hudson.plugins.active_directory;

import com.google.common.collect.ImmutableList;
import hudson.util.FlushProofOutputStream;
import org.acegisecurity.AuthenticationException;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;

/**
* {@link AuthenticationException} that supports multiple nested causes.
*
* @author Kohsuke Kawaguchi
*/
public class MultiCauseBadCredentialsException extends AuthenticationException {
private final List<Throwable> causes;

public MultiCauseBadCredentialsException(String msg, Collection<? extends Throwable> causes) {
super(msg);
this.causes = ImmutableList.copyOf(causes);
}

@Override
public void printStackTrace(PrintStream s) {
PrintWriter w = new PrintWriter(new FlushProofOutputStream(s));
printStackTrace(w);
w.flush();
}

@Override
public void printStackTrace(PrintWriter s) {
synchronized (s) {
super.printStackTrace(s);

for (int i = 0; i < causes.size(); i++) {
Throwable cause = causes.get(i);
s.format("Cause #%s: ", i + 1);
cause.printStackTrace(s);
}
}
}
}

0 comments on commit b73fbb9

Please sign in to comment.