View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.authn;
18  
19  
20  import org.apache.ldap.server.jndi.ServerContext;
21  import org.apache.ldap.common.name.LdapName;
22  
23  import javax.naming.NamingException;
24  import java.util.Enumeration;
25  
26  
27  /***
28   * Base class for all Authenticators.
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   */
32  public abstract class AbstractAuthenticator implements Authenticator, AuthenticatorConfig
33  {
34  
35      /*** authenticator config */
36      public AuthenticatorConfig authenticatorConfig;
37  
38      /*** authenticator type */
39      public String authenticatorType;
40  
41  
42      /***
43       * Create a new AuthenticationService.
44       *
45       * @param type authenticator's type
46       */
47      public AbstractAuthenticator( String type )
48      {
49          this.authenticatorType = type;
50      }
51  
52  
53      /***
54       * Returns a reference to the AuthenticatorContext in which this authenticator is running.
55       */
56      public AuthenticatorContext getAuthenticatorContext()
57      {
58          return authenticatorConfig.getAuthenticatorContext();
59      }
60  
61  
62      /***
63       * Returns this authenticator's type.
64       */
65      public String getAuthenticatorType()
66      {
67          return authenticatorType;
68      }
69  
70  
71      /***
72       * Return this authenticator's AuthenticatorConfig object.
73       */
74      public AuthenticatorConfig getAuthenticatorConfig()
75      {
76          return authenticatorConfig;
77      }
78  
79  
80      /***
81       * Called by the server to indicate to an authenticator that the authenticator
82       * is being placed into service.
83       */
84      public void init( AuthenticatorConfig authenticatorConfig ) throws NamingException
85      {
86          this.authenticatorConfig = authenticatorConfig;
87  
88          init();
89      }
90  
91  
92      /***
93       * A convenience method which can be overridden so that there's no need to
94       * call super.init( authenticatorConfig ).
95       */
96      public void init() throws NamingException
97      {
98      }
99  
100 
101     /***
102      * Perform the authentication operation and return the authorization id if successfull.
103      */
104     public abstract LdapPrincipal authenticate( ServerContext ctx ) throws NamingException;
105 
106 
107     /***
108      * Returns the name of this authenticator instance.
109      */
110     public String getAuthenticatorName()
111     {
112         return authenticatorConfig.getAuthenticatorName();
113     }
114 
115 
116     /***
117      * Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
118      */
119     public String getInitParameter( String name )
120     {
121         return authenticatorConfig.getInitParameter( name );
122     }
123 
124 
125     /***
126      * Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
127      */
128     public Enumeration getInitParameterNames()
129     {
130         return authenticatorConfig.getInitParameterNames();
131     }
132 
133 
134     /***
135      * Allows a means to create an LDAP principal without exposing LdapPrincipal creation
136      * to the rest of the world.
137      *
138      * @param dn the distinguished name of the X.500 principal
139      * @return the principal for the dn
140      * @throws NamingException if there is a problem parsing the dn
141      */
142     protected LdapPrincipal createLdapPrincipal( String dn ) throws NamingException
143     {
144         LdapName principalDn = new LdapName( dn );
145 
146         return new LdapPrincipal( principalDn );
147     }
148 
149 
150 }