예제 #1
0
 /**
  * Gets assertion consumer service for.
  *
  * @param authnRequest the authn request
  * @return the assertion consumer service for
  */
 public static AssertionConsumerService getAssertionConsumerServiceFor(
     final AuthnRequest authnRequest) {
   final AssertionConsumerService acs = new AssertionConsumerServiceBuilder().buildObject();
   acs.setBinding(authnRequest.getProtocolBinding());
   acs.setLocation(authnRequest.getAssertionConsumerServiceURL());
   acs.setResponseLocation(authnRequest.getAssertionConsumerServiceURL());
   return acs;
 }
  /** {@inheritDoc} */
  @Test
  public void testSingleElementOptionalAttributesUnmarshall() {
    AuthnRequest req = (AuthnRequest) unmarshallElement(singleElementOptionalAttributesFile);

    Assert.assertEquals(
        req.isForceAuthnXSBoolean(),
        expectedForceAuthn,
        "Unmarshalled ForceAuthn was not the expected value");
    Assert.assertEquals(
        req.isPassiveXSBoolean(),
        expectedIsPassive,
        "Unmarshalled IsPassive was not the expected value");
    Assert.assertEquals(
        req.getProtocolBinding(),
        expectedProtocolBinding,
        "Unmarshalled ProtocolBinding was not the expected value");
    Assert.assertEquals(
        req.getAssertionConsumerServiceIndex(),
        expectedAssertionConsumerServiceIndex,
        "Unmarshalled AssertionConsumerServiceIndex was not the expected value");
    Assert.assertEquals(
        req.getAssertionConsumerServiceURL(),
        expectedAssertionConsumerServiceURL,
        "Unmarshalled AssertionConsumerServiceURL was not the expected value");
    Assert.assertEquals(
        req.getAttributeConsumingServiceIndex(),
        expectedAttributeConsumingServiceIndex,
        "Unmarshalled AttributeConsumingServiceIndex was not the expected value");
    Assert.assertEquals(
        req.getProviderName(),
        expectedProviderName,
        "Unmarshalled ProviderName was not the expected value");

    super.helperTestSingleElementOptionalAttributesUnmarshall(req);
  }
  /**
   * Build the {@link MockHttpServletRequest}.
   *
   * @throws Exception if an error occurs
   */
  public void buildRequest() throws Exception {

    request.setMethod("POST");
    request.setRequestURI("/idp/profile/" + FLOW_ID);

    final AuthnRequest authnRequest = buildAuthnRequest(request);
    authnRequest.setDestination(getDestinationPost(request));

    final MessageContext<SAMLObject> messageContext =
        buildOutboundMessageContext(authnRequest, SAMLConstants.SAML2_POST_BINDING_URI);
    final SAMLObject message = messageContext.getMessage();
    final String encodedMessage = encodeMessage(message);
    request.addParameter("SAMLRequest", encodedMessage);
  }
  /** {@inheritDoc} */
  @Test
  public void testChildElementsUnmarshall() {
    AuthnRequest req = (AuthnRequest) unmarshallElement(childElementsFile);

    Assert.assertNotNull(req.getSubject(), "Subject was null");
    Assert.assertNotNull(req.getNameIDPolicy(), "NameIDPolicy was null");
    Assert.assertNotNull(req.getConditions(), "Conditions was null");
    Assert.assertNotNull(req.getRequestedAuthnContext(), "RequestedAuthnContext was null");
    Assert.assertNotNull(req.getScoping(), "Scoping was null");

    super.helperTestChildElementsUnmarshall(req);
  }
  /** {@inheritDoc} */
  @Test
  public void testSingleElementUnmarshall() {
    AuthnRequest req = (AuthnRequest) unmarshallElement(singleElementFile);

    Assert.assertNotNull(req, "AuthnRequest was null");
    Assert.assertEquals(
        req.isForceAuthn(), Boolean.FALSE, "ForceAuthn (empty) was not default value");
    Assert.assertEquals(req.isPassive(), Boolean.FALSE, "IsPassive (empty) was not default value");
    Assert.assertNull(req.getProtocolBinding(), "ProtocolBinding was not null");
    Assert.assertNull(
        req.getAssertionConsumerServiceIndex(), "AssertionConsumerServiceIndex was not null");
    Assert.assertNull(
        req.getAssertionConsumerServiceURL(), "AssertionConsumerServiceURL was not null");
    Assert.assertNull(
        req.getAttributeConsumingServiceIndex(), "AttributeConsumingServiceIndex was not null");
    Assert.assertNull(req.getProviderName(), "ProviderName was not null");

    super.helperTestSingleElementUnmarshall(req);
  }
  /** {@inheritDoc} */
  @Test
  public void testSingleElementOptionalAttributesMarshall() {
    QName qname =
        new QName(
            SAMLConstants.SAML20P_NS,
            AuthnRequest.DEFAULT_ELEMENT_LOCAL_NAME,
            SAMLConstants.SAML20P_PREFIX);
    AuthnRequest req = (AuthnRequest) buildXMLObject(qname);

    super.populateRequiredAttributes(req);
    super.populateOptionalAttributes(req);

    req.setForceAuthn(expectedForceAuthn);
    req.setIsPassive(expectedIsPassive);
    req.setProtocolBinding(expectedProtocolBinding);
    req.setAssertionConsumerServiceIndex(expectedAssertionConsumerServiceIndex);
    req.setAssertionConsumerServiceURL(expectedAssertionConsumerServiceURL);
    req.setAttributeConsumingServiceIndex(expectedAttributeConsumingServiceIndex);
    req.setProviderName(expectedProviderName);

    assertXMLEquals(expectedOptionalAttributesDOM, req);
  }
  /** {@inheritDoc} */
  @Test
  public void testChildElementsMarshall() {
    QName qname =
        new QName(
            SAMLConstants.SAML20P_NS,
            AuthnRequest.DEFAULT_ELEMENT_LOCAL_NAME,
            SAMLConstants.SAML20P_PREFIX);
    AuthnRequest req = (AuthnRequest) buildXMLObject(qname);

    super.populateChildElements(req);

    QName subjectQName =
        new QName(
            SAMLConstants.SAML20_NS,
            Subject.DEFAULT_ELEMENT_LOCAL_NAME,
            SAMLConstants.SAML20_PREFIX);
    req.setSubject((Subject) buildXMLObject(subjectQName));

    QName nameIDPolicyQName =
        new QName(
            SAMLConstants.SAML20P_NS,
            NameIDPolicy.DEFAULT_ELEMENT_LOCAL_NAME,
            SAMLConstants.SAML20P_PREFIX);
    req.setNameIDPolicy((NameIDPolicy) buildXMLObject(nameIDPolicyQName));

    QName conditionsQName =
        new QName(
            SAMLConstants.SAML20_NS,
            Conditions.DEFAULT_ELEMENT_LOCAL_NAME,
            SAMLConstants.SAML20_PREFIX);
    req.setConditions((Conditions) buildXMLObject(conditionsQName));

    QName requestedAuthnContextQName =
        new QName(
            SAMLConstants.SAML20P_NS,
            RequestedAuthnContext.DEFAULT_ELEMENT_LOCAL_NAME,
            SAMLConstants.SAML20P_PREFIX);
    req.setRequestedAuthnContext(
        (RequestedAuthnContext) buildXMLObject(requestedAuthnContextQName));

    QName scopingQName =
        new QName(
            SAMLConstants.SAML20P_NS,
            Scoping.DEFAULT_ELEMENT_LOCAL_NAME,
            SAMLConstants.SAML20P_PREFIX);
    req.setScoping((Scoping) buildXMLObject(scopingQName));

    assertXMLEquals(expectedChildElementsDOM, req);
  }
  /** Test the proper behavior of the XSBooleanValue attributes. */
  @Test
  public void testXSBooleanAttributes() {
    AuthnRequest req = (AuthnRequest) buildXMLObject(AuthnRequest.DEFAULT_ELEMENT_NAME);

    // ForceAuthn attribute
    req.setForceAuthn(Boolean.TRUE);
    Assert.assertEquals(
        req.isForceAuthn(), Boolean.TRUE, "Unexpected value for boolean attribute found");
    Assert.assertNotNull(req.isForceAuthnXSBoolean(), "XSBooleanValue was null");
    Assert.assertEquals(
        req.isForceAuthnXSBoolean(),
        new XSBooleanValue(Boolean.TRUE, false),
        "XSBooleanValue was unexpected value");
    Assert.assertEquals(
        req.isForceAuthnXSBoolean().toString(),
        "true",
        "XSBooleanValue string was unexpected value");

    req.setForceAuthn(Boolean.FALSE);
    Assert.assertEquals(
        req.isForceAuthn(), Boolean.FALSE, "Unexpected value for boolean attribute found");
    Assert.assertNotNull(req.isForceAuthnXSBoolean(), "XSBooleanValue was null");
    Assert.assertEquals(
        req.isForceAuthnXSBoolean(),
        new XSBooleanValue(Boolean.FALSE, false),
        "XSBooleanValue was unexpected value");
    Assert.assertEquals(
        req.isForceAuthnXSBoolean().toString(),
        "false",
        "XSBooleanValue string was unexpected value");

    req.setForceAuthn((Boolean) null);
    Assert.assertEquals(
        req.isForceAuthn(), Boolean.FALSE, "Unexpected default value for boolean attribute found");
    Assert.assertNull(req.isForceAuthnXSBoolean(), "XSBooleanValue was not null");

    // IsPassive attribute
    req.setIsPassive(Boolean.TRUE);
    Assert.assertEquals(
        req.isPassive(), Boolean.TRUE, "Unexpected value for boolean attribute found");
    Assert.assertNotNull(req.isPassiveXSBoolean(), "XSBooleanValue was null");
    Assert.assertEquals(
        req.isPassiveXSBoolean(),
        new XSBooleanValue(Boolean.TRUE, false),
        "XSBooleanValue was unexpected value");
    Assert.assertEquals(
        req.isPassiveXSBoolean().toString(), "true", "XSBooleanValue string was unexpected value");

    req.setIsPassive(Boolean.FALSE);
    Assert.assertEquals(
        req.isPassive(), Boolean.FALSE, "Unexpected value for boolean attribute found");
    Assert.assertNotNull(req.isPassiveXSBoolean(), "XSBooleanValue was null");
    Assert.assertEquals(
        req.isPassiveXSBoolean(),
        new XSBooleanValue(Boolean.FALSE, false),
        "XSBooleanValue was unexpected value");
    Assert.assertEquals(
        req.isPassiveXSBoolean().toString(), "false", "XSBooleanValue string was unexpected value");

    req.setIsPassive((Boolean) null);
    Assert.assertEquals(
        req.isPassive(), Boolean.FALSE, "Unexpected default value for boolean attribute found");
    Assert.assertNull(req.isPassiveXSBoolean(), "XSBooleanValue was not null");
  }
  @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;
  }