1 package org.apache.archiva.redback.authentication;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.redback.policy.AccountLockedException;
23 import org.apache.archiva.redback.policy.MustChangePasswordException;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.stereotype.Service;
27 import javax.annotation.PostConstruct;
28 import javax.inject.Inject;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
36 * DefaultAuthenticationManager: the goal of the authentication manager is to act as a conduit for
37 * authentication requests into different authentication schemes
39 * For example, the default implementation can be configured with any number of authenticators and will
40 * sequentially try them for an authenticated result. This allows you to have the standard user/pass
41 * auth procedure followed by authentication based on a known key for 'remember me' type functionality.
43 * @author: Jesse McConnell <jesse@codehaus.org>
45 @Service("authenticationManager")
46 public class DefaultAuthenticationManager
47 implements AuthenticationManager
50 private List<Authenticator> authenticators;
53 private ApplicationContext applicationContext;
55 @SuppressWarnings("unchecked")
57 public void initialize()
60 new ArrayList<Authenticator>( applicationContext.getBeansOfType( Authenticator.class ).values() );
66 return "Default Authentication Manager - " + this.getClass().getName() + " : managed authenticators - " +
67 knownAuthenticators();
70 public AuthenticationResult authenticate( AuthenticationDataSource source )
71 throws AccountLockedException, AuthenticationException, MustChangePasswordException
73 if ( authenticators == null || authenticators.size() == 0 )
75 return ( new AuthenticationResult( false, null, new AuthenticationException(
76 "no valid authenticators, can't authenticate" ) ) );
79 // put AuthenticationResult exceptions in a map
80 List<AuthenticationFailureCause> authnResultErrors = new ArrayList<AuthenticationFailureCause>();
81 for ( Authenticator authenticator : authenticators )
83 if ( authenticator.supportsDataSource( source ) )
85 AuthenticationResult authResult = authenticator.authenticate( source );
86 List<AuthenticationFailureCause> authenticationFailureCauses =
87 authResult.getAuthenticationFailureCauses();
89 if ( authResult.isAuthenticated() )
94 if ( authenticationFailureCauses != null )
96 authnResultErrors.addAll( authenticationFailureCauses );
100 if ( authResult.getException() != null )
102 authnResultErrors.add(
103 new AuthenticationFailureCause( AuthenticationConstants.AUTHN_RUNTIME_EXCEPTION,
104 authResult.getException().getMessage() ) );
112 return ( new AuthenticationResult( false, null, new AuthenticationException(
113 "authentication failed on authenticators: " + knownAuthenticators() ), authnResultErrors ) );
116 public List<Authenticator> getAuthenticators()
118 return authenticators;
121 private String knownAuthenticators()
123 StringBuilder strbuf = new StringBuilder();
125 for ( Authenticator authenticator : authenticators )
127 strbuf.append( '(' ).append( authenticator.getId() ).append( ") " );
130 return strbuf.toString();