use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.
the class ClientCredentialsTokenClientTest method getTokenResponseWithDefaultClientAuthMethod.
@Test
void getTokenResponseWithDefaultClientAuthMethod() throws InterruptedException {
this.server.enqueue(jsonResponse(TOKEN_RESPONSE));
ClientProperties clientProperties = clientProperties(tokenEndpointUrl, OAuth2GrantType.CLIENT_CREDENTIALS);
OAuth2AccessTokenResponse response = client.getTokenResponse(new ClientCredentialsGrantRequest(clientProperties));
RecordedRequest recordedRequest = this.server.takeRequest();
assertPostMethodAndJsonHeaders(recordedRequest);
assertThatClientAuthMethodIsClientSecretBasic(recordedRequest, clientProperties);
String body = recordedRequest.getBody().readUtf8();
assertThatRequestBodyContainsFormParameters(body);
assertThatResponseContainsAccessToken(response);
}
use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.
the class OAuth2AccessTokenServiceTest method getAccessTokenOnBehalfOf_WithCache_MultipleTimes_SameClientConfig.
@Test
void getAccessTokenOnBehalfOf_WithCache_MultipleTimes_SameClientConfig() {
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, 60));
OAuth2AccessTokenResponse oAuth2AccessTokenResponse1 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(onBehalfOfTokenResponseClient, times(1)).getTokenResponse(any(OnBehalfOfGrantRequest.class));
assertThat(oAuth2AccessTokenResponse1).hasNoNullFieldsOrProperties();
assertThat(oAuth2AccessTokenResponse1.getAccessToken()).isEqualTo("first_access_token");
// should get response from cache and NOT invoke client
reset(onBehalfOfTokenResponseClient);
OAuth2AccessTokenResponse oAuth2AccessTokenResponse2 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(onBehalfOfTokenResponseClient, never()).getTokenResponse(any(OnBehalfOfGrantRequest.class));
assertThat(oAuth2AccessTokenResponse2.getAccessToken()).isEqualTo("first_access_token");
// another user/token but same clientconfig, should invoke client and populate cache
reset(assertionResolver);
when(assertionResolver.token()).thenReturn(Optional.of(jwt("sub2").serialize()));
reset(onBehalfOfTokenResponseClient);
String secondAccessToken = "second_access_token";
when(onBehalfOfTokenResponseClient.getTokenResponse(any(OnBehalfOfGrantRequest.class))).thenReturn(accessTokenResponse(secondAccessToken, 60));
OAuth2AccessTokenResponse oAuth2AccessTokenResponse3 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(onBehalfOfTokenResponseClient, times(1)).getTokenResponse(any(OnBehalfOfGrantRequest.class));
assertThat(oAuth2AccessTokenResponse3.getAccessToken()).isEqualTo(secondAccessToken);
}
use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.
the class OAuth2AccessTokenServiceTest method getAccessTokenClientCredentials.
@Test
void getAccessTokenClientCredentials() {
ClientProperties clientProperties = clientCredentialsProperties();
String firstAccessToken = "first_access_token";
when(clientCredentialsTokenResponseClient.getTokenResponse(any(ClientCredentialsGrantRequest.class))).thenReturn(accessTokenResponse(firstAccessToken, 60));
OAuth2AccessTokenResponse oAuth2AccessTokenResponse1 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(clientCredentialsTokenResponseClient, times(1)).getTokenResponse(any(ClientCredentialsGrantRequest.class));
assertThat(oAuth2AccessTokenResponse1).hasNoNullFieldsOrProperties();
assertThat(oAuth2AccessTokenResponse1.getAccessToken()).isEqualTo("first_access_token");
}
use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.
the class OAuth2AccessTokenServiceTest method getAccessTokenClientCredentials_WithCache_MultipleTimes.
@Test
void getAccessTokenClientCredentials_WithCache_MultipleTimes() {
ClientProperties clientProperties = clientCredentialsProperties();
// should invoke client and populate cache
String firstAccessToken = "first_access_token";
when(clientCredentialsTokenResponseClient.getTokenResponse(any(ClientCredentialsGrantRequest.class))).thenReturn(accessTokenResponse(firstAccessToken, 60));
OAuth2AccessTokenResponse oAuth2AccessTokenResponse1 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(clientCredentialsTokenResponseClient, times(1)).getTokenResponse(any(ClientCredentialsGrantRequest.class));
assertThat(oAuth2AccessTokenResponse1).hasNoNullFieldsOrProperties();
assertThat(oAuth2AccessTokenResponse1.getAccessToken()).isEqualTo("first_access_token");
// should get response from cache and NOT invoke client
reset(clientCredentialsTokenResponseClient);
OAuth2AccessTokenResponse oAuth2AccessTokenResponse2 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(clientCredentialsTokenResponseClient, never()).getTokenResponse(any(ClientCredentialsGrantRequest.class));
assertThat(oAuth2AccessTokenResponse2.getAccessToken()).isEqualTo("first_access_token");
// another clientconfig, should invoke client and populate cache
clientProperties = clientCredentialsProperties("scope3");
reset(clientCredentialsTokenResponseClient);
String secondAccessToken = "second_access_token";
when(clientCredentialsTokenResponseClient.getTokenResponse(any(ClientCredentialsGrantRequest.class))).thenReturn(accessTokenResponse(secondAccessToken, 60));
OAuth2AccessTokenResponse oAuth2AccessTokenResponse3 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(clientCredentialsTokenResponseClient, times(1)).getTokenResponse(any(ClientCredentialsGrantRequest.class));
assertThat(oAuth2AccessTokenResponse3.getAccessToken()).isEqualTo(secondAccessToken);
}
use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.
the class OAuth2AccessTokenServiceTest method getAccessTokenOnBehalfOf.
@Test
void getAccessTokenOnBehalfOf() {
ClientProperties clientProperties = onBehalfOfProperties();
when(assertionResolver.token()).thenReturn(Optional.of(jwt("sub1").serialize()));
String firstAccessToken = "first_access_token";
when(onBehalfOfTokenResponseClient.getTokenResponse(any(OnBehalfOfGrantRequest.class))).thenReturn(accessTokenResponse(firstAccessToken, 60));
OAuth2AccessTokenResponse oAuth2AccessTokenResponse1 = oAuth2AccessTokenService.getAccessToken(clientProperties);
verify(onBehalfOfTokenResponseClient, times(1)).getTokenResponse(any(OnBehalfOfGrantRequest.class));
assertThat(oAuth2AccessTokenResponse1).hasNoNullFieldsOrProperties();
assertThat(oAuth2AccessTokenResponse1.getAccessToken()).isEqualTo("first_access_token");
}
Aggregations