1 17 package org.apache.ldap.server.authn; 18 19 20 import org.apache.ldap.common.exception.LdapAuthenticationException; 21 import org.apache.ldap.common.exception.LdapAuthenticationNotSupportedException; 22 import org.apache.ldap.common.message.ResultCodeEnum; 23 import org.apache.ldap.common.util.StringTools; 24 import org.apache.ldap.server.interceptor.Interceptor; 25 import org.apache.ldap.server.interceptor.InterceptorContext; 26 import org.apache.ldap.server.interceptor.NextInterceptor; 27 import org.apache.ldap.server.invocation.Invocation; 28 import org.apache.ldap.server.jndi.EnvKeys; 29 import org.apache.ldap.server.jndi.ServerContext; 30 import org.apache.ldap.server.jndi.ServerLdapContext; 31 32 import javax.naming.Context ; 33 import javax.naming.NamingException ; 34 import java.lang.reflect.Constructor ; 35 import java.util.*; 36 37 38 46 public class AuthenticationService implements Interceptor 47 { 48 49 private static final String AUTH_TYPE = Context.SECURITY_AUTHENTICATION; 50 51 52 private static final String CREDS = Context.SECURITY_CREDENTIALS; 53 54 55 public Map authenticators = new LinkedHashMap(); 56 57 58 61 public AuthenticationService() 62 { 63 } 64 65 public void init( InterceptorContext ctx ) throws NamingException 66 { 67 71 boolean allowAnonymous = !ctx.getEnvironment().containsKey( EnvKeys.DISABLE_ANONYMOUS ); 72 73 75 GenericAuthenticatorContext authenticatorContext = new GenericAuthenticatorContext(); 76 77 authenticatorContext.setPartitionNexus( ctx.getRootNexus() ); 78 79 authenticatorContext.setAllowAnonymous( allowAnonymous ); 80 81 try { 83 85 GenericAuthenticatorConfig authenticatorConfig = new GenericAuthenticatorConfig(); 86 87 authenticatorConfig.setAuthenticatorName( "none" ); 88 89 authenticatorConfig.setAuthenticatorContext( authenticatorContext ); 90 91 org.apache.ldap.server.authn.Authenticator authenticator = new AnonymousAuthenticator(); 92 93 authenticator.init( authenticatorConfig ); 94 95 this.register( authenticator ); 96 97 authenticatorConfig = new GenericAuthenticatorConfig(); 99 100 authenticatorConfig.setAuthenticatorName( "simple" ); 101 102 authenticatorConfig.setAuthenticatorContext( authenticatorContext ); 103 104 authenticator = new SimpleAuthenticator(); 105 106 authenticator.init( authenticatorConfig ); 107 108 this.register( authenticator ); 109 } 110 catch ( Exception e ) 111 { 112 throw new NamingException ( e.getMessage() ); 113 } 114 115 GenericAuthenticatorConfig[] configs = null; 116 117 configs = AuthenticatorConfigBuilder.getAuthenticatorConfigs( new Hashtable( ctx.getEnvironment() ) ); 118 119 for ( int ii = 0; ii < configs.length; ii++ ) 120 { 121 try 122 { 123 configs[ii].setAuthenticatorContext( authenticatorContext ); 124 125 String authenticatorClass = configs[ii].getAuthenticatorClass(); 126 127 Class clazz = Class.forName( authenticatorClass ); 128 129 Constructor constructor = clazz.getConstructor( new Class [] { } ); 130 131 AbstractAuthenticator authenticator = ( AbstractAuthenticator ) constructor.newInstance( new Object [] { } ); 132 133 authenticator.init( configs[ii] ); 134 135 this.register( authenticator ); 136 } 137 catch ( Exception e ) 138 { 139 e.printStackTrace(); 140 } 141 } 142 143 } 144 145 public void destroy() 146 { 147 authenticators.clear(); 148 } 149 150 158 public void register( org.apache.ldap.server.authn.Authenticator authenticator ) 159 { 160 Collection authenticatorList = getAuthenticators( authenticator.getAuthenticatorType() ); 161 162 if ( authenticatorList == null ) 163 { 164 authenticatorList = new ArrayList(); 165 166 authenticators.put( authenticator.getAuthenticatorType(), authenticatorList ); 167 } 168 169 authenticatorList.add( authenticator ); 170 } 171 172 181 public void unregister( org.apache.ldap.server.authn.Authenticator authenticator ) 182 { 183 Collection authenticatorList = getAuthenticators( authenticator.getAuthenticatorType() ); 184 185 if ( authenticatorList == null ) 186 { 187 return; 188 } 189 190 authenticatorList.remove( authenticator ); 191 } 192 193 199 public Collection getAuthenticators( String type ) 200 { 201 return ( Collection ) authenticators.get( type ); 202 } 203 204 public void process( NextInterceptor nextProcessor, Invocation call ) throws NamingException 205 { 206 ServerContext ctx = ( ServerLdapContext ) call.getContextStack().peek(); 209 210 if ( ctx.getPrincipal() != null ) 211 { 212 if ( ctx.getEnvironment().containsKey( CREDS ) ) 213 { 214 ctx.removeFromEnvironment( CREDS ); 215 } 216 217 nextProcessor.process(call); 218 219 return; 220 } 221 222 String authList = ( String ) ctx.getEnvironment().get( AUTH_TYPE ); 223 224 if ( authList == null ) 225 { 226 if ( ctx.getEnvironment().containsKey( CREDS ) ) 227 { 228 230 authList = "simple"; 231 } 232 else 233 { 234 236 authList = "none"; 237 } 238 239 } 240 241 authList = StringTools.deepTrim( authList ); 242 243 String [] auth = authList.split( " " ); 244 245 Collection authenticators = null; 246 247 249 for ( int i=0; i<auth.length; i++) 250 { 251 authenticators = getAuthenticators( auth[i] ); 252 253 if ( authenticators != null ) 254 { 255 break; 256 } 257 } 258 259 if ( authenticators == null ) 260 { 261 ctx.getEnvironment(); 263 ResultCodeEnum rc = ResultCodeEnum.AUTHMETHODNOTSUPPORTED; 264 265 throw new LdapAuthenticationNotSupportedException( rc ); 266 } 267 268 for ( Iterator i = authenticators.iterator(); i.hasNext(); ) 270 { 271 try 272 { 273 Authenticator authenticator = ( Authenticator ) i.next(); 274 275 277 LdapPrincipal authorizationId = authenticator.authenticate( ctx ); 278 279 281 ctx.setPrincipal( new TrustedPrincipalWrapper( authorizationId ) ); 282 283 285 ctx.removeFromEnvironment( CREDS ); 286 287 nextProcessor.process(call); 288 289 return; 290 } 291 catch ( LdapAuthenticationException e ) 292 { 293 } 295 } 296 297 throw new LdapAuthenticationException(); 298 } 299 300 301 312 public final class TrustedPrincipalWrapper 313 { 314 315 private final LdapPrincipal principal; 316 317 318 323 private TrustedPrincipalWrapper( LdapPrincipal principal ) 324 { 325 this.principal = principal; 326 } 327 328 329 334 public LdapPrincipal getPrincipal() 335 { 336 return principal; 337 } 338 } 339 } 340 | Popular Tags |