Ejemplo n.º 1
0
 /*
  * (non-Javadoc)
  *
  * @see java.lang.Object#toString()
  */
 @Override
 public String toString() {
   StringBuilder result = new StringBuilder();
   result.append("Principal: " + getPrincipal() + ", Attributes: ");
   for (AttributeStatement attributeStatement : getAttributeStatements()) {
     for (Attribute attr : attributeStatement.getAttributes()) {
       result.append("[ ");
       result.append(attr.getName());
       result.append(" : ");
       for (int i = 0; i < attr.getAttributeValues().size(); i++) {
         result.append(((XSString) attr.getAttributeValues().get(i)).getValue());
       }
       result.append("] ");
     }
   }
   // add this back in when we support parsing this information
   result.append(", AuthnStatements: ");
   for (AuthnStatement authStatement : getAuthnStatements()) {
     result.append("[ ");
     result.append(authStatement.getAuthnInstant() + " : ");
     result.append(
         authStatement.getAuthnContext().getAuthnContextClassRef().getAuthnContextClassRef());
     result.append("] ");
   }
   //        result.append(", AuthzDecisionStatements: ");
   //        for (AuthzDecisionStatement authDecision : getAuthzDecisionStatements()) {
   //            result.append("[ ");
   //            result.append(authDecision.getDecision().toString());
   //            result.append(" ]");
   //        }
   return result.toString();
 }
  /**
   * Checks that all the attributes have a unique Name/NameFormat pair.
   *
   * @param query the attribute query to validate
   * @throws ValidationException thrown if more than on Name/NameFormat pair is found in the list of
   *     attributes in this query
   */
  protected void validateUniqueAttributeIdentifiers(AttributeQuery query)
      throws ValidationException {
    List<Attribute> attributes = query.getAttributes();

    HashSet<Pair<String, String>> encounteredNames = new HashSet<Pair<String, String>>();
    String attributeName;
    String attributeNameFormat;
    for (Attribute attribute : attributes) {
      attributeName = attribute.getName();
      attributeNameFormat = attribute.getNameFormat();
      if (DatatypeHelper.isEmpty(attributeNameFormat)) {
        // SAML 2 core, sec. 2.7.3.1, if no format is specified,
        // unspecified is in effect. This avoids bug in processing null value.
        attributeNameFormat = Attribute.UNSPECIFIED;
      }

      Pair<String, String> pair = new Pair<String, String>(attributeName, attributeNameFormat);
      if (encounteredNames.contains(pair)) {
        throw new ValidationException(
            "Attribute query contains more than one attribute with the same Name and NameFormat");
      } else {
        encounteredNames.add(pair);
      }
    }
  }
Ejemplo n.º 3
0
  public List<EntityDescriptor> filterEntityCategory(
      List<EntityDescriptor> entities, String category) {
    List<EntityDescriptor> returnList = new ArrayList<EntityDescriptor>();

    for (EntityDescriptor entity : entities) {
      Extensions extensions = entity.getExtensions();
      List<XMLObject> extObjs = extensions.getOrderedChildren();
      for (XMLObject xmlObject : extObjs) {
        if (xmlObject instanceof EntityAttributes) {
          EntityAttributes entityAttrs = (EntityAttributes) xmlObject;
          for (Attribute attr : entityAttrs.getAttributes()) {
            if ("http://macedir.org/entity-category".equals(attr.getName())) {
              for (XMLObject value : attr.getAttributeValues()) {
                if (value instanceof XSAny) {
                  XSAny any = (XSAny) value;
                  if (category.equals(any.getTextContent())) {
                    returnList.add(entity);
                  }
                }
              }
            }
          }
        }
      }
    }

    return returnList;
  }
  /*
   * Process the response and returns the results
   */
  private Map<ClaimMapping, String> getAssertionStatements(Assertion assertion) {

    Map<ClaimMapping, String> results = new HashMap<ClaimMapping, String>();

    if (assertion != null) {

      List<AttributeStatement> attributeStatementList = assertion.getAttributeStatements();

      if (attributeStatementList != null) {
        for (AttributeStatement statement : attributeStatementList) {
          List<Attribute> attributesList = statement.getAttributes();
          for (Attribute attribute : attributesList) {
            Element value = attribute.getAttributeValues().get(0).getDOM();
            String attributeValue = value.getTextContent();
            results.put(
                ClaimMapping.build(attribute.getName(), attribute.getName(), null, false),
                attributeValue);
          }
        }
      }
    }
    return results;
  }
Ejemplo n.º 5
0
  @Override
  protected Saml2Profile retrieveUserProfile(
      final Saml2Credentials credentials, final WebContext context) {

    Saml2Profile profile = new Saml2Profile();
    profile.setId(credentials.getNameId().getValue());
    for (Attribute attribute : credentials.getAttributes()) {
      List<String> values = new ArrayList<String>();
      for (XMLObject attributeValue : attribute.getAttributeValues()) {
        Element attributeValueElement = attributeValue.getDOM();
        String value = attributeValueElement.getTextContent();
        values.add(value);
      }
      profile.addAttribute(attribute.getName(), values);
    }

    return profile;
  }
Ejemplo n.º 6
0
  @Override
  public Set<Principal> getPrincipals() {
    Set<Principal> principals = new HashSet<>();
    Principal primary = getPrincipal();
    principals.add(primary);
    principals.add(new RolePrincipal(primary.getName()));
    for (AttributeStatement attributeStatement : getAttributeStatements()) {
      for (Attribute attr : attributeStatement.getAttributes()) {
        if (StringUtils.containsIgnoreCase(attr.getName(), "role")) {
          for (final XMLObject obj : attr.getAttributeValues()) {
            principals.add(new RolePrincipal(((XSString) obj).getValue()));
          }
        }
      }
    }

    return principals;
  }
Ejemplo n.º 7
0
 /**
  * Checks if the NameIDFormat is of the following formats below, if not, the name is changed to
  * the value of the first matching usernameAttribute.
  */
 private void identifyNameIDFormat() {
   if (!((StringUtils.containsIgnoreCase(nameIDFormat, SAML2Constants.NAMEID_FORMAT_PERSISTENT)
           || StringUtils.containsIgnoreCase(
               nameIDFormat, SAML2Constants.NAMEID_FORMAT_X509_SUBJECT_NAME)
           || StringUtils.containsIgnoreCase(nameIDFormat, SAML2Constants.NAMEID_FORMAT_KERBEROS)
           || StringUtils.containsIgnoreCase(
               nameIDFormat, SAML2Constants.NAMEID_FORMAT_UNSPECIFIED))
       && !name.equals(""))) {
     for (AttributeStatement attributeStatementList : getAttributeStatements()) {
       List<Attribute> attributeList = attributeStatementList.getAttributes();
       for (Attribute attribute : attributeList) {
         if (listContainsIgnoreCase(usernameAttributeList, attribute.getName())) {
           name = ((XMLString) attribute.getAttributeValues().get(0)).getValue();
           return;
         }
       }
     }
   }
 }
  private Map<String, Object> getUserAttributes(ResponseImpl samlResponse) {

    Map<String, Object> userAttributes = new HashMap<>();

    // Add 'Subject'
    Assertion assertion = samlResponse.getAssertions().get(0);
    userAttributes.put(
        SAMLConstants.SAML2_ASSERTION_SUBJECT, assertion.getSubject().getNameID().getValue());

    // Add other user attributes.
    List<AttributeStatement> attributeStatements = assertion.getAttributeStatements();
    if (attributeStatements != null) {
      for (AttributeStatement attributeStatement : attributeStatements) {
        List<Attribute> attributes = attributeStatement.getAttributes();
        for (Attribute attribute : attributes) {
          userAttributes.put(
              attribute.getName(), attribute.getAttributeValues().get(0).getDOM().getTextContent());
        }
      }
    }

    return userAttributes;
  }