OAuth2RequestFilter extends AbstractRequestFilter { @Override public void filterRestRequest(SubmitContext context, RestRequestInterface request) { HttpRequestBase httpMethod = (HttpRequestBase) context.getProperty(BaseHttpRequestTransport.HTTP_METHOD); if (O_AUTH_2_0.toString().equals(request.getAuthType())) { OAuth2ProfileContainer profileContainer = request.getResource().getService().getProject() .getOAuth2ProfileContainer(); OAuth2Profile profile = profileContainer.getProfileByName(((AbstractHttpRequest) request).getSelectedAuthProfile()); if (profile == null || StringUtils.isNullOrEmpty(profile.getAccessToken())) { return; } OAuth2ClientFacade oAuth2Client = getOAuth2ClientFacade(); if (accessTokenIsExpired(profile)) { if (profile.shouldReloadAccessTokenAutomatically()) { reloadAccessToken(profile, oAuth2Client); } else { profile.setAccessTokenStatus(OAuth2Profile.AccessTokenStatus.EXPIRED); } } oAuth2Client.applyAccessToken(profile, httpMethod, request.getRequestContent()); } else if (O_AUTH_1_0.toString().equals(request.getAuthType())) { OAuth1ProfileContainer profileContainer = request.getResource().getService().getProject() .getOAuth1ProfileContainer(); OAuth1Profile profile = profileContainer.getProfileByName( ((AbstractHttpRequest) request).getSelectedAuthProfile()); if (profile == null || StringUtils.isNullOrEmpty(profile.getAccessToken())) { return; } OAuth1ClientFacade oAuth1Client = getOAuth1ClientFacade(); oAuth1Client.applyAccessToken(profile, httpMethod, request.getRequestContent()); } }  @Override void filterRestRequest(SubmitContext context, RestRequestInterface request);  }
@Test public void appliesAccessToken() throws URISyntaxException { String expectedAccessTokenValue = "Bearer " + ACCESS_TOKEN; oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); assertThat(httpRequest.getHeaders(OAuth.HeaderType.AUTHORIZATION)[0].getValue(), is(expectedAccessTokenValue)); }
@Test public void doesNotApplyNullAccessTokenToHeader() throws Exception { oAuth2Profile.setAccessToken(null); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); assertThat(httpRequest.getHeaders(OAuth.HeaderType.AUTHORIZATION), is(anEmptyArray())); }
@Test public void doesNotApplyAccessTokenIfOAuthTypeIsNotOAuth2() { restRequest.setSelectedAuthProfileAndAuthType(PREEMPTIVE.toString(), PREEMPTIVE); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); assertThat(httpRequest.getHeaders(OAuth.HeaderType.AUTHORIZATION), is(anEmptyArray())); }
@Test public void automaticallyRefreshAccessTokenIfExpired() throws Exception { OAuth2Profile profileWithRefreshToken = setProfileWithRefreshTokenAndExpiredAccessToken(); oAuth2FilterWithMockOAuth2ClientFacade(profileWithRefreshToken); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); String actualAccessTokenHeader = httpRequest.getHeaders((OAuth.HeaderType.AUTHORIZATION))[0].getValue(); assertThat(actualAccessTokenHeader, is("Bearer " + OAuth2TestUtils.ACCESS_TOKEN)); }
@Test public void automaticallyRefreshesAccessTokenIfExpired() throws Exception { setupProfileWithRefreshToken(); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); String actualAccessTokenHeader = httpRequest.getHeaders((OAuth.HeaderType.AUTHORIZATION))[0].getValue(); assertThat(actualAccessTokenHeader, is("Bearer " + OAuth2TestUtils.ACCESS_TOKEN)); }
@Test public void doesNotRefreshAccessTokenWhenRefreshMethodIsManual() throws SoapUIException { OAuth2Profile profileWithRefreshToken = setProfileWithRefreshTokenAndExpiredAccessToken(); profileWithRefreshToken.setRefreshAccessTokenMethod(OAuth2Profile.RefreshAccessTokenMethods.MANUAL); oAuth2FilterWithMockOAuth2ClientFacade(profileWithRefreshToken); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); assertThat(profileWithRefreshToken.getAccessToken(), is(EXPIRED_TOKEN)); }
@Test public void automaticallyReloadsAccessTokenWhenProfileHasAutomationScripts() throws Exception { setupProfileWithAutomationScripts(); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); String actualAccessTokenHeader = httpRequest.getHeaders((OAuth.HeaderType.AUTHORIZATION))[0].getValue(); assertThat(actualAccessTokenHeader, is("Bearer " + RETRIEVED_ACCESS_TOKEN)); }
@Test public void addsLogStatementsWhenRefreshingAccessToken() throws Exception { setupProfileWithRefreshToken(); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); verify(mockLogger, times(2)).info(any(String.class)); }
@Test public void addsLogStatementsWhenReloadingAccessToken() throws Exception { setupProfileWithAutomationScripts(); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); verify(mockLogger, times(2)).info(any(String.class)); }
@Test public void refreshAccessTokenIfManualExpirationTimeIsSetAndManualExpirationTimeHasPassed() throws Exception { setupProfileWithRefreshToken(); oAuth2Profile.setAccessTokenExpirationTime(3600); oAuth2Profile.setManualAccessTokenExpirationTime("1"); oAuth2Profile.setUseManualAccessTokenExpirationTime(true); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); String actualAccessTokenHeader = httpRequest.getHeaders((OAuth.HeaderType.AUTHORIZATION))[0].getValue(); assertThat(actualAccessTokenHeader, is("Bearer " + OAuth2TestUtils.ACCESS_TOKEN)); }
@Test public void doesNotRefreshAccessTokenEvenIfExpiredWhenManualExpirationTimeIsSelected() throws Exception { setupProfileWithRefreshToken(); oAuth2Profile.setAccessTokenIssuedTime(System.currentTimeMillis()); oAuth2Profile.setAccessTokenExpirationTime(1); oAuth2Profile.setUseManualAccessTokenExpirationTime(true); oAuth2Profile.setManualAccessTokenExpirationTime("3600"); String originalAccessToken = oAuth2Profile.getAccessToken(); oAuth2RequestFilter.filterRestRequest(mockContext, restRequest); assertThat(oAuth2Profile.getAccessToken(), is(originalAccessToken)); }