View Javadoc

1   package com.atlassian.user.impl.delegation.security.authentication;
2   
3   import com.atlassian.user.EntityException;
4   import com.atlassian.user.UserManager;
5   import com.atlassian.user.impl.delegation.repository.DelegatingRepository;
6   import com.atlassian.user.repository.RepositoryIdentifier;
7   import com.atlassian.user.security.authentication.Authenticator;
8   import com.atlassian.util.profiling.UtilTimerStack;
9   import org.apache.log4j.Logger;
10  
11  import java.util.List;
12  import java.util.LinkedList;
13  import java.util.ArrayList;
14  
15  public class DelegatingAuthenticator implements Authenticator
16  {
17      private static final Logger log = Logger.getLogger(DelegatingAuthenticator.class);
18      private final List<Authenticator> authenticators;
19      private final UserManager userManager;
20  
21      public DelegatingAuthenticator(UserManager userManager, List<Authenticator> authenticators)
22      {
23          this.userManager = userManager;
24          this.authenticators = authenticators;
25      }
26  
27      public boolean authenticate(String username, String password) throws EntityException
28      {
29          if (UtilTimerStack.isActive())
30              UtilTimerStack.push(this.getClass().getName() + "_delegating_authenticate(" + username + ")");
31  
32          try
33          {
34              RepositoryIdentifier repository = userManager.getRepository(userManager.getUser(username));
35              List<Authenticator> authenticators = getAuthenticatorsForRepository(repository);
36  
37              if (authenticators.isEmpty())
38              {
39                  log.error("Failed to find authenticator for user " + username + " from repository " + repository);
40                  return false;
41              }
42  
43              for (Authenticator authenticator : authenticators)
44              {
45                  if (authenticate(authenticator, username, password))
46                      return true;
47              }
48          }
49          finally
50          {
51              if (UtilTimerStack.isActive())
52                  UtilTimerStack.pop(this.getClass().getName() + "_delegating_authenticate(" + username + ")");
53          }
54  
55          return false;
56      }
57  
58      private boolean authenticate(Authenticator authenticator, String username, String password)
59      {
60          try
61          {
62              return authenticator.authenticate(username, password);
63          }
64          catch (EntityException e)
65          {
66              log.error(authenticator.getRepository() + ": " + e.getMessage());
67              return false;
68          }
69      }
70  
71      private List<Authenticator> getAuthenticatorsForRepository(RepositoryIdentifier repository)
72      {
73          List<Authenticator> result = new LinkedList<Authenticator>();
74  
75          for (Authenticator authenticator : authenticators)
76          {
77              if (authenticator.getRepository().equals(repository))
78                  result.add(authenticator);
79          }
80          return result;
81      }
82  
83      public RepositoryIdentifier getRepository()
84      {
85          List<RepositoryIdentifier> repositories = new ArrayList<RepositoryIdentifier>(authenticators.size());
86  
87          for (Authenticator authenticator : authenticators)
88          {
89              repositories.add(authenticator.getRepository());
90          }
91  
92          return new DelegatingRepository(repositories);
93      }
94  
95      public List getAuthenticators()
96      {
97          return authenticators;
98      }
99  
100 }