use of ddf.catalog.source.OAuthFederatedSource in project ddf by codice.
the class OAuthPluginTest method testDifferentDiscoveryUrl.
@Test(expected = OAuthPluginException.class)
public void testDifferentDiscoveryUrl() throws Exception {
OAuthFederatedSource source = oauthPlugin.oauthSource;
Subject subject = getSubject();
QueryRequest input = mock(QueryRequest.class);
when(input.getProperties()).thenReturn(ImmutableMap.of(SECURITY_SUBJECT, subject));
Map<String, Map<String, Object>> stateMap = mock(Map.class);
String accessToken = getAccessTokenBuilder().sign(validAlgorithm);
TokenInformation.TokenEntry tokenEntry = new TokenInformationImpl.TokenEntryImpl(accessToken, "refresh_token", "http://example.com");
when(tokenStorage.read(SESSION, CSW_SOURCE)).thenReturn(tokenEntry);
when(tokenStorage.getStateMap()).thenReturn(stateMap);
try {
oauthPlugin.process(source, input);
} catch (OAuthPluginException e) {
verify(tokenStorage, times(1)).delete(SESSION, CSW_SOURCE);
verify(tokenStorage, times(1)).getStateMap();
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
verify(stateMap, times(1)).put(anyString(), captor.capture());
assertUrl(e, captor.getValue());
throw e;
}
}
use of ddf.catalog.source.OAuthFederatedSource in project ddf by codice.
the class OAuthPluginTest method testNoStoredTokensButExistingUnderDifferentSource.
@Test(expected = OAuthPluginException.class)
public void testNoStoredTokensButExistingUnderDifferentSource() throws Exception {
OAuthFederatedSource source = oauthPlugin.oauthSource;
Subject subject = getSubject();
QueryRequest input = mock(QueryRequest.class);
when(input.getProperties()).thenReturn(ImmutableMap.of(SECURITY_SUBJECT, subject));
String accessToken = getAccessTokenBuilder().withExpiresAt(new Date(Instant.now().plus(1, ChronoUnit.MINUTES).toEpochMilli())).sign(validAlgorithm);
TokenInformation.TokenEntry tokenEntry = new TokenInformationImpl.TokenEntryImpl(accessToken, "myRefreshToken", METADATA_ENDPOINT);
TokenInformation tokenInformation = mock(TokenInformation.class);
when(tokenInformation.getDiscoveryUrls()).thenReturn(Collections.singleton(METADATA_ENDPOINT));
when(tokenInformation.getTokenEntries()).thenReturn(Collections.singletonMap("OS", tokenEntry));
when(tokenStorage.read(SESSION, SOURCE_ID)).thenReturn(null);
when(tokenStorage.read(SESSION)).thenReturn(tokenInformation);
try {
oauthPlugin.process(source, input);
} catch (OAuthPluginException e) {
assertEquals(e.getSourceId(), CSW_SOURCE);
assertEquals(e.getErrorType().getStatusCode(), 412);
String url = e.getUrl();
Map<String, String> urlParams = URLEncodedUtils.parse(new URI(url), StandardCharsets.UTF_8).stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));
assertEquals(urlParams.get(SOURCE_ID), CSW_SOURCE);
assertEquals(urlParams.get(DISCOVERY_URL), METADATA_ENDPOINT);
throw e;
}
}
use of ddf.catalog.source.OAuthFederatedSource in project ddf by codice.
the class OAuthPluginTest method testProcessExpiredAccessToken.
@Test
public void testProcessExpiredAccessToken() throws Exception {
OAuthFederatedSource source = oauthPlugin.oauthSource;
Subject subject = getSubject();
QueryRequest input = mock(QueryRequest.class);
when(input.getProperties()).thenReturn(ImmutableMap.of(SECURITY_SUBJECT, subject));
String accessToken = getAccessTokenBuilder().withExpiresAt(new Date(Instant.now().minus(1, ChronoUnit.MINUTES).toEpochMilli())).sign(validAlgorithm);
String refreshToken = getRefreshTokenBuilder().sign(validAlgorithm);
TokenInformation.TokenEntry tokenEntry = new TokenInformationImpl.TokenEntryImpl(accessToken, refreshToken, METADATA_ENDPOINT);
when(tokenStorage.read(SESSION, CSW_SOURCE)).thenReturn(tokenEntry);
String validAccessToken = getAccessTokenBuilder().sign(validAlgorithm);
Response response = mock(Response.class);
when(response.getStatus()).thenReturn(200);
when(response.getEntity()).thenReturn(getResponse(validAccessToken));
when(oauthPlugin.webClient.form(any(Form.class))).thenReturn(response);
QueryRequest output = oauthPlugin.process(source, input);
assertEquals(input, output);
verify(tokenStorage, times(1)).create(anyString(), anyString(), anyString(), anyString(), anyString());
}
use of ddf.catalog.source.OAuthFederatedSource in project ddf by codice.
the class OAuthPluginTest method testInvalidRefreshedAccessToken.
@Test(expected = OAuthPluginException.class)
public void testInvalidRefreshedAccessToken() throws Exception {
OAuthFederatedSource source = oauthPlugin.oauthSource;
Subject subject = getSubject();
QueryRequest input = mock(QueryRequest.class);
when(input.getProperties()).thenReturn(ImmutableMap.of(SECURITY_SUBJECT, subject));
String accessToken = getAccessTokenBuilder().withExpiresAt(new Date(Instant.now().minus(1, ChronoUnit.MINUTES).toEpochMilli())).sign(validAlgorithm);
String refreshToken = getRefreshTokenBuilder().sign(validAlgorithm);
Map<String, Map<String, Object>> stateMap = mock(Map.class);
TokenInformation.TokenEntry tokenEntry = new TokenInformationImpl.TokenEntryImpl(accessToken, refreshToken, METADATA_ENDPOINT);
when(tokenStorage.read(SESSION, CSW_SOURCE)).thenReturn(tokenEntry);
when(tokenStorage.getStateMap()).thenReturn(stateMap);
String invalidAccessToken = getAccessTokenBuilder().sign(invalidAlgorithm);
Response response = mock(Response.class);
when(response.getStatus()).thenReturn(200);
when(response.getEntity()).thenReturn(getResponse(invalidAccessToken));
when(oauthPlugin.webClient.form(any(Form.class))).thenReturn(response);
try {
oauthPlugin.process(source, input);
} catch (OAuthPluginException e) {
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
verify(stateMap, times(1)).put(anyString(), captor.capture());
verify(tokenStorage, times(0)).create(anyString(), anyString(), anyString(), anyString(), anyString());
verify(tokenStorage, times(1)).getStateMap();
assertUrl(e, captor.getValue());
throw e;
}
}
use of ddf.catalog.source.OAuthFederatedSource in project ddf by codice.
the class OAuthPluginTest method testInvalidRefreshError.
@Test(expected = OAuthPluginException.class)
public void testInvalidRefreshError() throws Exception {
OAuthFederatedSource source = oauthPlugin.oauthSource;
Subject subject = getSubject();
QueryRequest input = mock(QueryRequest.class);
when(input.getProperties()).thenReturn(ImmutableMap.of(SECURITY_SUBJECT, subject));
String accessToken = getAccessTokenBuilder().withExpiresAt(new Date(Instant.now().minus(1, ChronoUnit.MINUTES).toEpochMilli())).sign(validAlgorithm);
String refreshToken = getRefreshTokenBuilder().sign(validAlgorithm);
Map<String, Map<String, Object>> stateMap = mock(Map.class);
TokenInformation.TokenEntry tokenEntry = new TokenInformationImpl.TokenEntryImpl(accessToken, refreshToken, METADATA_ENDPOINT);
when(tokenStorage.read(SESSION, CSW_SOURCE)).thenReturn(tokenEntry);
when(tokenStorage.getStateMap()).thenReturn(stateMap);
Response response = mock(Response.class);
when(response.getStatus()).thenReturn(400);
when(response.getEntity()).thenReturn(new ByteArrayInputStream("".getBytes()));
when(oauthPlugin.webClient.form(any(Form.class))).thenReturn(response);
try {
oauthPlugin.process(source, input);
} catch (OAuthPluginException e) {
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
verify(stateMap, times(1)).put(anyString(), captor.capture());
verify(tokenStorage, times(1)).getStateMap();
assertUrl(e, captor.getValue());
throw e;
}
}
Aggregations