// 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.UiUserSignedInEvent;
import com.epam.ta.reportportal.dao.IntegrationRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
/*
 * 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;


/**
 * Dynamic (enableable) auth provider
 *
 * @author Andrei Varabyeu
 */
@Component
public abstract class EnableableAuthProvider implements AuthenticationProvider {

	protected final IntegrationRepository integrationRepository;
	protected final ApplicationEventPublisher eventPublisher;

	public EnableableAuthProvider(IntegrationRepository integrationRepository, ApplicationEventPublisher eventPublisher) {
		this.integrationRepository = integrationRepository;
		this.eventPublisher = eventPublisher;
	}

	protected abstract boolean isEnabled();

	protected abstract AuthenticationProvider getDelegate();

	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		if (isEnabled()) {
			Authentication auth = getDelegate().authenticate(authentication);
// 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/EnableableAuthProvider.java
import com.epam.reportportal.auth.event.UiUserSignedInEvent;
import com.epam.ta.reportportal.dao.IntegrationRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

/*
 * 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;


/**
 * Dynamic (enableable) auth provider
 *
 * @author Andrei Varabyeu
 */
@Component
public abstract class EnableableAuthProvider implements AuthenticationProvider {

	protected final IntegrationRepository integrationRepository;
	protected final ApplicationEventPublisher eventPublisher;

	public EnableableAuthProvider(IntegrationRepository integrationRepository, ApplicationEventPublisher eventPublisher) {
		this.integrationRepository = integrationRepository;
		this.eventPublisher = eventPublisher;
	}

	protected abstract boolean isEnabled();

	protected abstract AuthenticationProvider getDelegate();

	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		if (isEnabled()) {
			Authentication auth = getDelegate().authenticate(authentication);
			eventPublisher.publishEvent(new UiUserSignedInEvent(auth));