add CAS authentication #138

Merged
hsh-michaelhoennig merged 24 commits from feature/add-cas-authentication into master 2024-12-23 12:49:46 +01:00
2 changed files with 9 additions and 6 deletions
Showing only changes of commit 2c1a5f5933 - Show all commits

View File

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

View File

@ -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("""
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:authenticationSuccess>
<cas:user>test-user</cas:user>
<cas:user>%{username}</cas:user>
</cas:authenticationSuccess>
</cas:serviceResponse>
""")));
""".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