How Do I Use WireMock OIDC Server to pass a custom crafted JWT? #28566
-
I am working on switching to using Quarkus OIDC when we had previously used oauth2-proxy and passed headers/tokens through to the API server. In our existing integration-tests we would just craft JWTs and headers and pass them into the RestAssured calls, but now we would like to use WireMock OIDC Server to achieve a similar goal... In one of my Rest controllers I have a method like: @Inject
@IdToken
JsonWebToken idToken;
String authenticateUsername() {
Optional<String> preferredUsername = idToken.claim("preferred_username");
if (preferredUsername.isEmpty()) {
preferredUsername = idToken.getClaim("email");
}
return preferredUsername.get().split("@")[0];
} And in one of my tests, I am trying to use the following: @QuarkusTest
@TestHTTPEndpoint(UserprofileApiController.class)
@QuarkusTestResource(OidcWiremockTestResource.class)
class UserprofileApiControllerIT {
@InjectMock
EmployeeRepository repository;
String getAccessToken(String username) {
return Jwt.preferredUserName(username).claim("preferred_username", username)
.claim("email", format("%[email protected]", username)).issuer("https://server.example.com")
.audience("https://service.example.com").sign();
}
@Test
public void verifyUserIsReturnedWhenAuthenticated() throws Exception {
String mockUserName = "jqconsultant";
EmployeeEntity mockEmployee = (EmployeeEntity) new EmployeeEntity().email("[email protected]")
.id("jqconsultant").role("Senior Consultant").name("John Q. Consultant");
when(repository.findById(matches(mockUserName))).thenReturn((mockEmployee));
String requestBody = "{\"name\": \"John Q. Consultant\", \"id\": \"jqconsultant\", \"email\": \"[email protected]\", \"role\": \"Senior Consultant\"}";
given()
.body(requestBody)
.auth().oauth2(getAccessToken("jqconsultant"))
.when()
.get()
.then()
.statusCode(200)
.body("email", Matchers.equalTo("[email protected]"))
.body("id", Matchers.equalTo("jqconsultant"))
.body("role", Matchers.equalTo("Senior Consultant"))
.body("name", Matchers.equalTo("John Q. Consultant"));
}
} This does NOT appear to work and instead of a 200 OK, I get a 500 response because of a NullPointerException. Any suggestions on what I might be doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
/cc @pedroigor, @sberyozkin |
Beta Was this translation helpful? Give feedback.
-
Hi @InfoSec812 I see, so Does it help ? |
Beta Was this translation helpful? Give feedback.
-
It looks like I found the answer.
This appears to be working and I can parameterize the oauth2 token by changing the |
Beta Was this translation helpful? Give feedback.
It looks like I found the answer.
@QuarkusTestResource(OidcWiremockTestR…