View Javadoc

1   package com.atlassian.user.impl.osuser.security.authentication;
2   
3   import com.atlassian.user.EntityException;
4   import com.atlassian.user.repository.RepositoryIdentifier;
5   import com.atlassian.user.security.authentication.Authenticator;
6   import com.atlassian.user.security.authentication.EntityAuthenticationException;
7   import com.opensymphony.user.authenticator.AuthenticationException;
8   import com.opensymphony.user.provider.CredentialsProvider;
9   
10  /**
11   * OSUser has two classes which handle authentication - the {@link CredentialsProvider} and
12   * the {@link com.opensymphony.user.authenticator.Authenticator}. This Authenticator impl.
13   * has the capability to wrap one of either.
14   */
15  public class OSUAuthenticator implements Authenticator
16  {
17      private final RepositoryIdentifier repository;
18      private final CredentialsProvider credentialsProvider;
19      private final com.opensymphony.user.authenticator.Authenticator osuserAuthenticator;
20  
21      public OSUAuthenticator(RepositoryIdentifier repository, CredentialsProvider credentialsProvider)
22      {
23          this(repository, credentialsProvider, null);
24      }
25  
26      public OSUAuthenticator(RepositoryIdentifier repository, com.opensymphony.user.authenticator.Authenticator osuserAuthenticator)
27      {
28          this(repository, null, osuserAuthenticator);
29      }
30  
31      private OSUAuthenticator(RepositoryIdentifier repository, CredentialsProvider credentialsProvider,
32          com.opensymphony.user.authenticator.Authenticator osuserAuthenticator)
33      {
34          this.repository = repository;
35          this.credentialsProvider = credentialsProvider;
36          this.osuserAuthenticator = osuserAuthenticator;
37      }
38  
39      public boolean authenticate(String username, String password) throws EntityException
40      {
41          if (isWrappingCredentialProvider())
42          {
43              return credentialsProvider.authenticate(username, password);
44          }
45  
46          try
47          {
48              return osuserAuthenticator.login(username, password);
49          }
50          catch (AuthenticationException e)
51          {
52              throw new EntityAuthenticationException(e);
53          }
54      }
55  
56      public RepositoryIdentifier getRepository()
57      {
58          return repository;
59      }
60  
61      /**
62       * @return true if an instance of {@link CredentialsProvider} has been wrapped, false otherwise.
63       */
64      public boolean isWrappingCredentialProvider()
65      {
66          return credentialsProvider != null;
67      }
68  }