Search in sources :

Example 6 with ClientProperties

use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.

the class ClientAssertionTest method testCreateAssertion.

@Test
void testCreateAssertion() throws ParseException, JOSEException {
    ClientAuthenticationProperties clientAuth = ClientAuthenticationProperties.builder().clientJwk("src/test/resources/jwk.json").clientId("client1").clientAuthMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT).build();
    ClientProperties clientProperties = ClientProperties.builder().grantType(OAuth2GrantType.CLIENT_CREDENTIALS).tokenEndpointUrl(URI.create("http://token")).authentication(clientAuth).build();
    Instant now = Instant.now();
    ClientAssertion clientAssertion = new ClientAssertion(clientProperties.getTokenEndpointUrl(), clientProperties.getAuthentication());
    assertThat(clientAssertion).isNotNull();
    assertThat(clientAssertion.assertionType()).isEqualTo("urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
    String assertion = clientAssertion.assertion();
    assertThat(clientAssertion.assertion()).isNotNull();
    SignedJWT signedJWT = SignedJWT.parse(assertion);
    String keyId = clientProperties.getAuthentication().getClientRsaKey().getKeyID();
    assertThat(signedJWT.getHeader().getKeyID()).isEqualTo(keyId);
    assertThat(signedJWT.getHeader().getType()).isEqualTo(JOSEObjectType.JWT);
    assertThat(signedJWT.getHeader().getAlgorithm()).isEqualTo(JWSAlgorithm.RS256);
    JWSVerifier verifier = new RSASSAVerifier(clientAuth.getClientRsaKey());
    assertThat(signedJWT.verify(verifier)).isTrue();
    JWTClaimsSet claims = signedJWT.getJWTClaimsSet();
    assertThat(claims.getSubject()).isEqualTo(clientAuth.getClientId());
    assertThat(claims.getIssuer()).isEqualTo(clientAuth.getClientId());
    assertThat(claims.getAudience()).containsExactly(clientProperties.getTokenEndpointUrl().toString());
    assertThat(claims.getExpirationTime()).isAfter(Date.from(now));
    assertThat(claims.getNotBeforeTime()).isBefore(claims.getExpirationTime());
}
Also used : ClientProperties(no.nav.security.token.support.client.core.ClientProperties) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) Instant(java.time.Instant) JWSVerifier(com.nimbusds.jose.JWSVerifier) SignedJWT(com.nimbusds.jwt.SignedJWT) ClientAuthenticationProperties(no.nav.security.token.support.client.core.ClientAuthenticationProperties) Test(org.junit.jupiter.api.Test)

Example 7 with ClientProperties

use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.

the class ClientCredentialsTokenClientTest method getTokenResponseWithClientSecretPost.

@Test
void getTokenResponseWithClientSecretPost() throws InterruptedException {
    this.server.enqueue(jsonResponse(TOKEN_RESPONSE));
    ClientProperties clientProperties = clientProperties(tokenEndpointUrl, OAuth2GrantType.CLIENT_CREDENTIALS).toBuilder().authentication(ClientAuthenticationProperties.builder().clientId("client").clientSecret("secret").clientAuthMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST).build()).build();
    OAuth2AccessTokenResponse response = client.getTokenResponse(new ClientCredentialsGrantRequest(clientProperties));
    RecordedRequest recordedRequest = this.server.takeRequest();
    assertPostMethodAndJsonHeaders(recordedRequest);
    String body = recordedRequest.getBody().readUtf8();
    assertThatClientAuthMethodIsClientSecretPost(body, clientProperties);
    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 8 with ClientProperties

use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.

the class ClientCredentialsTokenClientTest method getTokenResponseWithPrivateKeyJwt.

@Test
void getTokenResponseWithPrivateKeyJwt() throws InterruptedException {
    this.server.enqueue(jsonResponse(TOKEN_RESPONSE));
    ClientProperties clientProperties = clientProperties(tokenEndpointUrl, OAuth2GrantType.CLIENT_CREDENTIALS).toBuilder().authentication(ClientAuthenticationProperties.builder().clientId("client").clientAuthMethod(ClientAuthenticationMethod.PRIVATE_KEY_JWT).clientJwk("src/test/resources/jwk.json").build()).build();
    OAuth2AccessTokenResponse response = client.getTokenResponse(new ClientCredentialsGrantRequest(clientProperties));
    RecordedRequest recordedRequest = this.server.takeRequest();
    assertPostMethodAndJsonHeaders(recordedRequest);
    String body = recordedRequest.getBody().readUtf8();
    assertThatClientAuthMethodIsPrivateKeyJwt(body, clientProperties);
    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 9 with ClientProperties

use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.

the class ClientCredentialsTokenClientTest method getTokenResponseWithClientSecretBasic.

@Test
void getTokenResponseWithClientSecretBasic() 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 10 with ClientProperties

use of no.nav.security.token.support.client.core.ClientProperties in project token-support by navikt.

the class OAuth2AccessTokenServiceTest method getAccessTokenExchange.

@Test
void getAccessTokenExchange() {
    ClientProperties clientProperties = exchangeProperties();
    when(assertionResolver.token()).thenReturn(Optional.of(jwt("sub1").serialize()));
    String firstAccessToken = "first_access_token";
    when(exchangeTokeResponseClient.getTokenResponse(any(TokenExchangeGrantRequest.class))).thenReturn(accessTokenResponse(firstAccessToken, 60));
    OAuth2AccessTokenResponse oAuth2AccessTokenResponse1 = oAuth2AccessTokenService.getAccessToken(clientProperties);
    verify(exchangeTokeResponseClient, times(1)).getTokenResponse(any(TokenExchangeGrantRequest.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