Search in sources :

Example 41 with PrincipalCollection

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

the class SubjectUtilsTest method getSubjectWithAttributes.

private Subject getSubjectWithAttributes(Map<String, List<String>> attributes) {
    Subject subject = mock(Subject.class);
    PrincipalCollection pc = mock(PrincipalCollection.class);
    SecurityAssertion assertion = mock(SecurityAssertion.class);
    AttributeStatement as = mock(AttributeStatement.class);
    List<Attribute> attrs = attributes.entrySet().stream().map(this::getAttribute).collect(Collectors.toList());
    doReturn(pc).when(subject).getPrincipals();
    doReturn(assertion).when(pc).oneByType(SecurityAssertion.class);
    doReturn(ImmutableList.of(assertion)).when(pc).byType(SecurityAssertion.class);
    doReturn(Collections.singletonList(as)).when(assertion).getAttributeStatements();
    doReturn(attrs).when(as).getAttributes();
    return subject;
}
Also used : Attribute(org.opensaml.saml.saml2.core.Attribute) AttributeStatement(org.opensaml.saml.saml2.core.AttributeStatement) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SecurityAssertion(ddf.security.assertion.SecurityAssertion)

Example 42 with PrincipalCollection

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

the class SubjectUtilsTest method testGetName.

@Test
public void testGetName() {
    org.apache.shiro.subject.Subject subject;
    org.apache.shiro.mgt.SecurityManager secManager = new DefaultSecurityManager();
    PrincipalCollection principals = new SimplePrincipalCollection(TEST_NAME, "testrealm");
    subject = new Subject.Builder(secManager).principals(principals).session(new SimpleSession()).authenticated(true).buildSubject();
    assertEquals(TEST_NAME, SubjectUtils.getName(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) Test(org.junit.Test)

Example 43 with PrincipalCollection

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

the class SubjectUtilsTest method testGetAttributeNullAssertion.

@Test
public void testGetAttributeNullAssertion() {
    Subject s = mock(Subject.class);
    PrincipalCollection principals = mock(PrincipalCollection.class);
    doReturn(principals).when(s).getPrincipals();
    assertThat(SubjectUtils.getAttribute(s, "any"), is(Collections.emptyList()));
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Test(org.junit.Test)

Example 44 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 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 : 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) Test(org.junit.Test)

Example 45 with PrincipalCollection

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

the class SubjectUtils method getName.

/**
     * Retrieves the user name from a given subject.
     *
     * @param subject           Subject to get the user name from.
     * @param defaultName       Name to send back if no user name was found.
     * @param returnDisplayName return formatted user name for displaying
     * @return String representation of the user name if available or
     * defaultName if no user name could be found or incoming subject
     * was null.
     */
public static String getName(Subject subject, String defaultName, boolean returnDisplayName) {
    String name = defaultName;
    if (subject != null) {
        PrincipalCollection principals = subject.getPrincipals();
        if (principals != null) {
            SecurityAssertion assertion = principals.oneByType(SecurityAssertion.class);
            if (assertion != null) {
                Principal principal = assertion.getPrincipal();
                if (principal instanceof KerberosPrincipal) {
                    StringTokenizer st = new StringTokenizer(principal.getName(), "@");
                    st = new StringTokenizer(st.nextToken(), "/");
                    name = st.nextToken();
                } else {
                    name = principal.getName();
                }
                if (returnDisplayName) {
                    name = getDisplayName(principal, name);
                }
            } else {
                // send back the primary principal as a string
                name = principals.getPrimaryPrincipal().toString();
            }
        } else {
            LOGGER.debug("No principals located in the incoming subject, cannot look up user name. Using default name of {}.", defaultName);
        }
    } else {
        LOGGER.debug("Incoming subject was null, cannot look up user name. Using default name of {}.", defaultName);
    }
    LOGGER.debug("Sending back name {}.", name);
    return name;
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) StringTokenizer(java.util.StringTokenizer) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) XSString(org.opensaml.core.xml.schema.XSString) SecurityAssertion(ddf.security.assertion.SecurityAssertion) X500Principal(javax.security.auth.x500.X500Principal) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) GuestPrincipal(ddf.security.principal.GuestPrincipal) Principal(java.security.Principal)

Aggregations

PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)87 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)40 Test (org.junit.Test)36 SecurityAssertion (ddf.security.assertion.SecurityAssertion)23 Subject (ddf.security.Subject)15 Principal (java.security.Principal)14 Subject (org.apache.shiro.subject.Subject)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 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)5 CollectionPermission (ddf.security.permission.CollectionPermission)4 KeyValueCollectionPermission (ddf.security.permission.KeyValueCollectionPermission)4