|
51 | 51 | import org.opensaml.saml.saml2.core.EncryptedAssertion;
|
52 | 52 | import org.opensaml.saml.saml2.core.EncryptedAttribute;
|
53 | 53 | import org.opensaml.saml.saml2.core.EncryptedID;
|
| 54 | +import org.opensaml.saml.saml2.core.Issuer; |
54 | 55 | import org.opensaml.saml.saml2.core.NameID;
|
55 | 56 | import org.opensaml.saml.saml2.core.OneTimeUse;
|
56 | 57 | import org.opensaml.saml.saml2.core.Response;
|
| 58 | +import org.opensaml.saml.saml2.core.Status; |
57 | 59 | import org.opensaml.saml.saml2.core.StatusCode;
|
58 | 60 | import org.opensaml.saml.saml2.core.SubjectConfirmation;
|
59 | 61 | import org.opensaml.saml.saml2.core.SubjectConfirmationData;
|
60 | 62 | import org.opensaml.saml.saml2.core.impl.AttributeBuilder;
|
61 | 63 | import org.opensaml.saml.saml2.core.impl.EncryptedAssertionBuilder;
|
62 | 64 | import org.opensaml.saml.saml2.core.impl.EncryptedIDBuilder;
|
63 | 65 | import org.opensaml.saml.saml2.core.impl.NameIDBuilder;
|
| 66 | +import org.opensaml.saml.saml2.core.impl.StatusBuilder; |
| 67 | +import org.opensaml.saml.saml2.core.impl.StatusCodeBuilder; |
64 | 68 | import org.opensaml.xmlsec.encryption.impl.EncryptedDataBuilder;
|
65 | 69 | import org.opensaml.xmlsec.signature.support.SignatureConstants;
|
66 | 70 | import org.w3c.dom.Element;
|
|
82 | 86 | import static org.assertj.core.api.Assertions.assertThat;
|
83 | 87 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
84 | 88 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 89 | +import static org.junit.Assert.assertFalse; |
| 90 | +import static org.junit.Assert.assertTrue; |
85 | 91 | import static org.mockito.ArgumentMatchers.any;
|
86 | 92 | import static org.mockito.BDDMockito.given;
|
87 | 93 | import static org.mockito.Mockito.atLeastOnce;
|
@@ -729,6 +735,77 @@ public void authenticateWhenCustomResponseValidatorThenUses() {
|
729 | 735 | verify(validator).convert(any(OpenSaml4AuthenticationProvider.ResponseToken.class));
|
730 | 736 | }
|
731 | 737 |
|
| 738 | + @Test |
| 739 | + public void setsOnlyParentStatusCodeOnResultDescription() { |
| 740 | + ResponseToken mockResponseToken = mock(ResponseToken.class); |
| 741 | + Saml2AuthenticationToken mockSamlToken = mock(Saml2AuthenticationToken.class); |
| 742 | + given(mockResponseToken.getToken()).willReturn(mockSamlToken); |
| 743 | + |
| 744 | + RelyingPartyRegistration mockRelyingPartyRegistration = mock(RelyingPartyRegistration.class); |
| 745 | + given(mockSamlToken.getRelyingPartyRegistration()).willReturn(mockRelyingPartyRegistration); |
| 746 | + |
| 747 | + RelyingPartyRegistration.AssertingPartyDetails mockAssertingPartyDetails = mock(RelyingPartyRegistration.AssertingPartyDetails.class); |
| 748 | + given(mockRelyingPartyRegistration.getAssertingPartyDetails()).willReturn(mockAssertingPartyDetails); |
| 749 | + |
| 750 | + Status parentStatus = new StatusBuilder().buildObject(); |
| 751 | + StatusCode parentStatusCode = new StatusCodeBuilder().buildObject(); |
| 752 | + parentStatusCode.setValue(StatusCode.AUTHN_FAILED); |
| 753 | + StatusCode childStatusCode = new StatusCodeBuilder().buildObject(); |
| 754 | + childStatusCode.setValue(StatusCode.NO_PASSIVE); |
| 755 | + parentStatusCode.setStatusCode(childStatusCode); |
| 756 | + parentStatus.setStatusCode(parentStatusCode); |
| 757 | + |
| 758 | + Response mockResponse = mock(Response.class); |
| 759 | + given(mockResponse.getStatus()).willReturn(parentStatus); |
| 760 | + Issuer mockIssuer = mock(Issuer.class); |
| 761 | + given(mockIssuer.getValue()).willReturn("mockedIssuer"); |
| 762 | + given(mockResponse.getIssuer()).willReturn(mockIssuer); |
| 763 | + |
| 764 | + given(mockResponseToken.getResponse()).willReturn(mockResponse); |
| 765 | + |
| 766 | + Converter<ResponseToken, Saml2ResponseValidatorResult> validator = OpenSaml4AuthenticationProvider.createDefaultResponseValidator(); |
| 767 | + Saml2ResponseValidatorResult result = validator.convert(mockResponseToken); |
| 768 | + |
| 769 | + String expectedErrorMessage = String.format("Invalid status [%s] for SAML response", parentStatusCode.getValue()); |
| 770 | + assertTrue(result.getErrors().stream().anyMatch(error -> error.getDescription().contains(expectedErrorMessage))); |
| 771 | + assertFalse(result.getErrors().stream().anyMatch(error -> error.getDescription().contains(childStatusCode.getValue()))); |
| 772 | + } |
| 773 | + |
| 774 | + @Test |
| 775 | + public void setsParentAndChildStatusCodeOnResultDescription() { |
| 776 | + ResponseToken mockResponseToken = mock(ResponseToken.class); |
| 777 | + Saml2AuthenticationToken mockSamlToken = mock(Saml2AuthenticationToken.class); |
| 778 | + given(mockResponseToken.getToken()).willReturn(mockSamlToken); |
| 779 | + |
| 780 | + RelyingPartyRegistration mockRelyingPartyRegistration = mock(RelyingPartyRegistration.class); |
| 781 | + given(mockSamlToken.getRelyingPartyRegistration()).willReturn(mockRelyingPartyRegistration); |
| 782 | + |
| 783 | + RelyingPartyRegistration.AssertingPartyDetails mockAssertingPartyDetails = mock(RelyingPartyRegistration.AssertingPartyDetails.class); |
| 784 | + given(mockRelyingPartyRegistration.getAssertingPartyDetails()).willReturn(mockAssertingPartyDetails); |
| 785 | + |
| 786 | + Status parentStatus = new StatusBuilder().buildObject(); |
| 787 | + StatusCode parentStatusCode = new StatusCodeBuilder().buildObject(); |
| 788 | + parentStatusCode.setValue(StatusCode.REQUESTER); |
| 789 | + StatusCode childStatusCode = new StatusCodeBuilder().buildObject(); |
| 790 | + childStatusCode.setValue(StatusCode.NO_PASSIVE); |
| 791 | + parentStatusCode.setStatusCode(childStatusCode); |
| 792 | + parentStatus.setStatusCode(parentStatusCode); |
| 793 | + |
| 794 | + Response mockResponse = mock(Response.class); |
| 795 | + given(mockResponse.getStatus()).willReturn(parentStatus); |
| 796 | + Issuer mockIssuer = mock(Issuer.class); |
| 797 | + given(mockIssuer.getValue()).willReturn("mockedIssuer"); |
| 798 | + given(mockResponse.getIssuer()).willReturn(mockIssuer); |
| 799 | + |
| 800 | + given(mockResponseToken.getResponse()).willReturn(mockResponse); |
| 801 | + |
| 802 | + Converter<ResponseToken, Saml2ResponseValidatorResult> validator = OpenSaml4AuthenticationProvider.createDefaultResponseValidator(); |
| 803 | + Saml2ResponseValidatorResult result = validator.convert(mockResponseToken); |
| 804 | + |
| 805 | + String expectedErrorMessage = String.format("Invalid status [%s] for SAML response", parentStatusCode.getValue() + childStatusCode.getValue()); |
| 806 | + assertTrue(result.getErrors().stream().anyMatch(error -> error.getDescription().contains(expectedErrorMessage))); |
| 807 | + } |
| 808 | + |
732 | 809 | @Test
|
733 | 810 | public void authenticateWhenAssertionIssuerNotValidThenFailsWithInvalidIssuer() {
|
734 | 811 | OpenSaml4AuthenticationProvider provider = new OpenSaml4AuthenticationProvider();
|
|
0 commit comments