Skip to content

Commit

Permalink
[FIXED JENKINS-11948] report all the causes, not just one.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Dec 16, 2011
1 parent 2e285cc commit b73fbb9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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)
Expand Down
@@ -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.