From 2c1a5f59332ac2a67fc68ae75ad24458446bed4a Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Fri, 20 Dec 2024 16:44:06 +0100 Subject: [PATCH] fetch username from CAS validation response --- .../hostsharing/hsadminng/config/CasAuthenticator.java | 5 +++-- .../config/CasAuthenticationFilterIntegrationTest.java | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) 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