use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.
the class OAuth2AccessTokenServiceTest method testCacheEntryIsEvictedOnExpiry.
@Test
void testCacheEntryIsEvictedOnExpiry() throws InterruptedException {
ClientProperties clientProperties = onBehalfOfProperties();
when(assertionResolver.token()).thenReturn(Optional.of(jwt("sub1").serialize()));
// should invoke client and populate cache
String firstAccessToken = "first_access_token";
when(onBehalfOfTokenResponseClient.getTokenResponse(any(OnBehalfOfGrantRequest.class))).thenReturn(accessTokenResponse(firstAccessToken, 1));
OAuth2AccessTokenResponse oAuth2AccessTokenResponse1 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(onBehalfOfTokenResponseClient, times(1)).getTokenResponse(any(OnBehalfOfGrantRequest.class));
assertThat(oAuth2AccessTokenResponse1).hasNoNullFieldsOrProperties();
assertThat(oAuth2AccessTokenResponse1.getAccessToken()).isEqualTo("first_access_token");
Thread.sleep(1000);
// entry should be missing from cache due to expiry
reset(onBehalfOfTokenResponseClient);
String secondAccessToken = "second_access_token";
when(onBehalfOfTokenResponseClient.getTokenResponse(any(OnBehalfOfGrantRequest.class))).thenReturn(accessTokenResponse(secondAccessToken, 1));
OAuth2AccessTokenResponse oAuth2AccessTokenResponse2 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(onBehalfOfTokenResponseClient, times(1)).getTokenResponse(any(OnBehalfOfGrantRequest.class));
assertThat(oAuth2AccessTokenResponse2.getAccessToken()).isEqualTo(secondAccessToken);
}
use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.
the class OnBehalfOfTokenClientTest method getTokenResponse.
@Test
void getTokenResponse() throws InterruptedException {
this.server.enqueue(jsonResponse(TOKEN_RESPONSE));
String assertion = jwt("sub1").serialize();
ClientProperties clientProperties = clientProperties(this.tokenEndpointUrl, OAuth2GrantType.JWT_BEARER);
OnBehalfOfGrantRequest oAuth2OnBehalfOfGrantRequest = new OnBehalfOfGrantRequest(clientProperties, assertion);
OAuth2AccessTokenResponse response = onBehalfOfTokenResponseClient.getTokenResponse(oAuth2OnBehalfOfGrantRequest);
RecordedRequest recordedRequest = server.takeRequest();
assertPostMethodAndJsonHeaders(recordedRequest);
String formParameters = recordedRequest.getBody().readUtf8();
assertThat(formParameters).contains("grant_type=" + URLEncoder.encode(OAuth2GrantType.JWT_BEARER.value(), StandardCharsets.UTF_8));
assertThat(formParameters).contains("scope=scope1+scope2");
assertThat(formParameters).contains("requested_token_use=on_behalf_of");
assertThat(formParameters).contains("assertion=" + assertion);
assertThat(response).isNotNull();
assertThat(response.getAccessToken()).isNotBlank();
assertThat(response.getExpiresAt()).isGreaterThan(0);
assertThat(response.getExpiresIn()).isGreaterThan(0);
}
use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.
the class OnBehalfOfTokenClientTest method getTokenResponseWithError.
@Test
void getTokenResponseWithError() {
this.server.enqueue(jsonResponse(ERROR_RESPONSE).setResponseCode(400));
String assertion = jwt("sub1").serialize();
ClientProperties clientProperties = clientProperties(this.tokenEndpointUrl, OAuth2GrantType.JWT_BEARER);
OnBehalfOfGrantRequest oAuth2OnBehalfOfGrantRequest = new OnBehalfOfGrantRequest(clientProperties, assertion);
assertThatExceptionOfType(OAuth2ClientException.class).isThrownBy(() -> onBehalfOfTokenResponseClient.getTokenResponse(oAuth2OnBehalfOfGrantRequest)).withMessageContaining(ERROR_RESPONSE);
}
Aggregations