use of org.mockserver.authentication.jwt.JWTGenerator in project mockserver by mock-server.
the class AuthenticatedControlPlaneUsingJWTClientMockingIntegrationTest method startServer.
@BeforeClass
public static void startServer() {
// save original value
originalControlPlaneJWTAuthenticationJWKSource = controlPlaneJWTAuthenticationJWKSource();
originalControlPlaneJWTAuthenticationRequired = controlPlaneJWTAuthenticationRequired();
// set new certificate authority values
AsymmetricKeyPair asymmetricKeyPair = AsymmetricKeyGenerator.createAsymmetricKeyPairSynchronously(AsymmetricKeyPair.KeyPairAlgorithm.RS256);
String jwkFile = TempFileWriter.write(new JWKGenerator().generateJWK(asymmetricKeyPair));
String jwt = new JWTGenerator(asymmetricKeyPair).generateJWT();
controlPlaneJWTAuthenticationJWKSource(jwkFile);
controlPlaneJWTAuthenticationRequired(true);
authorisationHeader = header(AUTHORIZATION.toString(), "Bearer " + jwt);
Main.main("-serverPort", "" + severHttpPort);
mockServerClient = new MockServerClient("localhost", severHttpPort).setRequestOverride(request().withHeader(authorisationHeader)).withSecure(true);
}
use of org.mockserver.authentication.jwt.JWTGenerator in project mockserver by mock-server.
the class AuthenticatedControlPlaneUsingJWTClientNotAuthenticatedIntegrationTest method shouldAuthenticateNonMatchingFirstClaimForExpectationCreationViaJavaClient.
@Test
public void shouldAuthenticateNonMatchingFirstClaimForExpectationCreationViaJavaClient() {
// given
String jwt = new JWTGenerator(asymmetricKeyPair).signJWT(ImmutableMap.of("exp", Clock.systemUTC().instant().plus(Duration.ofHours(1)).getEpochSecond(), "iat", Clock.systemUTC().instant().minus(Duration.ofHours(2)).getEpochSecond(), "iss", RandomStringUtils.randomAlphanumeric(20), "nbf", Clock.systemUTC().instant().minus(Duration.ofHours(2)).getEpochSecond(), "scope", "internal public", "name", "John Smith", "admin", "true", "aud", "https://mock-server.com"));
// when
AuthenticationException authenticationException = assertThrows(AuthenticationException.class, () -> mockServerClient.withControlPlaneJWT(() -> jwt).when(request()).respond(response().withBody("some_body")));
// then
assertThat(authenticationException.getMessage(), equalTo("Unauthorized for control plane - JWT name claim has value John Smith, must be John Doe"));
}
use of org.mockserver.authentication.jwt.JWTGenerator in project mockserver by mock-server.
the class AuthenticatedControlPlaneUsingJWTClientNotAuthenticatedIntegrationTest method shouldAuthenticateValidJWTForExpectationCreationViaJavaClient.
@Test
public void shouldAuthenticateValidJWTForExpectationCreationViaJavaClient() {
// given
String jwt = new JWTGenerator(asymmetricKeyPair).signJWT(ImmutableMap.of("exp", Clock.systemUTC().instant().plus(Duration.ofHours(1)).getEpochSecond(), "iat", Clock.systemUTC().instant().minus(Duration.ofHours(2)).getEpochSecond(), "iss", RandomStringUtils.randomAlphanumeric(20), "nbf", Clock.systemUTC().instant().minus(Duration.ofHours(2)).getEpochSecond(), "scope", "internal public", "name", "John Doe", "admin", "true", "aud", "https://mock-server.com"));
// when
mockServerClient.withControlPlaneJWT(() -> jwt).when(request()).respond(response().withBody("some_body"));
// then no exception thrown
}
use of org.mockserver.authentication.jwt.JWTGenerator in project mockserver by mock-server.
the class AuthenticatedControlPlaneUsingJWTViaOverrideClientMockingIntegrationTest method startServer.
@BeforeClass
public static void startServer() {
// save original value
originalControlPlaneJWTAuthenticationJWKSource = controlPlaneJWTAuthenticationJWKSource();
originalControlPlaneJWTAuthenticationRequired = controlPlaneJWTAuthenticationRequired();
// set new certificate authority values
AsymmetricKeyPair asymmetricKeyPair = AsymmetricKeyGenerator.createAsymmetricKeyPair(AsymmetricKeyPairAlgorithm.RSA2048_SHA256);
String jwkFile = TempFileWriter.write(new JWKGenerator().generateJWK(asymmetricKeyPair));
String jwt = new JWTGenerator(asymmetricKeyPair).generateJWT();
controlPlaneJWTAuthenticationJWKSource(jwkFile);
controlPlaneJWTAuthenticationRequired(true);
authorisationHeader = header(AUTHORIZATION.toString(), "Bearer " + jwt);
mockServerClient = ClientAndServer.startClientAndServer().withRequestOverride(request().withHeader(authorisationHeader)).withSecure(true);
}
use of org.mockserver.authentication.jwt.JWTGenerator in project mockserver by mock-server.
the class AuthenticatedControlPlaneUsingJWTViaSupplierClientMockingIntegrationTest method startServer.
@BeforeClass
public static void startServer() {
// save original value
originalControlPlaneJWTAuthenticationJWKSource = controlPlaneJWTAuthenticationJWKSource();
originalControlPlaneJWTAuthenticationRequired = controlPlaneJWTAuthenticationRequired();
// set new certificate authority values
AsymmetricKeyPair asymmetricKeyPair = AsymmetricKeyGenerator.createAsymmetricKeyPair(AsymmetricKeyPairAlgorithm.RSA2048_SHA256);
String jwkFile = TempFileWriter.write(new JWKGenerator().generateJWK(asymmetricKeyPair));
String jwt = new JWTGenerator(asymmetricKeyPair).generateJWT();
controlPlaneJWTAuthenticationJWKSource(jwkFile);
controlPlaneJWTAuthenticationRequired(true);
controlPlaneJWTSupplier = () -> jwt;
mockServerClient = ClientAndServer.startClientAndServer().withControlPlaneJWT(controlPlaneJWTSupplier).withSecure(true);
}
Aggregations