Example usage for javax.security.auth Subject getPrincipals

List of usage examples for javax.security.auth Subject getPrincipals

Introduction

In this page you can find the example usage for javax.security.auth Subject getPrincipals.

Prototype

public <T extends Principal> Set<T> getPrincipals(Class<T> c) 

Source Link

Document

Return a Set of Principals associated with this Subject that are instances or subclasses of the specified Class .

Usage

From source file:net.sourceforge.safr.sample.usermgnt.service.UserServiceImpl.java

private static Principal currentUserPrincipal() {
    Subject s = Subject.getSubject(AccessController.getContext());
    return s.getPrincipals(UserPrincipal.class).iterator().next();
}

From source file:ru.runa.wfe.security.auth.SubjectPrincipalsHelper.java

public static User getUser(Subject subject) throws AuthenticationException {
    Preconditions.checkNotNull(subject);
    Set<User> principals = subject.getPrincipals(User.class);
    for (User user : principals) {
        if (user != null) {
            return user;
        }/*from  ww  w  .  ja va2  s .c  o  m*/
    }
    throw new AuthenticationException("Subject does not contain user principal");
}

From source file:org.apache.hadoop.security.SecureClientLogin.java

public static Set<Principal> getUserPrincipals(Subject aSubject) {
    if (aSubject != null) {
        Set<User> list = aSubject.getPrincipals(User.class);
        if (list != null) {
            Set<Principal> ret = new HashSet<Principal>();
            for (User a : list) {
                ret.add(a);//from w  w w  .j  av a 2  s .  c  o  m
            }
            return ret;
        } else {
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.infoscoop.util.RoleUtil.java

/**
 * @param type/*from   www .  j  a  va 2 s.  com*/
 * @param regx
 * @return The result string list matched to group in regx, when type is not matched to regx, return null
 * @throws ClassNotFoundException
 */
public static List<String> getPermittedMatchList(String type, String regx) throws ClassNotFoundException {
    Subject loginUser = SecurityController.getContextSubject();
    List<String> retVal = null;
    if (loginUser != null) {
        Collection<ISPrincipal> principals = loginUser.getPrincipals(ISPrincipal.class);
        if (log.isInfoEnabled())
            log.info("LoginUser Prinipales:" + principals);

        try {
            Pattern pattern = Pattern.compile(regx);
            for (ISPrincipal p : principals) {
                if (type.equals(p.getType()) && p.getName() != null) {
                    Matcher matcher = pattern.matcher(p.getName());
                    retVal = matcher2List(matcher);
                    if (retVal != null)
                        break;
                }
            }
        } catch (PatternSyntaxException e) {
            log.warn("\"" + regx + "\" is invalid regular expression.");
        }
    }
    if (log.isInfoEnabled())
        log.info("PrincipalType=" + type + ",RegExp=" + regx + ", isPermitted=" + retVal);
    return retVal;
}

From source file:com.flexive.core.security.FxDefaultLogin.java

/**
 * Returns the UserTicket stored within the subject.
 *
 * @param sub the suubject//from  ww  w  .  ja v  a2s .  co m
 * @return the UserTicket stored within the subject
 * @throws FxNotFoundException if the subject doesnt hold a UserTicket.
 *                             This should never happen since the Login Module fills out
 *                             a UserTicket for every new subject.
 */
public static UserTicket getUserTicket(Subject sub) throws FxNotFoundException {
    Iterator it = sub.getPrincipals(FxPrincipal.class).iterator();
    if (it.hasNext()) {
        FxPrincipal p = (FxPrincipal) it.next();
        return p.getUserTicket();
    } else {
        FxNotFoundException nfe = new FxNotFoundException("Subject without UserTicket encountered");
        LOG.fatal(nfe);
        throw nfe;
    }
}

From source file:com.flexive.core.security.FxDefaultLogin.java

/**
 * Sets the user ticket within a subject
 *
 * @param sub    the subject/*from w w  w .j a v  a  2  s.  co m*/
 * @param ticket the new ticket for the subject
 * @return the user ticket
 */
public static Subject updateUserTicket(Subject sub, UserTicket ticket) {
    // remove the old user ticket
    for (FxPrincipal p : sub.getPrincipals(FxPrincipal.class))
        sub.getPrincipals().remove(p);
    // Set the credentials and principals
    sub.getPrincipals().add(new FxPrincipal(ticket));
    return sub;
}

From source file:org.apache.jxtadoop.security.UserGroupInformation.java

/** @return the {@link UserGroupInformation} for the current thread */
public static UserGroupInformation getCurrentUGI() {
    Subject user = getCurrentUser();

    if (user == null) {
        user = currentUser.get();/*from w  w w . java  2s  . c o  m*/
        if (user == null) {
            return null;
        }
    }

    Set<UserGroupInformation> ugiPrincipals = user.getPrincipals(UserGroupInformation.class);

    UserGroupInformation ugi = null;
    if (ugiPrincipals != null && ugiPrincipals.size() == 1) {
        ugi = ugiPrincipals.iterator().next();
        if (ugi == null) {
            throw new RuntimeException("Cannot find _current user_ UGI in the Subject!");
        }
    } else {
        throw new RuntimeException("Cannot resolve current user from subject, " + "which had "
                + ugiPrincipals.size() + " UGI principals!");
    }
    return ugi;
}

From source file:info.magnolia.cms.security.PermissionUtil.java

/**
 * Creates instance of AccessManager configured with subject principal permissions for requested workspace/repository. This method will likely move the AccessManagerProvider in the future version, and while public should not be considered part of the public API.
 *///from w  w w. j av  a 2s  . co m
public static AccessManager getAccessManager(String workspace, Subject subject) {
    List<Permission> availablePermissions = PermissionUtil.getPermissions(subject, workspace);
    if (availablePermissions == null) {
        log.warn("no permissions found for " + subject.getPrincipals(User.class));
    }
    // TODO: use provider instead of fixed impl
    AccessManagerImpl ami = new AccessManagerImpl();
    ami.setPermissionList(availablePermissions);
    return ami;
}

From source file:org.polymap.rhei.um.auth.UmAuthorizationModule.java

@Override
public Set<Principal> rolesOf(Subject subject) {
    Set<UserPrincipal> principals = subject.getPrincipals(UserPrincipal.class);
    UserPrincipal principal = Iterables.getOnlyElement(principals);

    Set<Principal> result = new HashSet();
    if (principal.getName().equals("admin")) {
        ///*w  w w . j  a va2  s  .  co m*/
    } else {
        User user = ((UmUserPrincipal) principal).getUser();
        for (final String groupName : loginModule.repo.groupsOf(user)) {
            result.add(new Principal() {
                @Override
                public String getName() {
                    return groupName;
                }
            });
        }
    }
    return result;
}

From source file:org.polymap.core.security.DummyAuthorizationModule.java

@Override
public Set<Principal> rolesOf(Subject subject) {
    Set<DummyUserPrincipal> dummyPrincipals = subject.getPrincipals(DummyUserPrincipal.class);
    assert dummyPrincipals.size() <= 1;

    // DummyUserPrincipal found
    if (!dummyPrincipals.isEmpty()) {
        return getOnlyElement(dummyPrincipals).getRoles();
    }/*from   w w  w . ja  v a 2  s.  c  om*/
    //
    else {
        Set<UserPrincipal> users = subject.getPrincipals(UserPrincipal.class);
        assert users.size() == 1 : "Too many/less UserPrincipals in subject: " + users;
        DummyUserPrincipal dummyUser = delegateLoginModule.userForName(getOnlyElement(users).getName());
        return dummyUser != null ? dummyUser.getRoles() : Collections.EMPTY_SET;
    }
}