// Path: src/main/java/com/epam/reportportal/auth/integration/converter/OAuthRegistrationConverters.java
// public static final BiFunction<OAuthRegistrationResource, ClientRegistration, OAuthRegistration> FROM_SPRING_MERGE = (registrationResource, clientResource) -> {
// 	OAuthRegistration registration = new OAuthRegistration();
// 	registration.setId(clientResource.getRegistrationId());
// 	registration.setClientId(registrationResource.getClientId());
// 	registration.setClientSecret(registrationResource.getClientSecret());
// 	registration.setClientAuthMethod(ofNullable(registrationResource.getClientAuthMethod()).orElseGet(() -> clientResource.getClientAuthenticationMethod()
// 			.getValue()));
// 	registration.setClientName(ofNullable(registrationResource.getClientName()).orElseGet(clientResource::getClientName));
// 	registration.setAuthGrantType(ofNullable(registrationResource.getAuthGrantType()).orElseGet(() -> clientResource.getAuthorizationGrantType()
// 			.getValue()));
// 	registration.setRedirectUrlTemplate(ofNullable(registrationResource.getRedirectUrlTemplate()).orElseGet(clientResource::getRedirectUriTemplate));
// 	registration.setScopes(ofNullable(registrationResource.getScopes()).map(scopes -> scopes.stream()
// 			.map(SCOPE_FROM_RESOURCE)
// 			.peek(registrationScope -> registrationScope.setRegistration(registration))
// 			.collect(Collectors.toSet()))
// 			.orElse(clientResource.getScopes()
// 					.stream()
// 					.map(SCOPE_FROM_RESOURCE)
// 					.peek(registrationScope -> registrationScope.setRegistration(registration))
// 					.collect(Collectors.toSet())));
// 
// 	List<OAuthRegistrationRestriction> registrationRestrictions = OAuthRestrictionConverter.FROM_RESOURCE.apply(registrationResource);
// 	registration.setRestrictions(registrationRestrictions.stream()
// 			.peek(restriction -> restriction.setRegistration(registration))
// 			.collect(Collectors.toSet()));
// 
// 	ClientRegistration.ProviderDetails details = clientResource.getProviderDetails();
// 	registration.setAuthorizationUri(ofNullable(registrationResource.getAuthorizationUri()).orElseGet(details::getAuthorizationUri));
// 	registration.setTokenUri(ofNullable(registrationResource.getTokenUri()).orElseGet(details::getTokenUri));
// 	registration.setUserInfoEndpointUri(ofNullable(registrationResource.getUserInfoEndpointUri()).orElseGet(() -> details.getUserInfoEndpoint()
// 			.getUri()));
// 	registration.setUserInfoEndpointNameAttribute(ofNullable(registrationResource.getUserInfoEndpointNameAttribute()).orElseGet(() -> details
// 			.getUserInfoEndpoint()
// 			.getUserNameAttributeName()));
// 	registration.setJwkSetUri(ofNullable(registrationResource.getJwkSetUri()).orElseGet(details::getJwkSetUri));
// 
// 	return registration;
// };


import com.epam.ta.reportportal.entity.oauth.OAuthRegistration;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource;
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import static com.epam.reportportal.auth.integration.converter.OAuthRegistrationConverters.FROM_SPRING_MERGE;
/*
 * 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.oauth;



public class OAuthProviderFactory {
	public static OAuthRegistration fillOAuthRegistration(String oauthProviderId, OAuthRegistrationResource registrationResource) {

		switch (oauthProviderId) {
			case "github":
				ClientRegistration springRegistration = createGitHubProvider(oauthProviderId, registrationResource);
// Path: src/main/java/com/epam/reportportal/auth/integration/converter/OAuthRegistrationConverters.java
// public static final BiFunction<OAuthRegistrationResource, ClientRegistration, OAuthRegistration> FROM_SPRING_MERGE = (registrationResource, clientResource) -> {
// 	OAuthRegistration registration = new OAuthRegistration();
// 	registration.setId(clientResource.getRegistrationId());
// 	registration.setClientId(registrationResource.getClientId());
// 	registration.setClientSecret(registrationResource.getClientSecret());
// 	registration.setClientAuthMethod(ofNullable(registrationResource.getClientAuthMethod()).orElseGet(() -> clientResource.getClientAuthenticationMethod()
// 			.getValue()));
// 	registration.setClientName(ofNullable(registrationResource.getClientName()).orElseGet(clientResource::getClientName));
// 	registration.setAuthGrantType(ofNullable(registrationResource.getAuthGrantType()).orElseGet(() -> clientResource.getAuthorizationGrantType()
// 			.getValue()));
// 	registration.setRedirectUrlTemplate(ofNullable(registrationResource.getRedirectUrlTemplate()).orElseGet(clientResource::getRedirectUriTemplate));
// 	registration.setScopes(ofNullable(registrationResource.getScopes()).map(scopes -> scopes.stream()
// 			.map(SCOPE_FROM_RESOURCE)
// 			.peek(registrationScope -> registrationScope.setRegistration(registration))
// 			.collect(Collectors.toSet()))
// 			.orElse(clientResource.getScopes()
// 					.stream()
// 					.map(SCOPE_FROM_RESOURCE)
// 					.peek(registrationScope -> registrationScope.setRegistration(registration))
// 					.collect(Collectors.toSet())));
// 
// 	List<OAuthRegistrationRestriction> registrationRestrictions = OAuthRestrictionConverter.FROM_RESOURCE.apply(registrationResource);
// 	registration.setRestrictions(registrationRestrictions.stream()
// 			.peek(restriction -> restriction.setRegistration(registration))
// 			.collect(Collectors.toSet()));
// 
// 	ClientRegistration.ProviderDetails details = clientResource.getProviderDetails();
// 	registration.setAuthorizationUri(ofNullable(registrationResource.getAuthorizationUri()).orElseGet(details::getAuthorizationUri));
// 	registration.setTokenUri(ofNullable(registrationResource.getTokenUri()).orElseGet(details::getTokenUri));
// 	registration.setUserInfoEndpointUri(ofNullable(registrationResource.getUserInfoEndpointUri()).orElseGet(() -> details.getUserInfoEndpoint()
// 			.getUri()));
// 	registration.setUserInfoEndpointNameAttribute(ofNullable(registrationResource.getUserInfoEndpointNameAttribute()).orElseGet(() -> details
// 			.getUserInfoEndpoint()
// 			.getUserNameAttributeName()));
// 	registration.setJwkSetUri(ofNullable(registrationResource.getJwkSetUri()).orElseGet(details::getJwkSetUri));
// 
// 	return registration;
// };


// Path: src/main/java/com/epam/reportportal/auth/oauth/OAuthProviderFactory.java
import com.epam.ta.reportportal.entity.oauth.OAuthRegistration;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource;
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import static com.epam.reportportal.auth.integration.converter.OAuthRegistrationConverters.FROM_SPRING_MERGE;

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



public class OAuthProviderFactory {
	public static OAuthRegistration fillOAuthRegistration(String oauthProviderId, OAuthRegistrationResource registrationResource) {

		switch (oauthProviderId) {
			case "github":
				ClientRegistration springRegistration = createGitHubProvider(oauthProviderId, registrationResource);
				return FROM_SPRING_MERGE.apply(registrationResource, springRegistration);