Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ArchivaUserManagerAuthenticator.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package org.apache.archiva.web.security;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
  22. import org.apache.archiva.redback.authentication.AbstractAuthenticator;
  23. import org.apache.archiva.redback.authentication.AuthenticationConstants;
  24. import org.apache.archiva.redback.authentication.AuthenticationDataSource;
  25. import org.apache.archiva.redback.authentication.AuthenticationException;
  26. import org.apache.archiva.redback.authentication.AuthenticationFailureCause;
  27. import org.apache.archiva.redback.authentication.AuthenticationResult;
  28. import org.apache.archiva.redback.authentication.Authenticator;
  29. import org.apache.archiva.redback.authentication.PasswordBasedAuthenticationDataSource;
  30. import org.apache.archiva.redback.policy.AccountLockedException;
  31. import org.apache.archiva.redback.policy.MustChangePasswordException;
  32. import org.apache.archiva.redback.policy.PasswordEncoder;
  33. import org.apache.archiva.redback.policy.UserSecurityPolicy;
  34. import org.apache.archiva.redback.users.User;
  35. import org.apache.archiva.redback.users.UserManager;
  36. import org.apache.archiva.redback.users.UserManagerException;
  37. import org.apache.archiva.redback.users.UserNotFoundException;
  38. import org.slf4j.Logger;
  39. import org.slf4j.LoggerFactory;
  40. import org.springframework.context.ApplicationContext;
  41. import org.springframework.stereotype.Service;
  42. import javax.annotation.PostConstruct;
  43. import javax.inject.Inject;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. /**
  47. * @author Olivier Lamy
  48. * @since 1.4-M4
  49. */
  50. @Service("authenticator#archiva")
  51. public class ArchivaUserManagerAuthenticator
  52. extends AbstractAuthenticator
  53. implements Authenticator
  54. {
  55. private Logger log = LoggerFactory.getLogger( getClass() );
  56. @Inject
  57. private UserSecurityPolicy securityPolicy;
  58. @Inject
  59. private ApplicationContext applicationContext;
  60. @Inject
  61. private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
  62. private List<UserManager> userManagers;
  63. @PostConstruct
  64. @Override
  65. public void initialize()
  66. throws AuthenticationException
  67. {
  68. try
  69. {
  70. List<String> userManagerImpls =
  71. redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
  72. userManagers = new ArrayList<>( userManagerImpls.size() );
  73. for ( String beanId : userManagerImpls )
  74. {
  75. userManagers.add( applicationContext.getBean( "userManager#" + beanId, UserManager.class ) );
  76. }
  77. }
  78. catch ( RepositoryAdminException e )
  79. {
  80. throw new AuthenticationException( e.getMessage(), e );
  81. }
  82. }
  83. public AuthenticationResult authenticate( AuthenticationDataSource ds )
  84. throws AuthenticationException, AccountLockedException, MustChangePasswordException
  85. {
  86. boolean authenticationSuccess = false;
  87. String username = null;
  88. Exception resultException = null;
  89. PasswordBasedAuthenticationDataSource source = (PasswordBasedAuthenticationDataSource) ds;
  90. List<AuthenticationFailureCause> authnResultErrors = new ArrayList<>();
  91. for ( UserManager userManager : userManagers )
  92. {
  93. try
  94. {
  95. log.debug( "Authenticate: {} with userManager: {}", source, userManager.getId() );
  96. User user = userManager.findUser( source.getUsername() );
  97. username = user.getUsername();
  98. if ( user.isLocked() )
  99. {
  100. //throw new AccountLockedException( "Account " + source.getUsername() + " is locked.", user );
  101. AccountLockedException e =
  102. new AccountLockedException( "Account " + source.getUsername() + " is locked.", user );
  103. log.warn( "{}", e.getMessage() );
  104. resultException = e;
  105. authnResultErrors.add(
  106. new AuthenticationFailureCause( AuthenticationConstants.AUTHN_LOCKED_USER_EXCEPTION,
  107. e.getMessage() ) );
  108. }
  109. if ( user.isPasswordChangeRequired() && source.isEnforcePasswordChange() )
  110. {
  111. //throw new MustChangePasswordException( "Password expired.", user );
  112. MustChangePasswordException e = new MustChangePasswordException( "Password expired.", user );
  113. log.warn( "{}", e.getMessage() );
  114. resultException = e;
  115. authnResultErrors.add(
  116. new AuthenticationFailureCause( AuthenticationConstants.AUTHN_MUST_CHANGE_PASSWORD_EXCEPTION,
  117. e.getMessage() ) );
  118. }
  119. PasswordEncoder encoder = securityPolicy.getPasswordEncoder();
  120. log.debug( "PasswordEncoder: {}", encoder.getClass().getName() );
  121. boolean isPasswordValid = encoder.isPasswordValid( user.getEncodedPassword(), source.getPassword() );
  122. if ( isPasswordValid )
  123. {
  124. log.debug( "User {} provided a valid password", source.getUsername() );
  125. try
  126. {
  127. securityPolicy.extensionPasswordExpiration( user );
  128. authenticationSuccess = true;
  129. //REDBACK-151 do not make unnessesary updates to the user object
  130. if ( user.getCountFailedLoginAttempts() > 0 )
  131. {
  132. user.setCountFailedLoginAttempts( 0 );
  133. if ( !userManager.isReadOnly() )
  134. {
  135. userManager.updateUser( user );
  136. }
  137. }
  138. return new AuthenticationResult( true, source.getUsername(), null );
  139. }
  140. catch ( MustChangePasswordException e )
  141. {
  142. user.setPasswordChangeRequired( true );
  143. //throw e;
  144. resultException = e;
  145. authnResultErrors.add( new AuthenticationFailureCause(
  146. AuthenticationConstants.AUTHN_MUST_CHANGE_PASSWORD_EXCEPTION, e.getMessage() ).user( user ) );
  147. }
  148. }
  149. else
  150. {
  151. log.warn( "Password is Invalid for user {} and userManager '{}'.", source.getUsername(),
  152. userManager.getId() );
  153. authnResultErrors.add( new AuthenticationFailureCause( AuthenticationConstants.AUTHN_NO_SUCH_USER,
  154. "Password is Invalid for user "
  155. + source.getUsername() + "." ).user( user ) );
  156. try
  157. {
  158. securityPolicy.extensionExcessiveLoginAttempts( user );
  159. }
  160. finally
  161. {
  162. if ( !userManager.isReadOnly() )
  163. {
  164. userManager.updateUser( user );
  165. }
  166. }
  167. //return new AuthenticationResult( false, source.getUsername(), null, authnResultExceptionsMap );
  168. }
  169. }
  170. catch ( UserNotFoundException e )
  171. {
  172. log.warn( "Login for user {} and userManager {} failed. user not found.", source.getUsername(),
  173. userManager.getId() );
  174. resultException = e;
  175. authnResultErrors.add( new AuthenticationFailureCause( AuthenticationConstants.AUTHN_NO_SUCH_USER,
  176. "Login for user " + source.getUsername()
  177. + " failed. user not found." ) );
  178. }
  179. catch ( Exception e )
  180. {
  181. log.warn( "Login for user {} and userManager {} failed, message: {}", source.getUsername(),
  182. userManager.getId(), e.getMessage() );
  183. resultException = e;
  184. authnResultErrors.add( new AuthenticationFailureCause( AuthenticationConstants.AUTHN_RUNTIME_EXCEPTION,
  185. "Login for user " + source.getUsername()
  186. + " failed, message: " + e.getMessage() ) );
  187. }
  188. }
  189. return new AuthenticationResult( authenticationSuccess, username, resultException, authnResultErrors );
  190. }
  191. public boolean supportsDataSource( AuthenticationDataSource source )
  192. {
  193. return ( source instanceof PasswordBasedAuthenticationDataSource );
  194. }
  195. public String getId()
  196. {
  197. return "ArchivaUserManagerAuthenticator";
  198. }
  199. }