Search in sources :

Example 51 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project ddf by codice.

the class CswEndpointTest method addSecurity.

private void addSecurity() {
    org.apache.shiro.mgt.SecurityManager secManager = new DefaultSecurityManager();
    PrincipalCollection principals = new SimplePrincipalCollection(USER_ID, "testrealm");
    Subject subject = new Subject.Builder(secManager).principals(principals).session(new SimpleSession()).authenticated(true).buildSubject();
    ThreadContext.bind(secManager);
    ThreadContext.bind(subject);
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SimpleSession(org.apache.shiro.session.mgt.SimpleSession) Subject(org.apache.shiro.subject.Subject)

Example 52 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project ddf by codice.

the class SubjectUtilsTest method testGetDefaultName.

@Test
public void testGetDefaultName() {
    org.apache.shiro.subject.Subject subject;
    org.apache.shiro.mgt.SecurityManager secManager = new DefaultSecurityManager();
    PrincipalCollection principals = new SimplePrincipalCollection();
    subject = new ddf.security.Subject.Builder(secManager).principals(principals).session(new SimpleSession()).authenticated(true).buildSubject();
    assertEquals(DEFAULT_NAME, subjectUtils.getName(subject, DEFAULT_NAME));
    assertEquals(DEFAULT_NAME, subjectUtils.getName(null, DEFAULT_NAME));
}
Also used : Subject(org.apache.shiro.subject.Subject) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SimpleSession(org.apache.shiro.session.mgt.SimpleSession) Subject(org.apache.shiro.subject.Subject) Test(org.junit.Test)

Example 53 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project ddf by codice.

the class SecurityManagerImpl method getSubject.

@Override
public Subject getSubject(Object token) throws SecurityServiceException {
    AuthenticationToken authenticationToken = null;
    if (token instanceof SessionToken) {
        SimpleSession session = new SimpleSession();
        session.setId((String) ((SessionToken) token).getCredentials());
        return new SubjectImpl(((PrincipalCollection) ((SessionToken) token).getPrincipal()), true, session, internalManager);
    } else if (token instanceof AuthenticationToken) {
        authenticationToken = (AuthenticationToken) token;
    }
    if (authenticationToken != null) {
        Subject subject = getSubject(authenticationToken);
        securityLogger.audit("Logged in", subject);
        return subject;
    } else {
        throw new SecurityServiceException("Incoming token object NOT supported by security manager implementation. Currently supported types are AuthenticationToken and SecurityToken");
    }
}
Also used : SecurityServiceException(ddf.security.service.SecurityServiceException) AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) SessionToken(org.codice.ddf.security.handler.SessionToken) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimpleSession(org.apache.shiro.session.mgt.SimpleSession) SubjectImpl(ddf.security.impl.SubjectImpl) Subject(ddf.security.Subject)

Example 54 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project ddf by codice.

the class SubjectUtils method getType.

/**
 * Retrieves the type of the Security Assertion inside the given Subject.
 *
 * @param subject Subject to get the user name from.
 * @return String representation of the user name if available or defaultName if no user name
 *     could be found or incoming subject was null.
 */
@Override
public String getType(Subject subject) {
    if (subject == null) {
        LOGGER.debug("Incoming subject was null, cannot look up security assertion type.");
        return null;
    }
    PrincipalCollection principals = subject.getPrincipals();
    if (principals == null) {
        LOGGER.debug("No principals located in the incoming subject, cannot look up security assertion type.");
        return null;
    }
    Collection<SecurityAssertion> assertions = principals.byType(SecurityAssertion.class);
    if (assertions == null || assertions.isEmpty()) {
        LOGGER.debug("No principals located in the incoming subject, cannot look up security assertion type.");
        return null;
    }
    List<SecurityAssertion> assertionList = new ArrayList<>(assertions);
    assertionList.sort(new SecurityAssertionComparator());
    return assertionList.get(0).getTokenType();
}
Also used : ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SecurityAssertion(ddf.security.assertion.SecurityAssertion)

Example 55 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project ddf by codice.

the class SubjectUtils method getAttribute.

/**
 * Get any attribute from a subject by key.
 *
 * @param subject
 * @param key
 * @return attribute values or an empty list if not found.
 */
@Override
public List<String> getAttribute(@Nullable Subject subject, String key) {
    Validate.notNull(key);
    if (subject == null) {
        LOGGER.debug("Incoming subject was null, cannot look up {}.", key);
        return Collections.emptyList();
    }
    PrincipalCollection principals = subject.getPrincipals();
    if (principals == null) {
        LOGGER.debug("No principals located in the incoming subject, cannot look up {}.", key);
        return Collections.emptyList();
    }
    Collection<SecurityAssertion> assertions = principals.byType(SecurityAssertion.class);
    if (assertions.isEmpty()) {
        LOGGER.debug("Could not find Security Assertion, cannot look up {}.", key);
        return Collections.emptyList();
    }
    List<SecurityAssertion> assertionList = new ArrayList<>(assertions);
    assertionList.sort(new SecurityAssertionComparator());
    return assertionList.stream().map(SecurityAssertion::getAttributeStatements).flatMap(List::stream).flatMap(as -> as.getAttributes().stream()).filter(a -> a.getName().equals(key)).flatMap(a -> a.getValues().stream()).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) X500Principal(javax.security.auth.x500.X500Principal) SortedSet(java.util.SortedSet) LoggerFactory(org.slf4j.LoggerFactory) AttributeStatement(ddf.security.assertion.AttributeStatement) BCStyle(org.bouncycastle.asn1.x500.style.BCStyle) SubjectOperations(ddf.security.SubjectOperations) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) AttributeTypeAndValue(org.bouncycastle.asn1.x500.AttributeTypeAndValue) X500Name(org.bouncycastle.asn1.x500.X500Name) Subject(org.apache.shiro.subject.Subject) StringTokenizer(java.util.StringTokenizer) Map(java.util.Map) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) Nullable(javax.annotation.Nullable) SecurityAssertion(ddf.security.assertion.SecurityAssertion) Logger(org.slf4j.Logger) Attribute(ddf.security.assertion.Attribute) RDN(org.bouncycastle.asn1.x500.RDN) Predicate(java.util.function.Predicate) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Principal(java.security.Principal) GuestPrincipal(ddf.security.principal.impl.GuestPrincipal) Comparator(java.util.Comparator) Collections(java.util.Collections) Validate(org.apache.commons.lang.Validate) ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) ArrayList(java.util.ArrayList) List(java.util.List) SecurityAssertion(ddf.security.assertion.SecurityAssertion)

Aggregations

PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)88 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)40 Test (org.junit.Test)36 SecurityAssertion (ddf.security.assertion.SecurityAssertion)23 Subject (ddf.security.Subject)15 Subject (org.apache.shiro.subject.Subject)15 Principal (java.security.Principal)14 ArrayList (java.util.ArrayList)10 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)10 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)9 Permission (org.apache.shiro.authz.Permission)8 Session (org.apache.shiro.session.Session)8 SimpleSession (org.apache.shiro.session.mgt.SimpleSession)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Attribute (ddf.security.assertion.Attribute)5 Map (java.util.Map)5 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)5 CollectionPermission (ddf.security.permission.CollectionPermission)4