diff --git a/src/main/java/net/hostsharing/hsadminng/config/CasAuthenticator.java b/src/main/java/net/hostsharing/hsadminng/config/CasAuthenticator.java index 278b5470..aa580fbf 100644 --- a/src/main/java/net/hostsharing/hsadminng/config/CasAuthenticator.java +++ b/src/main/java/net/hostsharing/hsadminng/config/CasAuthenticator.java @@ -31,12 +31,13 @@ public class CasAuthenticator implements Authenticator { final var doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new java.io.ByteArrayInputStream(response.getBytes())); - if ( doc.getElementsByTagName("cas:authenticationSuccess").getLength() == 0 ) { + if (doc.getElementsByTagName("cas:authenticationSuccess").getLength() == 0) { // TODO.impl: for unknown reasons, this results in a 403 FORBIDDEN // throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "CAS service ticket could not be validated"); throw new BadCredentialsException("CAS service ticket could not be validated"); } - final var authentication = new UsernamePasswordAuthenticationToken("test-user-from-authenticate", null, null); // TODO + final var userName = doc.getElementsByTagName("cas:user").item(0).getTextContent(); + final var authentication = new UsernamePasswordAuthenticationToken(userName, null, null); SecurityContextHolder.getContext().setAuthentication(authentication); return authentication.getName(); } diff --git a/src/test/java/net/hostsharing/hsadminng/config/CasAuthenticationFilterIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/config/CasAuthenticationFilterIntegrationTest.java index 3cb13840..c470ba4a 100644 --- a/src/test/java/net/hostsharing/hsadminng/config/CasAuthenticationFilterIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/config/CasAuthenticationFilterIntegrationTest.java @@ -13,7 +13,7 @@ import org.springframework.http.HttpStatus; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestPropertySource; - +import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static com.github.tomakehurst.wiremock.client.WireMock.*; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -33,16 +33,18 @@ class CasAuthenticationFilterIntegrationTest { @Test public void shouldAcceptRequest() { // given + final var username = "test-user-" + randomAlphanumeric(4); wireMockServer.stubFor(get(urlEqualTo("/cas/p3/serviceValidate?service=http://localhost:8080/api&ticket=valid")) .willReturn(aResponse() .withStatus(200) .withBody(""" - test-user + %{username} - """))); + """.replace("%{username}", username) + ))); // when final var result = restTemplate.exchange( @@ -54,7 +56,7 @@ class CasAuthenticationFilterIntegrationTest { // then assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(result.getBody()).isEqualTo("pong test-user-from-authenticate\n"); + assertThat(result.getBody()).isEqualTo("pong " + username + "\n"); } @Test