Return oauth1 providers on registered oauth providers list request

pull/624/head
ivinokur 2023-12-11 09:16:11 +02:00
parent ebca10356e
commit 7b86990011
8 changed files with 82 additions and 11 deletions

View File

@ -90,7 +90,10 @@ public class KubernetesAuthorisationRequestManager implements AuthorisationReque
Map<String, List<String>> params = getQueryParametersFromState(getState(requestUrl));
errorValues = errorValues == null ? uriInfo.getQueryParameters().get("error") : errorValues;
if (errorValues != null && errorValues.contains("access_denied")) {
store(getParameter(params, "oauth_provider"));
String oauthProvider = getParameter(params, "oauth_provider");
if (!isNullOrEmpty(oauthProvider)) {
store(oauthProvider);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2023 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@ -21,6 +21,7 @@ import com.google.inject.Singleton;
@Singleton
public class BitbucketServerOAuthAuthenticator extends OAuthAuthenticator {
public static final String AUTHENTICATOR_NAME = "bitbucket-server";
private final String bitbucketEndpoint;
private final String apiEndpoint;
public BitbucketServerOAuthAuthenticator(
@ -33,6 +34,7 @@ public class BitbucketServerOAuthAuthenticator extends OAuthAuthenticator {
apiEndpoint + "/oauth/1.0/callback",
null,
privateKey);
this.bitbucketEndpoint = bitbucketEndpoint;
this.apiEndpoint = apiEndpoint;
}
@ -48,4 +50,9 @@ public class BitbucketServerOAuthAuthenticator extends OAuthAuthenticator {
+ AUTHENTICATOR_NAME
+ "&request_method=POST&signature_method=rsa";
}
@Override
public String getEndpointUrl() {
return bitbucketEndpoint;
}
}

View File

@ -51,4 +51,9 @@ public class NoopOAuthAuthenticator extends OAuthAuthenticator {
public String getLocalAuthenticateUrl() {
return "Noop URL";
}
@Override
public String getEndpointUrl() {
return "Noop URL";
}
}

View File

@ -60,7 +60,8 @@ public class EmbeddedOAuthAPI implements OAuthAPI {
@Named("che.auth.access_denied_error_page")
protected String errorPage;
@Inject protected OAuthAuthenticatorProvider providers;
@Inject protected OAuthAuthenticatorProvider oauth2Providers;
@Inject protected org.eclipse.che.security.oauth1.OAuthAuthenticatorProvider oauth1Providers;
private String redirectAfterLogin;
@Override
@ -126,7 +127,10 @@ public class EmbeddedOAuthAPI implements OAuthAPI {
Set<OAuthAuthenticatorDescriptor> result = new HashSet<>();
final UriBuilder uriBuilder =
uriInfo.getBaseUriBuilder().clone().path(OAuthAuthenticationService.class);
for (String name : providers.getRegisteredProviderNames()) {
Set<String> registeredProviderNames =
new HashSet<>(oauth2Providers.getRegisteredProviderNames());
registeredProviderNames.addAll(oauth1Providers.getRegisteredProviderNames());
for (String name : registeredProviderNames) {
final List<Link> links = new LinkedList<>();
links.add(
LinksHelper.createLink(
@ -147,11 +151,14 @@ public class EmbeddedOAuthAPI implements OAuthAPI {
.withName("mode")
.withRequired(true)
.withDefaultValue("federated_login")));
OAuthAuthenticator authenticator = providers.getAuthenticator(name);
OAuthAuthenticator authenticator = oauth2Providers.getAuthenticator(name);
result.add(
newDto(OAuthAuthenticatorDescriptor.class)
.withName(name)
.withEndpointUrl(authenticator.getEndpointUrl())
.withEndpointUrl(
authenticator != null
? authenticator.getEndpointUrl()
: oauth1Providers.getAuthenticator(name).getEndpointUrl())
.withLinks(links));
}
return result;
@ -193,7 +200,7 @@ public class EmbeddedOAuthAPI implements OAuthAPI {
}
protected OAuthAuthenticator getAuthenticator(String oauthProviderName) throws NotFoundException {
OAuthAuthenticator oauth = providers.getAuthenticator(oauthProviderName);
OAuthAuthenticator oauth = oauth2Providers.getAuthenticator(oauthProviderName);
if (oauth == null) {
LOG.warn("Unsupported OAuth provider {} ", oauthProviderName);
throw new NotFoundException("Unsupported OAuth provider " + oauthProviderName);

View File

@ -243,6 +243,13 @@ public abstract class OAuthAuthenticator {
*/
public abstract String getLocalAuthenticateUrl();
/**
* Get endpoint URL.
*
* @return provider's endpoint URL
*/
public abstract String getEndpointUrl();
/**
* Compute the Authorization header to sign the OAuth 1 request.
*

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* Copyright (c) 2012-2023 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@ -11,6 +11,8 @@
*/
package org.eclipse.che.security.oauth1;
import static java.util.stream.Collectors.toUnmodifiableSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@ -44,4 +46,15 @@ public class OAuthAuthenticatorProvider {
public OAuthAuthenticator getAuthenticator(String oauthProviderName) {
return oAuthAuthenticators.get(oauthProviderName);
}
/**
* Gets registered OAuth1 provider names
*
* @return set of registered OAuth1 provider names
*/
public Set<String> getRegisteredProviderNames() {
return oAuthAuthenticators.keySet().stream()
.filter(key -> !"Noop".equals(key))
.collect(toUnmodifiableSet());
}
}

View File

@ -20,11 +20,14 @@ import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.UriInfo;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Set;
import org.eclipse.che.api.auth.shared.dto.OAuthToken;
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.security.oauth.shared.dto.OAuthAuthenticatorDescriptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
@ -35,7 +38,8 @@ import org.testng.annotations.Test;
@Listeners(value = MockitoTestNGListener.class)
public class EmbeddedOAuthAPITest {
@Mock OAuthAuthenticatorProvider providers;
@Mock OAuthAuthenticatorProvider oauth2Providers;
@Mock org.eclipse.che.security.oauth1.OAuthAuthenticatorProvider oauth1Providers;
@InjectMocks EmbeddedOAuthAPI embeddedOAuthAPI;
@ -51,7 +55,7 @@ public class EmbeddedOAuthAPITest {
String provider = "myprovider";
String token = "token123";
OAuthAuthenticator authenticator = mock(OAuthAuthenticator.class);
when(providers.getAuthenticator(eq(provider))).thenReturn(authenticator);
when(oauth2Providers.getAuthenticator(eq(provider))).thenReturn(authenticator);
when(authenticator.getToken(anyString())).thenReturn(newDto(OAuthToken.class).withToken(token));
@ -60,6 +64,26 @@ public class EmbeddedOAuthAPITest {
assertEquals(result.getToken(), token);
}
@Test
public void shouldGetRegisteredAuthenticators() throws Exception {
// given
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getBaseUriBuilder()).thenReturn(UriBuilder.fromUri("http://eclipse.che"));
when(oauth2Providers.getRegisteredProviderNames()).thenReturn(Set.of("github"));
when(oauth1Providers.getRegisteredProviderNames()).thenReturn(Set.of("bitbucket"));
org.eclipse.che.security.oauth1.OAuthAuthenticator authenticator =
mock(org.eclipse.che.security.oauth1.OAuthAuthenticator.class);
when(oauth2Providers.getAuthenticator("github")).thenReturn(mock(OAuthAuthenticator.class));
when(oauth1Providers.getAuthenticator("bitbucket")).thenReturn(authenticator);
// when
Set<OAuthAuthenticatorDescriptor> registeredAuthenticators =
embeddedOAuthAPI.getRegisteredAuthenticators(uriInfo);
// then
assertEquals(registeredAuthenticators.size(), 2);
}
@Test
public void shouldEncodeRejectErrorForRedirectUrl() throws Exception {
// given

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2023 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@ -62,6 +62,11 @@ public class OAuthAuthenticatorTest {
public String getLocalAuthenticateUrl() {
return null;
}
@Override
public String getEndpointUrl() {
return null;
}
};
}