org.apache.shiro.subject.MutablePrincipalCollection Java Examples

The following examples show how to use org.apache.shiro.subject.MutablePrincipalCollection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JndiLdapRealmWithUser.java    From jesterj with Apache License 2.0 5 votes vote down vote up
@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token, Object ldapPrincipal, Object ldapCredentials, LdapContext ldapContext) throws NamingException {
  SimpleAuthenticationInfo authenticationInfo = (SimpleAuthenticationInfo) super.createAuthenticationInfo(token, ldapPrincipal, ldapCredentials, ldapContext);
  MutablePrincipalCollection mpc = (MutablePrincipalCollection) authenticationInfo.getPrincipals();
  final SearchControls constraints = new SearchControls();
  constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

  // get all attributes
  constraints.setReturningAttributes(null);
  String templ = getUserDnTemplate();
  String userDn = MessageFormat.format(templ, mpc.getPrimaryPrincipal());
  final NamingEnumeration<SearchResult> answer = ldapContext.search(userDn, "(objectClass=*)", constraints);

  if (answer.hasMore()) {
    Attributes attrs = answer.next().getAttributes();
    if (answer.hasMore()) {
      throw new NamingException("Non-unique user specified by:" + userDn);
    }
    //TODO: make this Guicy
    User user = new UserFromLdap(attrs, mpc);

    // at present there should only be one realm involved.
    Iterator<String> realmIter = mpc.getRealmNames().iterator();
    String firstRealm = realmIter.next();
    if (realmIter.hasNext()) {
      // ugh, need a new solution here
      String explanation = String.format("More than one realm found! (%s and %s)", firstRealm, realmIter.next());
      throw new NamingException(explanation);
    }
    mpc.add(user,firstRealm);
  } else {
    throw new NamingException("Invalid User specified by:" + userDn);
  }

  return authenticationInfo;
}
 
Example #2
Source File: SimpleAuthenticationInfo.java    From nano-framework with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void merge(AuthenticationInfo info) {
	if (info == null || info.getPrincipals() == null || info.getPrincipals().isEmpty()) {
		return;
	}

	if (this.principals == null) {
		this.principals = info.getPrincipals();
	} else {
		if (!(this.principals instanceof MutablePrincipalCollection)) {
			this.principals = new SimplePrincipalCollection(this.principals);
		}
		((MutablePrincipalCollection) this.principals).addAll(info.getPrincipals());
	}

	if (this.credentialsSalt == null && info instanceof SaltedAuthenticationInfo) {
		this.credentialsSalt = ((SaltedAuthenticationInfo) info).getCredentialsSalt();
	}

	Object thisCredentials = getCredentials();
	Object otherCredentials = info.getCredentials();

	if (otherCredentials == null) {
		return;
	}

	if (thisCredentials == null) {
		this.credentials = otherCredentials;
		return;
	}

	if (!(thisCredentials instanceof Collection)) {
		Set<Object> newSet = new HashSet<>();
		newSet.add(thisCredentials);
		setCredentials(newSet);
	}

	// At this point, the credentials should be a collection
	Collection<Object> credentialCollection = (Collection<Object>) getCredentials();
	if (otherCredentials instanceof Collection) {
		credentialCollection.addAll((Collection<Object>) otherCredentials);
	} else {
		credentialCollection.add(otherCredentials);
	}
}
 
Example #3
Source File: UsergridAuthenticationInfo.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Takes the specified <code>info</code> argument and adds its principals and credentials into this instance.
 *
 * @param info the <code>AuthenticationInfo</code> to add into this instance.
 */
@SuppressWarnings("unchecked")
public void merge(AuthenticationInfo info) {
    if (info == null || info.getPrincipals() == null || info.getPrincipals().isEmpty()) {
        return;
    }

    if (this.principals == null) {
        this.principals = info.getPrincipals();
    } else {
        if (!(this.principals instanceof MutablePrincipalCollection)) {
            this.principals = new SimplePrincipalCollection(this.principals);
        }
        ((MutablePrincipalCollection) this.principals).addAll(info.getPrincipals());
    }

    //only mess with a salt value if we don't have one yet.  It doesn't make sense
    //to merge salt values from different realms because a salt is used only within
    //the realm's credential matching process.  But if the current instance's salt
    //is null, then it can't hurt to pull in a non-null value if one exists.
    //
    //since 1.1:
    if (this.credentialsSalt == null && info instanceof SaltedAuthenticationInfo) {
        this.credentialsSalt = ((SaltedAuthenticationInfo) info).getCredentialsSalt();
    }

    Object thisCredentials = getCredentials();
    Object otherCredentials = info.getCredentials();

    if (otherCredentials == null) {
        return;
    }

    if (thisCredentials == null) {
        this.credentials = otherCredentials;
        return;
    }

    if (!(thisCredentials instanceof Collection)) {
        Set newSet = new HashSet();
        newSet.add(thisCredentials);
        setCredentials(newSet);
    }

    // At this point, the credentials should be a collection
    Collection credentialCollection = (Collection) getCredentials();
    if (otherCredentials instanceof Collection) {
        credentialCollection.addAll((Collection) otherCredentials);
    } else {
        credentialCollection.add(otherCredentials);
    }
}