-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Getting response attributes from Saml2AuthenticatedPrincipal #8667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,11 @@ | |
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectOutputStream; | ||
import java.time.Instant; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.hamcrest.BaseMatcher; | ||
import org.hamcrest.Description; | ||
|
@@ -39,6 +43,7 @@ | |
import org.springframework.security.saml2.credentials.Saml2X509Credential; | ||
|
||
import static org.springframework.security.saml2.provider.service.authentication.TestOpenSamlObjects.assertion; | ||
import static org.springframework.security.saml2.provider.service.authentication.TestOpenSamlObjects.attributeStatements; | ||
import static org.springframework.security.saml2.provider.service.authentication.TestOpenSamlObjects.encrypted; | ||
import static org.springframework.security.saml2.provider.service.authentication.TestOpenSamlObjects.response; | ||
import static org.springframework.security.saml2.provider.service.authentication.TestOpenSamlObjects.signed; | ||
|
@@ -47,6 +52,7 @@ | |
import static org.springframework.security.saml2.credentials.TestSaml2X509Credentials.assertingPartySigningCredential; | ||
import static org.springframework.security.saml2.credentials.TestSaml2X509Credentials.relyingPartyDecryptingCredential; | ||
import static org.springframework.security.saml2.credentials.TestSaml2X509Credentials.relyingPartyVerifyingCredential; | ||
import static org.springframework.test.util.AssertionErrors.assertEquals; | ||
import static org.springframework.test.util.AssertionErrors.assertTrue; | ||
import static org.springframework.util.StringUtils.hasText; | ||
|
||
|
@@ -193,6 +199,30 @@ public void authenticateWhenAssertionContainsValidationAddressThenItSucceeds() t | |
this.provider.authenticate(token); | ||
} | ||
|
||
@Test | ||
public void authenticateWhenAssertionContainsAttributesThenItSucceeds() { | ||
Response response = response(); | ||
Assertion assertion = assertion(); | ||
attributeStatements().forEach(as -> assertion.getAttributeStatements().add(as)); | ||
signed(assertion, assertingPartySigningCredential(), RELYING_PARTY_ENTITY_ID); | ||
response.getAssertions().add(assertion); | ||
Saml2AuthenticationToken token = token(response, relyingPartyVerifyingCredential()); | ||
Authentication authentication = this.provider.authenticate(token); | ||
Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal) authentication.getPrincipal(); | ||
|
||
Map<String, Object> attributes = new LinkedHashMap<>(); | ||
attributes.put("email", Arrays.asList("[email protected]", "[email protected]")); | ||
attributes.put("name", Collections.singletonList("John Doe")); | ||
attributes.put("age", Collections.singletonList(21)); | ||
attributes.put("website", Collections.singletonList("https://johndoe.com/")); | ||
attributes.put("registered", Collections.singletonList(true)); | ||
Instant registeredDate = Instant.ofEpochMilli(DateTime.parse("1970-01-01T00:00:00Z").getMillis()); | ||
attributes.put("registeredDate", Collections.singletonList(registeredDate)); | ||
|
||
assertEquals("Values should be equal", "John Doe", principal.getFirstAttribute("name")); | ||
assertTrue("Attributes should be equal", attributes.equals(principal.getAttributes())); | ||
} | ||
|
||
@Test | ||
public void authenticateWhenEncryptedAssertionWithoutSignatureThenItFails() throws Exception { | ||
this.exception.expect(authenticationMatcher(Saml2ErrorCodes.INVALID_SIGNATURE)); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* Copyright 2002-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -16,15 +16,58 @@ | |
|
||
package org.springframework.security.saml2.provider.service.authentication; | ||
|
||
import org.junit.Assert; | ||
import org.joda.time.DateTime; | ||
import org.junit.Test; | ||
|
||
import java.time.Instant; | ||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class SimpleSaml2AuthenticatedPrincipalTests { | ||
|
||
@Test | ||
public void createSimpleSaml2AuthenticatedPrincipal() { | ||
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user"); | ||
Map<String, List<Object>> attributes = new LinkedHashMap<>(); | ||
attributes.put("email", Arrays.asList("[email protected]", "[email protected]")); | ||
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user", attributes); | ||
assertThat(principal.getName()).isEqualTo("user"); | ||
assertThat(principal.getAttributes()).isEqualTo(attributes); | ||
} | ||
|
||
@Test | ||
public void getFirstAttributeWhenStringValueThenReturnsValue() { | ||
Map<String, List<Object>> attributes = new LinkedHashMap<>(); | ||
attributes.put("email", Arrays.asList("[email protected]", "[email protected]")); | ||
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user", attributes); | ||
assertThat(principal.<String>getFirstAttribute("email")).isEqualTo(attributes.get("email").get(0)); | ||
} | ||
|
||
@Test | ||
public void getAttributeWhenStringValuesThenReturnsValues() { | ||
Map<String, List<Object>> attributes = new LinkedHashMap<>(); | ||
attributes.put("email", Arrays.asList("[email protected]", "[email protected]")); | ||
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user", attributes); | ||
assertThat(principal.<String>getAttribute("email")).isEqualTo(attributes.get("email")); | ||
} | ||
|
||
@Test | ||
public void getAttributeWhenDistinctValuesThenReturnsValues() { | ||
final Boolean registered = true; | ||
final Instant registeredDate = Instant.ofEpochMilli(DateTime.parse("1970-01-01T00:00:00Z").getMillis()); | ||
|
||
Map<String, List<Object>> attributes = new LinkedHashMap<>(); | ||
attributes.put("registration", Arrays.asList(registered, registeredDate)); | ||
|
||
SimpleSaml2AuthenticatedPrincipal principal = new SimpleSaml2AuthenticatedPrincipal("user", attributes); | ||
|
||
List<Object> registrationInfo = principal.getAttribute("registration"); | ||
|
||
Assert.assertEquals("user", principal.getName()); | ||
assertThat(registrationInfo).isNotNull(); | ||
assertThat((Boolean) registrationInfo.get(0)).isEqualTo(registered); | ||
assertThat((Instant) registrationInfo.get(1)).isEqualTo(registeredDate); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.