Skip to content

Commit a524e40

Browse files
author
m.horcicko
committed
spring-projects#7722: Saml2WebSsoAuthenticationFilter now sets the authentication details with AbstractAuthenticationProcessingFilter 'authenticationDetailsSource'
1 parent 73babc3 commit a524e40

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ public Authentication authenticate(Authentication authentication) throws Authent
177177
Response samlResponse = getSaml2Response(token);
178178
Assertion assertion = validateSaml2Response(token, token.getRecipientUri(), samlResponse);
179179
String username = getUsername(token, assertion);
180-
return new Saml2Authentication(
180+
Saml2Authentication saml2Authentication = new Saml2Authentication(
181181
() -> username, token.getSaml2Response(),
182182
this.authoritiesMapper.mapAuthorities(getAssertionAuthorities(assertion))
183183
);
184+
saml2Authentication.setDetails(authentication.getDetails());
185+
return saml2Authentication;
184186
} catch (Saml2AuthenticationException e) {
185187
throw e;
186188
} catch (Exception e) {

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/servlet/filter/Saml2WebSsoAuthenticationFilter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
8686
localSpEntityId,
8787
rp.getCredentials()
8888
);
89+
authentication.setDetails(authenticationDetailsSource.buildDetails(request));
8990
return getAuthenticationManager().authenticate(authentication);
9091
}
9192

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationProviderTests.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import static org.springframework.security.saml2.provider.service.authentication.Saml2CryptoTestSupport.signXmlObject;
4242
import static org.springframework.security.saml2.provider.service.authentication.TestSaml2X509Credentials.assertingPartyCredentials;
4343
import static org.springframework.security.saml2.provider.service.authentication.TestSaml2X509Credentials.relyingPartyCredentials;
44+
import static org.springframework.test.util.AssertionErrors.assertEquals;
4445
import static org.springframework.test.util.AssertionErrors.assertTrue;
4546
import static org.springframework.util.StringUtils.hasText;
4647

@@ -171,7 +172,7 @@ public void authenticateWhenOpenSAMLValidationErrorThenThrowAuthenticationExcept
171172
}
172173

173174
@Test
174-
public void authenticateWhenMissingSubjectThenThrowAuthenticationException() {
175+
public void authenticateWhenMissingSubjectThenThrowAuthenticationException() {
175176
Response response = response(recipientUri, idpEntityId);
176177
Assertion assertion = defaultAssertion();
177178
assertion.setSubject(null);
@@ -346,6 +347,26 @@ public void authenticateWhenDecryptionKeysAreWrongThenThrowAuthenticationExcepti
346347
provider.authenticate(token);
347348
}
348349

350+
@Test
351+
public void authenticateAlsoPassAlongTheAuthenticationDetailsFromAuthenticationFilter() {
352+
Response response = response(recipientUri, idpEntityId);
353+
Assertion assertion = defaultAssertion();
354+
signXmlObject(
355+
assertion,
356+
assertingPartyCredentials(),
357+
recipientEntityId
358+
);
359+
EncryptedAssertion encryptedAssertion = encryptAssertion(assertion, assertingPartyCredentials());
360+
response.getEncryptedAssertions().add(encryptedAssertion);
361+
token = responseXml(response, idpEntityId);
362+
token.setDetails("details");
363+
Authentication authentication = provider.authenticate(token);
364+
assertEquals(
365+
OpenSamlAuthenticationProvider.class + " should pass authentication details along",
366+
"details", authentication.getDetails()
367+
);
368+
}
369+
349370
private Assertion defaultAssertion() {
350371
return assertion(
351372
username,
@@ -400,7 +421,7 @@ public boolean matches(Object item) {
400421

401422
@Override
402423
public void describeTo(Description desc) {
403-
String excepting = "Saml2AuthenticationException[code="+code+"; description="+description+"]";
424+
String excepting = "Saml2AuthenticationException[code=" + code + "; description=" + description + "]";
404425
desc.appendText(excepting);
405426

406427
}

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/servlet/filter/Saml2WebSsoAuthenticationFilterTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@
2323
import org.junit.rules.ExpectedException;
2424
import org.springframework.mock.web.MockHttpServletRequest;
2525
import org.springframework.mock.web.MockHttpServletResponse;
26+
import org.springframework.security.authentication.AuthenticationManager;
27+
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
2628
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
2729

2830
import javax.servlet.http.HttpServletResponse;
31+
import java.util.Base64;
2932

33+
import static org.mockito.ArgumentMatchers.any;
34+
import static org.mockito.ArgumentMatchers.argThat;
35+
import static org.mockito.BDDMockito.given;
3036
import static org.mockito.Mockito.mock;
37+
import static org.mockito.Mockito.verify;
3138

3239
public class Saml2WebSsoAuthenticationFilterTests {
3340

3441
private Saml2WebSsoAuthenticationFilter filter;
3542
private RelyingPartyRegistrationRepository repository = mock(RelyingPartyRegistrationRepository.class);
43+
private AuthenticationManager manager = mock(AuthenticationManager.class);
3644
private MockHttpServletRequest request = new MockHttpServletRequest();
3745
private HttpServletResponse response = new MockHttpServletResponse();
3846

@@ -42,8 +50,9 @@ public class Saml2WebSsoAuthenticationFilterTests {
4250
@Before
4351
public void setup() {
4452
filter = new Saml2WebSsoAuthenticationFilter(repository);
53+
filter.setAuthenticationManager(manager);
4554
request.setPathInfo("/login/saml2/sso/idp-registration-id");
46-
request.setParameter("SAMLResponse", "xml-data-goes-here");
55+
request.setParameter("SAMLResponse", new String(Base64.getEncoder().encode("xml-data".getBytes())));
4756
}
4857

4958
@Test
@@ -71,5 +80,13 @@ public void requiresAuthenticationWhenCustomProcessingUrlThenReturnsTrue() {
7180
Assert.assertTrue(filter.requiresAuthentication(request, response));
7281
}
7382

83+
@Test
84+
public void attemptAuthenticationAlsoSetsAuthenticationDetails() {
85+
given(repository.findByRegistrationId(any())).willReturn(mock(RelyingPartyRegistration.class));
86+
filter.setAuthenticationDetailsSource((request) -> "details");
87+
filter.attemptAuthentication(request, response);
88+
verify(manager).authenticate(argThat(argument -> argument.getDetails() == "details"));
89+
}
90+
7491

7592
}

0 commit comments

Comments
 (0)