use of org.opensaml.core.xml.XMLObject in project cxf by apache.
the class SAMLProviderActAsTest method testDefaultSaml1ActAsUsernameToken.
/**
* Create a default Saml1 Bearer Assertion with ActAs from a UsernameToken
*/
@org.junit.Test
public void testDefaultSaml1ActAsUsernameToken() throws Exception {
TokenProvider samlTokenProvider = new SAMLTokenProvider();
UsernameTokenType usernameToken = new UsernameTokenType();
AttributedString username = new AttributedString();
username.setValue("bob");
usernameToken.setUsername(username);
JAXBElement<UsernameTokenType> usernameTokenType = new JAXBElement<UsernameTokenType>(QNameConstants.USERNAME_TOKEN, UsernameTokenType.class, usernameToken);
TokenProviderParameters providerParameters = createProviderParameters(WSS4JConstants.WSS_SAML_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE, usernameTokenType);
// Principal must be set in ReceivedToken/ActAs
providerParameters.getTokenRequirements().getActAs().setPrincipal(new CustomTokenPrincipal(username.getValue()));
assertTrue(samlTokenProvider.canHandleToken(WSS4JConstants.WSS_SAML_TOKEN_TYPE));
TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters);
assertNotNull(providerResponse);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
// Verify the token
Element token = (Element) providerResponse.getToken();
SamlAssertionWrapper assertion = new SamlAssertionWrapper(token);
Assert.assertEquals("technical-user", assertion.getSubjectName());
boolean foundActAsAttribute = false;
for (org.opensaml.saml.saml1.core.AttributeStatement attributeStatement : assertion.getSaml1().getAttributeStatements()) {
for (org.opensaml.saml.saml1.core.Attribute attribute : attributeStatement.getAttributes()) {
if ("ActAs".equals(attribute.getAttributeName())) {
for (XMLObject attributeValue : attribute.getAttributeValues()) {
Element attributeValueElement = attributeValue.getDOM();
String text = attributeValueElement.getTextContent();
if (text.contains("bob")) {
foundActAsAttribute = true;
break;
}
}
}
}
}
assertTrue(foundActAsAttribute);
}
use of org.opensaml.core.xml.XMLObject in project verify-hub by alphagov.
the class AuthnRequestFactory method anInvalidAuthnRequest.
public String anInvalidAuthnRequest(String id, String issuer, Optional<Boolean> forceAuthentication, Optional<URI> assertionConsumerServiceUrl, Optional<Integer> assertionConsumerServiceIndex, String publicCert, String privateKey, String ssoRequestEndpoint, Optional<DateTime> issueInstant) {
// Pad ID to ensure request is long enough
AuthnRequest authnRequest = getAuthnRequest(StringUtils.rightPad(id, 1200, "x"), issuer, forceAuthentication, assertionConsumerServiceUrl, assertionConsumerServiceIndex, publicCert, privateKey, ssoRequestEndpoint, issueInstant);
authnRequest.setSignature(null);
// Use a different transformer to ensure that no Signature elements are added
XmlObjectToBase64EncodedStringTransformer<XMLObject> transformer = new XmlObjectToBase64EncodedStringTransformer<>();
return transformer.apply(authnRequest);
}
use of org.opensaml.core.xml.XMLObject in project verify-hub by alphagov.
the class HubAttributeQueryRequestToSamlAttributeQueryTransformerTest method transform_shouldOnlyIncludeCycle3Unencrypted.
@Test
public void transform_shouldOnlyIncludeCycle3Unencrypted() {
final HubAssertion cycle3DataAssertion = aHubAssertion().build();
HubAttributeQueryRequest originalQuery = aHubAttributeQueryRequest().withCycle3DataAssertion(cycle3DataAssertion).build();
AttributeQuery transformedQuery = transformer.apply(originalQuery);
List<XMLObject> unknownXMLObjects = transformedQuery.getSubject().getSubjectConfirmations().get(0).getSubjectConfirmationData().getUnknownXMLObjects(Assertion.DEFAULT_ELEMENT_NAME);
assertThat(unknownXMLObjects.size()).isEqualTo(1);
assertThat(((Assertion) unknownXMLObjects.get(0)).getID()).isEqualTo(cycle3DataAssertion.getId());
}
use of org.opensaml.core.xml.XMLObject in project verify-hub by alphagov.
the class HubAttributeQueryRequestToSamlAttributeQueryTransformerTest method transform_shouldContainBothMdsAndAuthnAssertionsEncrypted.
@Test
public void transform_shouldContainBothMdsAndAuthnAssertionsEncrypted() {
HubAttributeQueryRequest originalQuery = aHubAttributeQueryRequest().withEncryptedMatchingDatasetAssertion(ENCRYPTED_MDS_ASSERTION).withEncryptedAuthnAssertion(ENCRYPTED_AUTHN_ASSERTION).build();
final EncryptedAssertion value1 = new EncryptedAssertionBuilder().buildObject();
final EncryptedAssertion value2 = new EncryptedAssertionBuilder().buildObject();
when(encryptedAssertionUnmarshaller.transform(ENCRYPTED_MDS_ASSERTION)).thenReturn(value1);
when(encryptedAssertionUnmarshaller.transform(ENCRYPTED_AUTHN_ASSERTION)).thenReturn(value2);
AttributeQuery transformedQuery = transformer.apply(originalQuery);
List<XMLObject> encryptedAssertions = transformedQuery.getSubject().getSubjectConfirmations().get(0).getSubjectConfirmationData().getUnknownXMLObjects(EncryptedAssertion.DEFAULT_ELEMENT_NAME);
assertThat(encryptedAssertions.size()).isEqualTo(2);
assertThat(encryptedAssertions).contains(value1, value2);
}
use of org.opensaml.core.xml.XMLObject in project verify-hub by alphagov.
the class MatchingDatasetAssertionValidator method validateAttribute.
private void validateAttribute(Attribute attribute) {
String attributeName = attribute.getName();
if (!VALID_ATTRIBUTE_NAMES_1_1.contains(attributeName))
throw new SamlValidationException(mdsAttributeNotRecognised(attributeName));
List<XMLObject> attributeValues = attribute.getAttributeValues();
if (attributeValues.isEmpty())
throw new SamlValidationException(emptyAttribute(attributeName));
QName schemaType = attributeValues.get(0).getSchemaType();
if (!VALID_TYPE_FOR_ATTRIBUTE.get(attributeName).equals(schemaType))
throw new SamlValidationException(attributeWithIncorrectType(attributeName, VALID_TYPE_FOR_ATTRIBUTE.get(attributeName), schemaType));
if (!VALID_ATTRIBUTE_NAME_FORMATS.contains(attribute.getNameFormat()))
warn(invalidAttributeNameFormat(attribute.getNameFormat()));
}
Aggregations