private void authenticate() throws NamingException
{
// check if we are already authenticated and if so we return making
// sure first that the credentials are not exposed within context
ServerContext ctx =
( ServerContext ) InvocationStack.getInstance().peek().getCaller();
if ( ctx.getPrincipal() != null )
{
if ( ctx.getEnvironment().containsKey( Context.SECURITY_CREDENTIALS ) )
{
ctx.removeFromEnvironment( Context.SECURITY_CREDENTIALS );
}
return;
}
String authList = ( String ) ctx.getEnvironment().get( Context.SECURITY_AUTHENTICATION );
if ( authList == null )
{
if ( ctx.getEnvironment().containsKey( Context.SECURITY_CREDENTIALS ) )
{
// authentication type is simple here
authList = "simple";
}
else
{
// authentication type is anonymous
authList = "none";
}
}
authList = StringTools.deepTrim( authList );
String[] auth = authList.split( " " );
Collection authenticators = null;
// pick the first matching authenticator type
for ( int i=0; i<auth.length; i++)
{
authenticators = getAuthenticators( auth[i] );
if ( authenticators != null )
{
break;
}
}
if ( authenticators == null )
{
ctx.getEnvironment(); // shut's up idea's yellow light
ResultCodeEnum rc = ResultCodeEnum.AUTHMETHODNOTSUPPORTED;
throw new LdapAuthenticationNotSupportedException( rc );
}
// try each authenticators
for ( Iterator i = authenticators.iterator(); i.hasNext(); )
{
try
{
Authenticator authenticator = ( Authenticator ) i.next();
// perform the authentication
LdapPrincipal authorizationId = authenticator.authenticate( ctx );
// authentication was successful
ctx.setPrincipal( new TrustedPrincipalWrapper( authorizationId ) );
// remove creds so there is no security risk
ctx.removeFromEnvironment( Context.SECURITY_CREDENTIALS );
return;
}
catch ( LdapAuthenticationException e )
{
// authentication failed, try the next authenticator