Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix so authorizationcode flow is supported. #755

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.eviware.soapui.impl.rest.actions.oauth;

import com.eviware.soapui.support.components.WebViewBasedBrowserComponent;
import com.eviware.soapui.support.components.WebViewBasedBrowserComponentFactory;

public class AuthorizationCodeWebViewUserBrowserFacade extends WebViewUserBrowserFacade {

private final WebViewBasedBrowserComponent browserComponent;

public AuthorizationCodeWebViewUserBrowserFacade() {
this(false);
}

public AuthorizationCodeWebViewUserBrowserFacade(boolean addNavigationBar) {
browserComponent = WebViewBasedBrowserComponentFactory.createAuthorizationBrowserComponent(addNavigationBar);
}

@Override
public WebViewBasedBrowserComponent getBrowserComponent() {
return browserComponent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport;
import com.eviware.soapui.support.StringUtils;
import com.eviware.soapui.support.TimeUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
Expand All @@ -46,8 +48,9 @@ public class OAuth2TokenExtractor {
public static final String TITLE = "<TITLE>";
public static final String TOKEN = "token";
public static final String ACCESS_TOKEN = "access_token";
private final static Logger logger = LogManager.getLogger("soapui.httplog");

protected List<BrowserListener> browserListeners = new ArrayList<BrowserListener>();
protected List<BrowserListener> browserListeners = new ArrayList<>();

public void extractAccessToken(final OAuth2Parameters parameters) throws OAuthSystemException, MalformedURLException, URISyntaxException, OAuthProblemException {
OAuth2Profile.OAuth2Flow i = parameters.getOAuth2Flow();
Expand All @@ -70,12 +73,13 @@ public void extractAccessToken(final OAuth2Parameters parameters) throws OAuthSy

void extractAccessTokenForAuthorizationCodeGrantFlow(final OAuth2Parameters parameters) throws URISyntaxException,
MalformedURLException, OAuthSystemException {
final UserBrowserFacade browserFacade = getBrowserFacade();
final UserBrowserFacade browserFacade = getAuthorizationCodeComaptibleBrowserFacade();
addBrowserInteractionHandler(browserFacade, parameters);
addExternalListeners(browserFacade);
browserFacade.addBrowserListener(new BrowserListenerAdapter() {
@Override
public void locationChanged(String newLocation) {
logger.atInfo().log(newLocation);
getAccessTokenAndSaveToProfile(browserFacade, parameters, extractAuthorizationCodeFromForm(extractFormData(newLocation), CODE));
}

Expand Down Expand Up @@ -207,11 +211,12 @@ public void addBrowserListener(BrowserListener listener) {
protected OAuthClient getOAuthClient() {
return new OAuthClient(new HttpClient4(HttpClientSupport.getHttpClient()));
}

protected UserBrowserFacade getBrowserFacade() {
return new WebViewUserBrowserFacade();
}

protected UserBrowserFacade getAuthorizationCodeComaptibleBrowserFacade() {
return new AuthorizationCodeWebViewUserBrowserFacade();
}
/* Helper methods */

private void setRetrievedCanceledStatus(OAuth2Parameters parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class WebViewUserBrowserFacade implements UserBrowserFacade {

private WebViewBasedBrowserComponent browserComponent;
private final WebViewBasedBrowserComponent browserComponent;
private JFrame popupWindow;

public WebViewUserBrowserFacade() {
Expand All @@ -46,26 +46,26 @@ public WebViewUserBrowserFacade(boolean addNavigationBar) {
public void open(URL url) {
popupWindow = new JFrame("Browser");
popupWindow.setIconImages(SoapUI.getFrameIcons());
popupWindow.getContentPane().add(browserComponent.getComponent());
popupWindow.getContentPane().add(getBrowserComponent().getComponent());
popupWindow.setBounds(100, 100, 800, 600);
popupWindow.setVisible(true);
popupWindow.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
browserComponent.close(true);
getBrowserComponent().close(true);
}
});
browserComponent.navigate(url.toString());
getBrowserComponent().navigate(url.toString());
}

@Override
public void addBrowserListener(BrowserListener listener) {
browserComponent.addBrowserStateListener(listener);
getBrowserComponent().addBrowserStateListener(listener);
}

@Override
public void removeBrowserStateListener(BrowserListener listener) {
browserComponent.removeBrowserStateListener(listener);
getBrowserComponent().removeBrowserStateListener(listener);
}

@Override
Expand All @@ -79,7 +79,7 @@ public void run() {
popupWindow.dispose();
}
});
browserComponent.close(true);
getBrowserComponent().close(true);
} catch (Exception e) {
SoapUI.log.debug("Could not close window due to unexpected error: " + e.getMessage() + "!");
}
Expand All @@ -88,7 +88,11 @@ public void run() {

@Override
public void executeJavaScript(String script) {
browserComponent.executeJavaScript(script);
getBrowserComponent().executeJavaScript(script);
}

public WebViewBasedBrowserComponent getBrowserComponent(){
return browserComponent;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.eviware.soapui.support.components;

import com.eviware.soapui.SoapUI;
import javafx.application.Platform;


class EnabledAuthorizationCodeWebViewBasedBrowserComponent extends EnabledWebViewBasedBrowserComponent {
private static final String DEFAULT_ERROR_PAGE = "<html><body><h1>AuthorizationCode request failed</h1></body></html>";

public String url;

@Override
public String getDefaultErrorPage()
{
return DEFAULT_ERROR_PAGE;
}
EnabledAuthorizationCodeWebViewBasedBrowserComponent(boolean addNavigationBar, PopupStrategy popupStrategy)
{
super(addNavigationBar,popupStrategy);
}

@Override
public void navigate(final String url) {
navigate(url, DEFAULT_ERROR_PAGE);
}
@Override
public void navigate(final String url, String backupUrl) {
if (SoapUI.isBrowserDisabled()) {
return;
}

loadUrl(url);

Platform.runLater(() -> getWebEngine().load(url));
}

private void loadUrl(final String url) {
Platform.runLater(() -> getWebEngine().load(url));
this.url = url;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@
import java.util.regex.Pattern;

import static com.eviware.soapui.SoapUI.getThreadPool;
import static com.eviware.soapui.SoapUI.log;

class EnabledWebViewBasedBrowserComponent implements WebViewBasedBrowserComponent {
public static final String CHARSET_PATTERN = "(.+)(;\\s*charset=)(.+)";
public static final String DEFAULT_ERROR_PAGE = "<html><body><h1>The page could not be loaded</h1></body></html>";
private static final String DEFAULT_ERROR_PAGE = "<html><body><h1>The page could not be loaded</h1></body></html>";
private Pattern charsetFinderPattern = Pattern.compile(CHARSET_PATTERN);

private JPanel panel = new JPanel(new BorderLayout());
Expand Down Expand Up @@ -99,6 +100,10 @@ public Component getComponent() {
return panel;
}

public String getDefaultErrorPage()
{
return DEFAULT_ERROR_PAGE;
}
private void initializeWebView(boolean addNavigationBar) {
if (addNavigationBar) {
navigationBar = new WebViewNavigationBar();
Expand Down Expand Up @@ -269,7 +274,7 @@ public void run() {
pcs.firePropertyChange("content", null, contentAsString);
}

private WebEngine getWebEngine() {
protected WebEngine getWebEngine() {
return webView.getEngine();
}

Expand All @@ -295,10 +300,10 @@ public void removePropertyChangeListener(PropertyChangeListener pcl) {

@Override
public void navigate(final String url) {
navigate(url, DEFAULT_ERROR_PAGE);
navigate(url, getDefaultErrorPage());
}

public void navigate(final String url, String backupUrl) {
@Override
public void navigate(final String url, String backupUrl) {
if (SoapUI.isBrowserDisabled()) {
return;
}
Expand Down Expand Up @@ -331,6 +336,7 @@ public static boolean verifyReturnCode(String urlString) {
if (neededIndex != -1) {
urlString = urlString.substring(0, neededIndex);
}

URL url = new URL(urlString);
final URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof HttpURLConnection) {
Expand Down Expand Up @@ -476,6 +482,7 @@ private void listenForLocationChanges() {
public void changed(ObservableValue<? extends String> observableValue, String oldLocation,
String newLocation) {
lastLocation = newLocation;
log.info(newLocation);
for (BrowserListener listener : listeners) {
listener.locationChanged(newLocation);
}
Expand All @@ -502,7 +509,7 @@ public void changed(ObservableValue value, Worker.State oldState, Worker.State n
} else if (newState == Worker.State.FAILED && !showingErrorPage) {
try {
showingErrorPage = true;
setContent(errorPage == null ? DEFAULT_ERROR_PAGE : errorPage);
setContent(errorPage == null ? getDefaultErrorPage() : errorPage);
} finally {
showingErrorPage = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ public static WebViewBasedBrowserComponent createBrowserComponent(boolean addNav
return new EnabledWebViewBasedBrowserComponent(addNavigationBar, popupStrategy);
}
}

public static WebViewBasedBrowserComponent createAuthorizationBrowserComponent(boolean addNavigationBar) {
return createAuthorizationBrowserComponent(addNavigationBar, WebViewBasedBrowserComponent.PopupStrategy.INTERNAL_BROWSER_NEW_WINDOW);
}

public static WebViewBasedBrowserComponent createAuthorizationBrowserComponent(boolean addNavigationBar, WebViewBasedBrowserComponent.PopupStrategy popupStrategy) {
if (SoapUI.isBrowserDisabled()) {
return new DisabledWebViewBasedBrowserComponent();
} else {
return new EnabledAuthorizationCodeWebViewBasedBrowserComponent(addNavigationBar, popupStrategy);
}
}
}