Search in sources :

Example 11 with ClientProperties

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);
}
Also used : ClientProperties(no.nav.security.token.support.client.core.ClientProperties) Test(org.junit.jupiter.api.Test)

Example 12 with ClientProperties

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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ClientProperties(no.nav.security.token.support.client.core.ClientProperties) Test(org.junit.jupiter.api.Test)

Example 13 with ClientProperties

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);
}
Also used : ClientProperties(no.nav.security.token.support.client.core.ClientProperties) Test(org.junit.jupiter.api.Test)

Aggregations

ClientProperties (no.nav.security.token.support.client.core.ClientProperties)13 Test (org.junit.jupiter.api.Test)13 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 JWSVerifier (com.nimbusds.jose.JWSVerifier)1 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 Instant (java.time.Instant)1 ClientAuthenticationProperties (no.nav.security.token.support.client.core.ClientAuthenticationProperties)1