1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.authn;
18
19
20 import javax.naming.NamingException;
21 import javax.naming.spi.InitialContextFactory;
22
23 import org.apache.ldap.server.DirectoryServiceConfiguration;
24 import org.apache.ldap.server.configuration.AuthenticatorConfiguration;
25 import org.apache.ldap.server.jndi.ServerContext;
26 import org.apache.ldap.common.aci.AuthenticationLevel;
27 import org.apache.ldap.common.name.LdapName;
28
29
30 /***
31 * Base class for all Authenticators.
32 *
33 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34 * @version $Rev: 328236 $, $Date: 2005-10-24 22:13:10 -0400 (Mon, 24 Oct 2005) $
35 */
36 public abstract class AbstractAuthenticator implements Authenticator
37 {
38 private DirectoryServiceConfiguration factoryCfg;
39 private AuthenticatorConfiguration cfg;
40
41 /*** authenticator type */
42 private String authenticatorType;
43
44
45 /***
46 * Creates a new instance.
47 *
48 * @param type the type of this authenticator (e.g. <tt>'simple'</tt>, <tt>'none'</tt>...)
49 */
50 protected AbstractAuthenticator( String type )
51 {
52 this.authenticatorType = type;
53 }
54
55
56 /***
57 * Returns {@link DirectoryServiceConfiguration} of {@link InitialContextFactory}
58 * which initialized this authenticator.
59 */
60 public DirectoryServiceConfiguration getFactoryConfiguration()
61 {
62 return factoryCfg;
63 }
64
65 /***
66 * Returns the configuration of this authenticator.
67 */
68 public AuthenticatorConfiguration getConfiguration()
69 {
70 return cfg;
71 }
72
73 public String getAuthenticatorType()
74 {
75 return authenticatorType;
76 }
77
78
79 /***
80 * Initializes default properties (<tt>factoryConfiguration</tt> and
81 * <tt>configuration</tt>, and calls {@link #doInit()} method.
82 * Please put your initialization code into {@link #doInit()}.
83 */
84 public final void init( DirectoryServiceConfiguration factoryCfg, AuthenticatorConfiguration cfg ) throws NamingException
85 {
86 this.factoryCfg = factoryCfg;
87 this.cfg = cfg;
88 doInit();
89 }
90
91
92 /***
93 * Implement your initialization code here.
94 */
95 protected void doInit() throws NamingException
96 {
97 }
98
99 /***
100 * Calls {@link #doDestroy()} method, and clears default properties
101 * (<tt>factoryConfiguration</tt> and <tt>configuration</tt>).
102 * Please put your deinitialization code into {@link #doDestroy()}.
103 */
104 public final void destroy()
105 {
106 try
107 {
108 doDestroy();
109 }
110 finally
111 {
112 this.factoryCfg = null;
113 this.cfg = null;
114 }
115 }
116
117 /***
118 * Implement your deinitialization code here.
119 */
120 protected void doDestroy()
121 {
122 }
123
124 public abstract LdapPrincipal authenticate( ServerContext ctx ) throws NamingException;
125
126
127 /***
128 * Returns a new {@link LdapPrincipal} instance whose value is the specified
129 * <tt>name</tt>.
130 *
131 * @param name the distinguished name of the X.500 principal
132 * @param authenticationLeve
133 * @return the principal for the <tt>name</tt>
134 * @throws NamingException if there is a problem parsing <tt>name</tt>
135 */
136
137 protected static LdapPrincipal createLdapPrincipal( String name, AuthenticationLevel authenticationLeve ) throws NamingException
138 {
139 LdapName principalDn = new LdapName( name );
140 return new LdapPrincipal( principalDn, AuthenticationLevel.SIMPLE );
141 }
142 }