Beispiel #1
0
 @Test
 public void loadStaticContextFactories() {
   assertNotNull(XMLObjectProviderRegistrySupport.getParserPool());
   assertNotNull(XMLObjectProviderRegistrySupport.getBuilderFactory());
   assertNotNull(XMLObjectProviderRegistrySupport.getMarshallerFactory());
   assertNotNull(XMLObjectProviderRegistrySupport.getUnmarshallerFactory());
 }
  /** Test that an addition condition is not added if an assertion already contains one. */
  @Test
  public void testSingleAssertionWithExistingAudienceCondition() throws Exception {
    final SAMLObjectBuilder<AudienceRestrictionCondition> conditionBuilder =
        (SAMLObjectBuilder<AudienceRestrictionCondition>)
            XMLObjectProviderRegistrySupport.getBuilderFactory()
                .getBuilder(AudienceRestrictionCondition.DEFAULT_ELEMENT_NAME);
    final AudienceRestrictionCondition condition = conditionBuilder.buildObject();

    final SAMLObjectBuilder<Conditions> conditionsBuilder =
        (SAMLObjectBuilder<Conditions>)
            XMLObjectProviderRegistrySupport.getBuilderFactory()
                .getBuilder(Conditions.DEFAULT_ELEMENT_NAME);
    final Conditions conditions = conditionsBuilder.buildObject();
    conditions.getAudienceRestrictionConditions().add(condition);

    final Assertion assertion = SAML1ActionTestingSupport.buildAssertion();
    assertion.setConditions(conditions);

    final Response response = SAML1ActionTestingSupport.buildResponse();
    response.getAssertions().add(assertion);

    final ProfileRequestContext prc =
        new RequestContextBuilder().setOutboundMessage(response).buildProfileRequestContext();

    action.execute(prc);
    ActionTestingSupport.assertProceedEvent(prc);

    Assert.assertNotNull(assertion.getConditions());
    Assert.assertEquals(assertion.getConditions().getAudienceRestrictionConditions().size(), 1);
    final AudienceRestrictionCondition audcond =
        assertion.getConditions().getAudienceRestrictionConditions().get(0);
    Assert.assertEquals(audcond.getAudiences().size(), 2);
    Assert.assertEquals(audcond.getAudiences().get(0).getUri(), AUDIENCE1);
    Assert.assertEquals(audcond.getAudiences().get(1).getUri(), AUDIENCE2);
  }
 /** Constructor. */
 public SAML1NameIdentifierAttributeDefinition() {
   nameIdentifierBuilder =
       (SAMLObjectBuilder<NameIdentifier>)
           XMLObjectProviderRegistrySupport.getBuilderFactory()
               .<NameIdentifier>getBuilderOrThrow(NameIdentifier.DEFAULT_ELEMENT_NAME);
   nameIdFormat = NameIdentifier.UNSPECIFIED;
 }
  /** Tests unmarshalling a full response message. */
  @Test
  public void testResponseUnmarshall() {

    try {
      InputStream in = ResponseTest.class.getResourceAsStream(fullResponsePath);
      Document responseDoc = parserPool.parse(in);
      Unmarshaller unmarshaller =
          XMLObjectProviderRegistrySupport.getUnmarshallerFactory()
              .getUnmarshaller(responseDoc.getDocumentElement());

      Response response = (Response) unmarshaller.unmarshall(responseDoc.getDocumentElement());

      Assert.assertEquals(
          response.getElementQName().getLocalPart(),
          "Response",
          "First element of response data was not expected Response");
    } catch (XMLParserException xe) {
      Assert.fail("Unable to parse XML file: " + xe);
    } catch (UnmarshallingException ue) {
      Assert.fail("Unable to unmarshall XML: " + ue);
    }
  }
/** A set of utility methods to construct SAMLP Request statements */
public final class SamlpRequestComponentBuilder {

  private static volatile SAMLObjectBuilder<AuthnRequest> authnRequestBuilder;

  private static volatile SAMLObjectBuilder<Issuer> issuerBuilder;

  private static volatile SAMLObjectBuilder<NameIDPolicy> nameIDBuilder;

  private static volatile SAMLObjectBuilder<RequestedAuthnContext> requestedAuthnCtxBuilder;

  private static volatile SAMLObjectBuilder<AuthnContextClassRef> requestedAuthnCtxClassRefBuilder;

  private static volatile XMLObjectBuilderFactory builderFactory =
      XMLObjectProviderRegistrySupport.getBuilderFactory();

  private SamlpRequestComponentBuilder() {}

  @SuppressWarnings("unchecked")
  // CHECKSTYLE:OFF
  public static AuthnRequest createAuthnRequest(
      String serviceURL,
      boolean forceAuthn,
      boolean isPassive,
      String protocolBinding,
      SAMLVersion version,
      Issuer issuer,
      NameIDPolicy nameIDPolicy,
      RequestedAuthnContext requestedAuthnCtx) {
    // CHECKSTYLE:ON
    if (authnRequestBuilder == null) {
      authnRequestBuilder =
          (SAMLObjectBuilder<AuthnRequest>)
              builderFactory.getBuilder(AuthnRequest.DEFAULT_ELEMENT_NAME);
    }
    AuthnRequest authnRequest = authnRequestBuilder.buildObject();
    authnRequest.setAssertionConsumerServiceURL(serviceURL);
    authnRequest.setForceAuthn(forceAuthn);
    authnRequest.setID("_" + UUID.randomUUID());
    authnRequest.setIsPassive(isPassive);
    authnRequest.setIssueInstant(new DateTime());
    authnRequest.setProtocolBinding(protocolBinding);
    authnRequest.setVersion(version);

    authnRequest.setIssuer(issuer);
    authnRequest.setNameIDPolicy(nameIDPolicy);
    authnRequest.setRequestedAuthnContext(requestedAuthnCtx);

    return authnRequest;
  }

  @SuppressWarnings("unchecked")
  public static Issuer createIssuer(String issuerValue) {
    if (issuerBuilder == null) {
      issuerBuilder =
          (SAMLObjectBuilder<Issuer>) builderFactory.getBuilder(Issuer.DEFAULT_ELEMENT_NAME);
    }
    Issuer issuer = issuerBuilder.buildObject();
    issuer.setValue(issuerValue);

    return issuer;
  }

  @SuppressWarnings("unchecked")
  public static NameIDPolicy createNameIDPolicy(
      boolean allowCreate, String format, String spNameQualifier) {
    if (nameIDBuilder == null) {
      nameIDBuilder =
          (SAMLObjectBuilder<NameIDPolicy>)
              builderFactory.getBuilder(NameIDPolicy.DEFAULT_ELEMENT_NAME);
    }
    NameIDPolicy nameId = nameIDBuilder.buildObject();
    nameId.setAllowCreate(allowCreate);
    nameId.setFormat(format);
    nameId.setSPNameQualifier(spNameQualifier);

    return nameId;
  }

  @SuppressWarnings("unchecked")
  public static RequestedAuthnContext createRequestedAuthnCtxPolicy(
      AuthnContextComparisonTypeEnumeration comparison,
      List<AuthnContextClassRef> authnCtxClassRefList,
      List<AuthnContextDeclRef> authnCtxDeclRefList) {
    if (requestedAuthnCtxBuilder == null) {
      requestedAuthnCtxBuilder =
          (SAMLObjectBuilder<RequestedAuthnContext>)
              builderFactory.getBuilder(RequestedAuthnContext.DEFAULT_ELEMENT_NAME);
    }
    RequestedAuthnContext authnCtx = requestedAuthnCtxBuilder.buildObject();
    authnCtx.setComparison(comparison);

    if (authnCtxClassRefList != null) {
      List<AuthnContextClassRef> classRefList = authnCtx.getAuthnContextClassRefs();
      classRefList.addAll(authnCtxClassRefList);
    }

    if (authnCtxDeclRefList != null) {
      List<AuthnContextDeclRef> declRefList = authnCtx.getAuthnContextDeclRefs();
      declRefList.addAll(authnCtxDeclRefList);
    }

    return authnCtx;
  }

  @SuppressWarnings("unchecked")
  public static AuthnContextClassRef createAuthnCtxClassRef(String authnCtxClassRefValue) {
    if (requestedAuthnCtxClassRefBuilder == null) {
      requestedAuthnCtxClassRefBuilder =
          (SAMLObjectBuilder<AuthnContextClassRef>)
              builderFactory.getBuilder(AuthnContextClassRef.DEFAULT_ELEMENT_NAME);
    }
    AuthnContextClassRef authnCtxClassRef = requestedAuthnCtxClassRefBuilder.buildObject();
    authnCtxClassRef.setAuthnContextClassRef(authnCtxClassRefValue);

    return authnCtxClassRef;
  }
}