OAuth2TokenExtractor { void extractAccessTokenForAuthorizationCodeGrantFlow(final OAuth2Parameters parameters) throws URISyntaxException, MalformedURLException, OAuthSystemException { final UserBrowserFacade browserFacade = getBrowserFacade(); addBrowserInteractionHandler(browserFacade, parameters); addExternalListeners(browserFacade); browserFacade.addBrowserListener(new BrowserListenerAdapter() { @Override public void locationChanged(String newLocation) { getAccessTokenAndSaveToProfile(browserFacade, parameters, extractAuthorizationCodeFromForm(extractFormData(newLocation), CODE)); } @Override public void contentChanged(String newContent) { int titlePosition = newContent.indexOf(TITLE); if (titlePosition != -1) { String title = newContent.substring(titlePosition + TITLE.length(), newContent.indexOf("</TITLE>")); getAccessTokenAndSaveToProfile(browserFacade, parameters, extractAuthorizationCodeFromTitle(title)); } } @Override public void browserClosed() { super.browserClosed(); if (!parameters.isAccessTokenRetrivedFromServer()) { setRetrievedCanceledStatus(parameters); } } }); browserFacade.open(new URI(createAuthorizationURL(parameters, CODE)).toURL()); parameters.waitingForAuthorization(); }  void extractAccessToken(final OAuth2Parameters parameters); OAuthClientRequest getClientRequestForROPC(final OAuth2Parameters parameters); void extractAccessTokenForClientCredentialsGrant(final OAuth2Parameters parameters); OAuthClientRequest getClientRequestForClientCredentialsGrant(final OAuth2Parameters parameters); void addBrowserListener(BrowserListener listener); static final String CODE; static final String TITLE; static final String TOKEN; static final String ACCESS_TOKEN; }
@Test public void updatesProfileAccessTokenStatus() throws Exception { final List<OAuth2Profile.AccessTokenStatus> statuses = new ArrayList<OAuth2Profile.AccessTokenStatus>(); profile.addPropertyChangeListener(OAuth2Profile.ACCESS_TOKEN_STATUS_PROPERTY, new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { statuses.add((OAuth2Profile.AccessTokenStatus) evt.getNewValue()); } }); oAuth2TokenExtractor.extractAccessTokenForAuthorizationCodeGrantFlow(parameters); assertThat(statuses.size(), is(3)); assertThat(statuses, hasItem(OAuth2Profile.AccessTokenStatus.WAITING_FOR_AUTHORIZATION)); assertThat(statuses, hasItem(OAuth2Profile.AccessTokenStatus.RECEIVED_AUTHORIZATION_CODE)); assertThat(statuses, hasItem(OAuth2Profile.AccessTokenStatus.RETRIEVED_FROM_SERVER)); }
@Test public void executesJavaScripts() throws Exception { String firstScript = "document.getElementById('okButton').click()"; String secondScript = "document.getElementById('confirmButton').click()"; profile.setAutomationJavaScripts(Arrays.asList(firstScript, secondScript)); oAuth2TokenExtractor.extractAccessTokenForAuthorizationCodeGrantFlow(parameters); assertThat(executedScripts, hasItem(firstScript)); assertThat(executedScripts, hasItem(secondScript)); }
@Test public void getsTheAccessTokenFromJsonResponse() throws Exception { oAuth2TokenExtractor.extractAccessTokenForAuthorizationCodeGrantFlow(parameters); assertThat(profile.getAccessToken(), is(ACCESS_TOKEN)); }
@Test public void closesBrowserWindowAfterSavingTheAccessTokenToProfile() throws Exception { oAuth2TokenExtractor.extractAccessTokenForAuthorizationCodeGrantFlow(parameters); assertThat(browserFacade.browserClosed, is(true)); }
@Test public void getsTheAccessTokenFromUrlEncodedFormResponse() throws Exception { profile.setOAuth2Flow(OAuth2Profile.OAuth2Flow.IMPLICIT_GRANT); oAuth2TokenExtractor.extractAccessTokenForAuthorizationCodeGrantFlow(parameters); assertThat(profile.getAccessToken(), is(ACCESS_TOKEN)); }
@Test public void getsRefreshTokenFromResponseURI() throws Exception { oAuth2TokenExtractor.extractAccessTokenForAuthorizationCodeGrantFlow(parameters); assertThat(profile.getRefreshToken(), is(REFRESH_TOKEN)); }
@Test public void getsTheAccessTokenFromResponseBodyInOobRequest() throws Exception { profile.setRedirectURI(OAUTH_2_OOB_URN); oAuth2TokenExtractor.extractAccessTokenForAuthorizationCodeGrantFlow(parameters); assertThat(profile.getAccessToken(), is(ACCESS_TOKEN)); }
@Test public void sendsAuthorizationCodeInMessageBody() throws Exception { oAuth2TokenExtractor.extractAccessTokenForAuthorizationCodeGrantFlow(parameters); String code = (String) OAuthUtils.decodeForm(spyingOauthClientStub.oAuthClientRequest.getBody()).get("code"); assertThat(code, is(AUTHORIZATION_CODE)); }