CONNECTSamlAssertionValidator extends SamlAssertionValidator { public void checkAttributes(SamlAssertionWrapper assertion) throws WSSecurityException { Assertion saml2Assertion = assertion.getSaml2(); Set<String> foundAttr = new HashSet<>(); for (AttributeStatement statement : saml2Assertion.getAttributeStatements()) { for (Attribute attribute : statement.getAttributes()) { if (CollectionUtils.isNotEmpty(attribute.getAttributeValues()) && attribute.getAttributeValues().get(0) != null && isAttributeValueValid(attribute.getAttributeValues().get(0), attribute.getName())) { foundAttr.add(attribute.getName()); } } } Set<String> missingAttrs = new HashSet<>(VALIDATED_ATTRIBUTES); missingAttrs.removeAll(foundAttr); if (CollectionUtils.isNotEmpty(missingAttrs)) { LOG.error("Missing Required Attributes: {}", missingAttrs); throw new WSSecurityException(ErrorCode.FAILED_CHECK, "missingRequiredSAMLAttribute"); } } CONNECTSamlAssertionValidator();  CONNECTSamlAssertionValidator(final PropertyAccessor propertyAccessor); @Override Credential validate(final Credential credential, final RequestData data); void checkAttributes(SamlAssertionWrapper assertion);  }
@Test(expected = WSSecurityException.class) public void testValidateMissingAttributesFail() throws WSSecurityException { final org.opensaml.saml.saml2.core.Assertion saml2Assertion = mock(org.opensaml.saml.saml2.core.Assertion.class); final SamlAssertionWrapper assertionWrapper = new SamlAssertionWrapper(saml2Assertion); final XMLObjectBuilderFactory builderFactory = XMLObjectProviderRegistrySupport.getBuilderFactory(); final SAMLObjectBuilder<AttributeStatement> attributeStatementBuilder = (SAMLObjectBuilder<AttributeStatement>) builderFactory .getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME); List<Object> values = new ArrayList<>(); values.add("value"); Set<String> partialList = new HashSet<>( Arrays.asList(SamlConstants.ATTRIBUTE_NAME_ORG, SamlConstants.ATTRIBUTE_NAME_ORG_ID)); List<Attribute> attributes = new ArrayList<>(); for (String name : partialList) { Attribute attr = SAML2ComponentBuilder.createAttribute("", name, "nameFormat", values); attributes.add(attr); } final AttributeStatement attributeStatement = attributeStatementBuilder.buildObject(); attributeStatement.getAttributes().addAll(attributes); List<AttributeStatement> statements = new ArrayList<>(); statements.add(attributeStatement); when(saml2Assertion.getAttributeStatements()).thenReturn(statements); CONNECTSamlAssertionValidator validator = new CONNECTSamlAssertionValidator(); validator.checkAttributes(assertionWrapper); }
@Test(expected = WSSecurityException.class) public void testValidateAttributes_AttributeMissingStringValue() throws WSSecurityException { final org.opensaml.saml.saml2.core.Assertion saml2Assertion = mock(org.opensaml.saml.saml2.core.Assertion.class); final SamlAssertionWrapper assertionWrapper = new SamlAssertionWrapper(saml2Assertion); final XMLObjectBuilderFactory builderFactory = XMLObjectProviderRegistrySupport.getBuilderFactory(); final SAMLObjectBuilder<AttributeStatement> attributeStatementBuilder = (SAMLObjectBuilder<AttributeStatement>) builderFactory .getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME); List<Object> values = new ArrayList<>(); values.add("value"); List<Attribute> attributes = new ArrayList<>(); for (String name : VALIDATED_ATTRIBUTES) { Attribute attr; if (name.equals(SamlConstants.ATTRIBUTE_NAME_HCID)) { attr = SAML2ComponentBuilder.createAttribute("", name, "nameFormat", new ArrayList<>()); } else { attr = SAML2ComponentBuilder.createAttribute("", name, "nameFormat", values); } attributes.add(attr); } final AttributeStatement attributeStatement = attributeStatementBuilder.buildObject(); attributeStatement.getAttributes().addAll(attributes); List<AttributeStatement> statements = new ArrayList<>(); statements.add(attributeStatement); when(saml2Assertion.getAttributeStatements()).thenReturn(statements); CONNECTSamlAssertionValidator validator = new CONNECTSamlAssertionValidator(); validator.checkAttributes(assertionWrapper); }
@Test(expected = WSSecurityException.class) public void testValidateAttributes_AttributeMissingXMLObjectValue() throws WSSecurityException { final org.opensaml.saml.saml2.core.Assertion saml2Assertion = mock(org.opensaml.saml.saml2.core.Assertion.class); final SamlAssertionWrapper assertionWrapper = new SamlAssertionWrapper(saml2Assertion); final XMLObjectBuilderFactory builderFactory = XMLObjectProviderRegistrySupport.getBuilderFactory(); final SAMLObjectBuilder<AttributeStatement> attributeStatementBuilder = (SAMLObjectBuilder<AttributeStatement>) builderFactory .getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME); List<Object> values = new ArrayList<>(); values.add("value"); List<Attribute> attributes = new ArrayList<>(); for (String name : VALIDATED_ATTRIBUTES) { Attribute attr; if (name.equals(SamlConstants.ATTRIBUTE_NAME_PURPOSE_OF_USE)) { attr = OpenSAML2ComponentBuilder.getInstance().createPurposeOfUseAttribute(null, null, null, null); } else { attr = SAML2ComponentBuilder.createAttribute("", name, "nameFormat", values); } attributes.add(attr); } final AttributeStatement attributeStatement = attributeStatementBuilder.buildObject(); attributeStatement.getAttributes().addAll(attributes); List<AttributeStatement> statements = new ArrayList<>(); statements.add(attributeStatement); when(saml2Assertion.getAttributeStatements()).thenReturn(statements); CONNECTSamlAssertionValidator validator = new CONNECTSamlAssertionValidator(); validator.checkAttributes(assertionWrapper); }
@Test public void testValidateAttributesAnyType() throws WSSecurityException { final org.opensaml.saml.saml2.core.Assertion saml2Assertion = mock(org.opensaml.saml.saml2.core.Assertion.class); final SamlAssertionWrapper assertionWrapper = new SamlAssertionWrapper(saml2Assertion); final XMLObjectBuilderFactory builderFactory = XMLObjectProviderRegistrySupport.getBuilderFactory(); final SAMLObjectBuilder<AttributeStatement> attributeStatementBuilder = (SAMLObjectBuilder<AttributeStatement>) builderFactory .getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME); final XSAnyImpl anyType = mock(XSAnyImpl.class); List<Object> values = new ArrayList<>(); values.add("value"); List<Attribute> attributes = new ArrayList<>(); for (String name : VALIDATED_ATTRIBUTES) { Attribute attr = SAML2ComponentBuilder.createAttribute("", name, "nameFormat", values); if (name.equals(SamlConstants.ATTRIBUTE_NAME_ORG)) { attr.getAttributeValues().clear(); attr.getAttributeValues().add(anyType); } attributes.add(attr); } final AttributeStatement attributeStatement = attributeStatementBuilder.buildObject(); attributeStatement.getAttributes().addAll(attributes); List<AttributeStatement> statements = new ArrayList<>(); statements.add(attributeStatement); when(saml2Assertion.getAttributeStatements()).thenReturn(statements); when(anyType.getTextContent()).thenReturn("value"); CONNECTSamlAssertionValidator validator = new CONNECTSamlAssertionValidator(); validator.checkAttributes(assertionWrapper); }