Ejemplo n.º 1
0
  protected IDPSSODescriptorType getIdpMetadataFromFile(SPType configuration) {
    InputStream is = this.servletContext.getResourceAsStream(configuration.getIdpMetadataFile());
    if (is == null) {
      return null;
    }

    Object metadata = null;
    try {
      Document samlDocument = DocumentUtil.getDocument(is);
      SAMLParser parser = new SAMLParser();
      metadata = parser.parse(DocumentUtil.getNodeAsStream(samlDocument));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    IDPSSODescriptorType idpSSO = null;
    if (metadata instanceof EntitiesDescriptorType) {
      EntitiesDescriptorType entities = (EntitiesDescriptorType) metadata;
      idpSSO = handleMetadata(entities);
    } else {
      idpSSO = handleMetadata((EntityDescriptorType) metadata);
    }
    if (idpSSO == null) {
      logger.samlSPUnableToGetIDPDescriptorFromMetadata();
      return idpSSO;
    }

    return idpSSO;
  }
  private EntitiesDescriptorType parseMDFile() throws ParsingException {
    InputStream is = SecurityActions.loadStream(getClass(), SP_MD_FILE);

    if (is == null) throw logger.nullValueError(SP_MD_FILE);

    SAMLParser parser = new SAMLParser();
    return (EntitiesDescriptorType) parser.parse(is);
  }
Ejemplo n.º 3
0
  public boolean handleSAML11UnsolicitedResponse(
      HttpServletRequest request, HttpServletResponse response) throws IOException {
    String samlResponse = request.getParameter(GeneralConstants.SAML_RESPONSE_KEY);

    Principal principal = request.getUserPrincipal();

    // If we have already authenticated the user and there is no request from IDP or logout from
    // user
    if (principal != null) {
      return true;
    }

    HttpSession session = request.getSession(true);

    // See if we got a response from IDP
    if (isNotNull(samlResponse)) {
      boolean isValid = false;
      try {
        isValid = validate(request);
      } catch (Exception e) {
        logger.samlSPHandleRequestError(e);
        throw new IOException();
      }
      if (!isValid) {
        throw new IOException(ErrorCodes.VALIDATION_CHECK_FAILED);
      }

      try {
        InputStream base64DecodedResponse = null;

        if ("GET".equalsIgnoreCase(request.getMethod())) {
          base64DecodedResponse = RedirectBindingUtil.base64DeflateDecode(samlResponse);
        } else {
          base64DecodedResponse = PostBindingUtil.base64DecodeAsStream(samlResponse);
        }

        SAMLParser parser = new SAMLParser();
        SAML11ResponseType saml11Response =
            (SAML11ResponseType) parser.parse(base64DecodedResponse);

        List<SAML11AssertionType> assertions = saml11Response.get();
        if (assertions.size() > 1) {
          logger.trace("More than one assertion from IDP. Considering the first one.");
        }
        String username = null;
        List<String> roles = new ArrayList<String>();
        SAML11AssertionType assertion = assertions.get(0);
        if (assertion != null) {
          // Get the subject
          List<SAML11StatementAbstractType> statements = assertion.getStatements();
          for (SAML11StatementAbstractType statement : statements) {
            if (statement instanceof SAML11AuthenticationStatementType) {
              SAML11AuthenticationStatementType subStat =
                  (SAML11AuthenticationStatementType) statement;
              SAML11SubjectType subject = subStat.getSubject();
              username = subject.getChoice().getNameID().getValue();
            }
          }
          roles = AssertionUtil.getRoles(assertion, null);
        }

        return true;
      } catch (Exception e) {
        logger.samlSPHandleRequestError(e);
      }
    }

    return false;
  }