Skip to content

Commit

Permalink
Improving flow when user denies Bitbucket access to Che (#19620)
Browse files Browse the repository at this point in the history
* redirecting to dashboard after denying authorization

Signed-off-by: xbaran4 <pbaran@redhat.com>
  • Loading branch information
xbaran4 committed Apr 28, 2021
1 parent 6875bf9 commit f162778
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author Kevin Pollet
* @author Igor Vinokur
*/
public final class OAuthAuthenticationException extends ServerException {
public class OAuthAuthenticationException extends ServerException {

/**
* Constructs an instance of {@link OAuthAuthenticationException}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.eclipse.che.api.core.BadRequestException;
import org.eclipse.che.api.core.rest.Service;
import org.eclipse.che.commons.env.EnvironmentContext;
Expand All @@ -43,7 +44,7 @@ public class OAuthAuthenticationService extends Service {
private static final Logger LOG = LoggerFactory.getLogger(OAuthAuthenticationService.class);

private static final String UNSUPPORTED_OAUTH_PROVIDER_ERROR = "Unsupported OAuth provider: %s";

private static final String ERROR_QUERY_NAME = "error_code";
@Inject protected OAuthAuthenticatorProvider providers;

@GET
Expand Down Expand Up @@ -74,9 +75,16 @@ public Response callback() throws OAuthAuthenticationException, BadRequestExcept
final String providerName = getParameter(parameters, "oauth_provider");
final String redirectAfterLogin = getParameter(parameters, "redirect_after_login");

getAuthenticator(providerName).callback(requestUrl);
UriBuilder redirectUriBuilder = UriBuilder.fromUri(redirectAfterLogin);

return Response.temporaryRedirect(URI.create(redirectAfterLogin)).build();
try {
getAuthenticator(providerName).callback(requestUrl);
} catch (UserDeniedOAuthAuthenticationException e) {
redirectUriBuilder.queryParam(ERROR_QUERY_NAME, "access_denied");
} catch (OAuthAuthenticationException e) {
redirectUriBuilder.queryParam(ERROR_QUERY_NAME, "invalid_request");
}
return Response.temporaryRedirect(redirectUriBuilder.build()).build();
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ String callback(final URL requestUrl) throws OAuthAuthenticationException {
getAccessToken.consumerKey = clientId;
getAccessToken.temporaryToken = oauthTemporaryToken;
getAccessToken.verifier = (String) callbackUrl.getFirst(OAUTH_VERIFIER_PARAM_KEY);

if ("denied".equals(getAccessToken.verifier)) {
throw new UserDeniedOAuthAuthenticationException("Authorization denied");
}

getAccessToken.transport = httpTransport;
if (signatureMethod != null && "rsa".equalsIgnoreCase(signatureMethod)) {
getAccessToken.signer = getOAuthRsaSigner();
Expand All @@ -197,7 +202,6 @@ String callback(final URL requestUrl) throws OAuthAuthenticationException {
}

final OAuthCredentialsResponse credentials = getAccessToken.execute();

String userId = getParameterFromState(state, USER_ID_PARAM_KEY);

credentialsStoreLock.lock();
Expand All @@ -217,6 +221,8 @@ String callback(final URL requestUrl) throws OAuthAuthenticationException {

return userId;

} catch (OAuthAuthenticationException e) {
throw e;
} catch (Exception e) {
throw new OAuthAuthenticationException(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2012-2018 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/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.security.oauth1;

/** Exception used when a user denies access on the OAuth authorization page. */
public final class UserDeniedOAuthAuthenticationException extends OAuthAuthenticationException {
public UserDeniedOAuthAuthenticationException(String message) {
super(message);
}

public UserDeniedOAuthAuthenticationException(Throwable cause) {
super(cause);
}
}

0 comments on commit f162778

Please sign in to comment.