Search in sources :

Example 1 with ClientProperties

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

Example 2 with ClientProperties

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

Example 3 with ClientProperties

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

Example 4 with ClientProperties

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

Example 5 with ClientProperties

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");
}
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