1 package org.apache.archiva.redback.authentication;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.redback.policy.AccountLockedException;
23 import org.apache.archiva.redback.policy.MustChangePasswordException;
24 import org.apache.archiva.redback.users.User;
25 import org.apache.archiva.redback.users.UserManager;
26 import org.apache.archiva.redback.users.UserManagerException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.stereotype.Service;
31
32 import javax.annotation.PostConstruct;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40
41
42
43
44
45
46
47
48
49
50
51 @Service("authenticationManager")
52 public class DefaultAuthenticationManager
53 implements AuthenticationManager
54 {
55
56 private Logger log = LoggerFactory.getLogger( getClass() );
57
58 private List<Authenticator> authenticators;
59
60 @Inject
61 private ApplicationContext applicationContext;
62
63 @Inject
64 @Named( value = "userManager#default" )
65 private UserManager userManager;
66
67 @SuppressWarnings( "unchecked" )
68 @PostConstruct
69 public void initialize()
70 {
71 this.authenticators =
72 new ArrayList<Authenticator>( applicationContext.getBeansOfType( Authenticator.class ).values() );
73 }
74
75
76 public String getId()
77 {
78 return "Default Authentication Manager - " + this.getClass().getName() + " : managed authenticators - " +
79 knownAuthenticators();
80 }
81
82 public AuthenticationResult authenticate( AuthenticationDataSource source )
83 throws AccountLockedException, AuthenticationException, MustChangePasswordException
84 {
85 if ( authenticators == null || authenticators.size() == 0 )
86 {
87 return ( new AuthenticationResult( false, null, new AuthenticationException(
88 "no valid authenticators, can't authenticate" ) ) );
89 }
90
91
92 List<AuthenticationFailureCause> authnResultErrors = new ArrayList<AuthenticationFailureCause>();
93 for ( Authenticator authenticator : authenticators )
94 {
95 if ( authenticator.supportsDataSource( source ) )
96 {
97 AuthenticationResult authResult = authenticator.authenticate( source );
98 List<AuthenticationFailureCause> authenticationFailureCauses =
99 authResult.getAuthenticationFailureCauses();
100
101 if ( authResult.isAuthenticated() )
102 {
103
104
105
106
107
108
109
110
111 for ( AuthenticationFailureCause authenticationFailureCause : authenticationFailureCauses )
112 {
113 User user = authenticationFailureCause.getUser();
114 if ( user != null )
115 {
116 if ( user.getCountFailedLoginAttempts() > 0 )
117 {
118 user.setCountFailedLoginAttempts( 0 );
119 if ( !userManager.isReadOnly() )
120 {
121 try
122 {
123 userManager.updateUser( user );
124 }
125 catch ( UserManagerException e )
126 {
127 log.debug( e.getMessage(), e );
128 log.warn( "skip error updating user: {}", e.getMessage() );
129 }
130 }
131 }
132 }
133 }
134 return authResult;
135 }
136
137 if ( authenticationFailureCauses != null )
138 {
139 authnResultErrors.addAll( authenticationFailureCauses );
140 }
141 else
142 {
143 if ( authResult.getException() != null )
144 {
145 authnResultErrors.add(
146 new AuthenticationFailureCause( AuthenticationConstants.AUTHN_RUNTIME_EXCEPTION,
147 authResult.getException().getMessage() ) );
148 }
149 }
150
151
152 }
153 }
154
155 return ( new AuthenticationResult( false, null, new AuthenticationException(
156 "authentication failed on authenticators: " + knownAuthenticators() ), authnResultErrors ) );
157 }
158
159 public List<Authenticator> getAuthenticators()
160 {
161 return authenticators;
162 }
163
164 private String knownAuthenticators()
165 {
166 StringBuilder strbuf = new StringBuilder();
167
168 for ( Authenticator authenticator : authenticators )
169 {
170 strbuf.append( '(' ).append( authenticator.getId() ).append( ") " );
171 }
172
173 return strbuf.toString();
174 }
175 }