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