// Path: src/main/java/com/epam/reportportal/auth/event/UiAuthenticationFailureEventHandler.java
// @Component
// public class UiAuthenticationFailureEventHandler implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
// 
// 	private static final long MAXIMUM_SIZE = 5000;
// 	private static final long EXPIRATION_SECONDS = 30;
// 	private static final int MAX_ATTEMPTS = 3;
// 	private static final RequestHeaderRequestMatcher AJAX_REQUEST_MATCHER = new RequestHeaderRequestMatcher(HttpHeaders.X_REQUESTED_WITH,
// 			"XMLHttpRequest");
// 
// 	@Inject
// 	private Provider<HttpServletRequest> request;
// 
// 	private LoadingCache<String, AtomicInteger> failures;
// 
// 	public UiAuthenticationFailureEventHandler() {
// 		super();
// 		failures = CacheBuilder.newBuilder().maximumSize(MAXIMUM_SIZE).expireAfterWrite(EXPIRATION_SECONDS, TimeUnit.SECONDS)
// 				.build(new CacheLoader<String, AtomicInteger>() {
// 					@Override
// 					public AtomicInteger load(String key) {
// 						return new AtomicInteger(0);
// 					}
// 				});
// 	}
// 
// 	public boolean isBlocked(HttpServletRequest request) {
// 		AtomicInteger attempts = failures.getIfPresent(getClientIP(request));
// 		return null != attempts && attempts.get() > MAX_ATTEMPTS;
// 	}
// 
// 	private void onAjaxFailure(HttpServletRequest request) {
// 		String clientIP = getClientIP(request);
// 		failures.getUnchecked(clientIP).incrementAndGet();
// 
// 	}
// 
// 	private String getClientIP(HttpServletRequest request) {
// 		String xfHeader = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
// 		if (xfHeader == null) {
// 			return request.getRemoteAddr();
// 		}
// 		return xfHeader.split(",")[0];
// 	}
// 
// 	@Override
// 	public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
// 		onAjaxFailure(request.get());
// 	}
// }
// 
// Path: src/main/java/com/epam/reportportal/auth/event/UiUserSignedInEvent.java
// public class UiUserSignedInEvent extends AuthenticationSuccessEvent {
// 
// 	private static final long serialVersionUID = -6746135168882975399L;
// 
// 	public UiUserSignedInEvent(Authentication authentication) {
// 		super(authentication);
// 	}
// 
// }


import com.epam.reportportal.auth.event.UiAuthenticationFailureEventHandler;
import com.epam.reportportal.auth.event.UiUserSignedInEvent;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import javax.inject.Provider;
import javax.servlet.http.HttpServletRequest;
/*
 * Copyright 2019 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.epam.reportportal.auth.basic;



/**
 * Checks whether client have more auth errors than defined and throws exception if so
 *
 * @author <a href="mailto:andrei_varabyeu@epam.com">Andrei Varabyeu</a>
 */
public class BasicPasswordAuthenticationProvider extends DaoAuthenticationProvider {

	@Autowired
	private ApplicationEventPublisher eventPublisher;

	@Autowired
	private UiAuthenticationFailureEventHandler failureEventHandler;

	@Autowired
	private Provider<HttpServletRequest> request;

	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		boolean accountNonLocked = !failureEventHandler.isBlocked(request.get());
		if (!accountNonLocked) {
			throw new ReportPortalException(ErrorType.ADDRESS_LOCKED);
		}
		Authentication auth = super.authenticate(authentication);
// Path: src/main/java/com/epam/reportportal/auth/event/UiAuthenticationFailureEventHandler.java
// @Component
// public class UiAuthenticationFailureEventHandler implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
// 
// 	private static final long MAXIMUM_SIZE = 5000;
// 	private static final long EXPIRATION_SECONDS = 30;
// 	private static final int MAX_ATTEMPTS = 3;
// 	private static final RequestHeaderRequestMatcher AJAX_REQUEST_MATCHER = new RequestHeaderRequestMatcher(HttpHeaders.X_REQUESTED_WITH,
// 			"XMLHttpRequest");
// 
// 	@Inject
// 	private Provider<HttpServletRequest> request;
// 
// 	private LoadingCache<String, AtomicInteger> failures;
// 
// 	public UiAuthenticationFailureEventHandler() {
// 		super();
// 		failures = CacheBuilder.newBuilder().maximumSize(MAXIMUM_SIZE).expireAfterWrite(EXPIRATION_SECONDS, TimeUnit.SECONDS)
// 				.build(new CacheLoader<String, AtomicInteger>() {
// 					@Override
// 					public AtomicInteger load(String key) {
// 						return new AtomicInteger(0);
// 					}
// 				});
// 	}
// 
// 	public boolean isBlocked(HttpServletRequest request) {
// 		AtomicInteger attempts = failures.getIfPresent(getClientIP(request));
// 		return null != attempts && attempts.get() > MAX_ATTEMPTS;
// 	}
// 
// 	private void onAjaxFailure(HttpServletRequest request) {
// 		String clientIP = getClientIP(request);
// 		failures.getUnchecked(clientIP).incrementAndGet();
// 
// 	}
// 
// 	private String getClientIP(HttpServletRequest request) {
// 		String xfHeader = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
// 		if (xfHeader == null) {
// 			return request.getRemoteAddr();
// 		}
// 		return xfHeader.split(",")[0];
// 	}
// 
// 	@Override
// 	public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
// 		onAjaxFailure(request.get());
// 	}
// }
// 
// Path: src/main/java/com/epam/reportportal/auth/event/UiUserSignedInEvent.java
// public class UiUserSignedInEvent extends AuthenticationSuccessEvent {
// 
// 	private static final long serialVersionUID = -6746135168882975399L;
// 
// 	public UiUserSignedInEvent(Authentication authentication) {
// 		super(authentication);
// 	}
// 
// }


// Path: src/main/java/com/epam/reportportal/auth/basic/BasicPasswordAuthenticationProvider.java
import com.epam.reportportal.auth.event.UiAuthenticationFailureEventHandler;
import com.epam.reportportal.auth.event.UiUserSignedInEvent;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import javax.inject.Provider;
import javax.servlet.http.HttpServletRequest;

/*
 * Copyright 2019 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.epam.reportportal.auth.basic;



/**
 * Checks whether client have more auth errors than defined and throws exception if so
 *
 * @author <a href="mailto:andrei_varabyeu@epam.com">Andrei Varabyeu</a>
 */
public class BasicPasswordAuthenticationProvider extends DaoAuthenticationProvider {

	@Autowired
	private ApplicationEventPublisher eventPublisher;

	@Autowired
	private UiAuthenticationFailureEventHandler failureEventHandler;

	@Autowired
	private Provider<HttpServletRequest> request;

	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		boolean accountNonLocked = !failureEventHandler.isBlocked(request.get());
		if (!accountNonLocked) {
			throw new ReportPortalException(ErrorType.ADDRESS_LOCKED);
		}
		Authentication auth = super.authenticate(authentication);
		eventPublisher.publishEvent(new UiUserSignedInEvent(auth));