From 7adeab1247259560de0103caf1c3d600913b11f0 Mon Sep 17 00:00:00 2001 From: Kristof Riebbels Date: Mon, 31 Oct 2022 15:20:19 +0100 Subject: [PATCH] Bugfix so authorizationcode flow is supported. Tested manually with auth0 --- ...orizationCodeWebViewUserBrowserFacade.java | 22 ++++++++++ .../actions/oauth/OAuth2TokenExtractor.java | 13 ++++-- .../oauth/WebViewUserBrowserFacade.java | 20 +++++---- ...ationCodeWebViewBasedBrowserComponent.java | 42 +++++++++++++++++++ .../EnabledWebViewBasedBrowserComponent.java | 19 ++++++--- .../WebViewBasedBrowserComponentFactory.java | 12 ++++++ 6 files changed, 110 insertions(+), 18 deletions(-) create mode 100644 soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/AuthorizationCodeWebViewUserBrowserFacade.java create mode 100644 soapui/src/main/java/com/eviware/soapui/support/components/EnabledAuthorizationCodeWebViewBasedBrowserComponent.java diff --git a/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/AuthorizationCodeWebViewUserBrowserFacade.java b/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/AuthorizationCodeWebViewUserBrowserFacade.java new file mode 100644 index 0000000000..68003336bd --- /dev/null +++ b/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/AuthorizationCodeWebViewUserBrowserFacade.java @@ -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; + } +} diff --git a/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/OAuth2TokenExtractor.java b/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/OAuth2TokenExtractor.java index 244f163464..3341839371 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/OAuth2TokenExtractor.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/OAuth2TokenExtractor.java @@ -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; @@ -46,8 +48,9 @@ public class OAuth2TokenExtractor { public static final String 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(); @@ -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)); } @@ -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) { diff --git a/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/WebViewUserBrowserFacade.java b/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/WebViewUserBrowserFacade.java index 759e9a7cb2..2d11db1119 100644 --- a/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/WebViewUserBrowserFacade.java +++ b/soapui/src/main/java/com/eviware/soapui/impl/rest/actions/oauth/WebViewUserBrowserFacade.java @@ -31,7 +31,7 @@ */ public class WebViewUserBrowserFacade implements UserBrowserFacade { - private WebViewBasedBrowserComponent browserComponent; + private final WebViewBasedBrowserComponent browserComponent; private JFrame popupWindow; public WebViewUserBrowserFacade() { @@ -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 @@ -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() + "!"); } @@ -88,7 +88,11 @@ public void run() { @Override public void executeJavaScript(String script) { - browserComponent.executeJavaScript(script); + getBrowserComponent().executeJavaScript(script); } + public WebViewBasedBrowserComponent getBrowserComponent(){ + return browserComponent; + } } + diff --git a/soapui/src/main/java/com/eviware/soapui/support/components/EnabledAuthorizationCodeWebViewBasedBrowserComponent.java b/soapui/src/main/java/com/eviware/soapui/support/components/EnabledAuthorizationCodeWebViewBasedBrowserComponent.java new file mode 100644 index 0000000000..c8d29aee65 --- /dev/null +++ b/soapui/src/main/java/com/eviware/soapui/support/components/EnabledAuthorizationCodeWebViewBasedBrowserComponent.java @@ -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; + } + +} diff --git a/soapui/src/main/java/com/eviware/soapui/support/components/EnabledWebViewBasedBrowserComponent.java b/soapui/src/main/java/com/eviware/soapui/support/components/EnabledWebViewBasedBrowserComponent.java index 6ad6dbc853..0851ed3a09 100644 --- a/soapui/src/main/java/com/eviware/soapui/support/components/EnabledWebViewBasedBrowserComponent.java +++ b/soapui/src/main/java/com/eviware/soapui/support/components/EnabledWebViewBasedBrowserComponent.java @@ -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()); @@ -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(); @@ -269,7 +274,7 @@ public void run() { pcs.firePropertyChange("content", null, contentAsString); } - private WebEngine getWebEngine() { + protected WebEngine getWebEngine() { return webView.getEngine(); } @@ -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; } @@ -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) { @@ -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); } @@ -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; } diff --git a/soapui/src/main/java/com/eviware/soapui/support/components/WebViewBasedBrowserComponentFactory.java b/soapui/src/main/java/com/eviware/soapui/support/components/WebViewBasedBrowserComponentFactory.java index fd6eaf4da9..72011c13b5 100644 --- a/soapui/src/main/java/com/eviware/soapui/support/components/WebViewBasedBrowserComponentFactory.java +++ b/soapui/src/main/java/com/eviware/soapui/support/components/WebViewBasedBrowserComponentFactory.java @@ -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); + } + } }