Search in sources :

Example 21 with PrincipalCollection

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

the class AllSuccessfulStrategyTest method beforeAttemptRealmDoesntSupportToken.

@Test(expected = UnsupportedTokenException.class)
public void beforeAttemptRealmDoesntSupportToken() {
    Realm notSupportingRealm = new AuthorizingRealm() {

        public boolean supports(AuthenticationToken token) {
            return false;
        }

        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            return null;
        }

        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
            return null;
        }
    };
    strategy.beforeAttempt(notSupportingRealm, null, null);
}
Also used : AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Realm(org.apache.shiro.realm.Realm) SimpleAccountRealm(org.apache.shiro.realm.SimpleAccountRealm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Test(org.junit.Test)

Example 22 with PrincipalCollection

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

the class SimpleAuthenticationInfoTest method testMergeWithImmutablePrincipalCollection.

@SuppressWarnings("serial")
@Test
public void testMergeWithImmutablePrincipalCollection() {
    SimpleAuthenticationInfo aggregate = new SimpleAuthenticationInfo();
    // Make a quick test fixture that does *not* implement MutablePrincipalCollection
    PrincipalCollection principalCollection = new PrincipalCollection() {

        @SuppressWarnings("unchecked")
        public List asList() {
            return null;
        }

        @SuppressWarnings("unchecked")
        public Set asSet() {
            return null;
        }

        public <T> Collection<T> byType(Class<T> type) {
            return null;
        }

        @SuppressWarnings("unchecked")
        public Collection fromRealm(String realmName) {
            Collection<Object> principals = new HashSet<Object>();
            principals.add("testprincipal");
            return principals;
        }

        public Object getPrimaryPrincipal() {
            return null;
        }

        public Set<String> getRealmNames() {
            Set<String> realms = new HashSet<String>();
            realms.add("testrealm");
            return realms;
        }

        public boolean isEmpty() {
            return false;
        }

        public <T> T oneByType(Class<T> type) {
            return null;
        }

        @SuppressWarnings("unchecked")
        public Iterator iterator() {
            return null;
        }
    };
    aggregate.setPrincipals(principalCollection);
    SimpleAuthenticationInfo local = new SimpleAuthenticationInfo("username", "password", "testRealm");
    aggregate.merge(local);
    assertEquals(2, aggregate.getPrincipals().asList().size());
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 23 with PrincipalCollection

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

the class AbstractRememberMeManagerTest method testGetRememberedPrincipalsWithEmptySerializedBytes.

/**
 * Tests the {@link AbstractRememberMeManager#getRememberedPrincipals(SubjectContext)} method
 * implementation when the internal
 * {@link AbstractRememberMeManager#getRememberedSerializedIdentity(SubjectContext)} method
 * returns null or empty bytes.
 */
@Test
public void testGetRememberedPrincipalsWithEmptySerializedBytes() {
    AbstractRememberMeManager rmm = new DummyRememberMeManager();
    // Since the dummy's getRememberedSerializedIdentity implementation returns an empty byte
    // array, we should be ok:
    PrincipalCollection principals = rmm.getRememberedPrincipals(new DefaultSubjectContext());
    assertNull(principals);
    // try with a null return value too:
    rmm = new DummyRememberMeManager() {

        @Override
        protected byte[] getRememberedSerializedIdentity(SubjectContext subjectContext) {
            return null;
        }
    };
    principals = rmm.getRememberedPrincipals(new DefaultSubjectContext());
    assertNull(principals);
}
Also used : SubjectContext(org.apache.shiro.subject.SubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Test(org.junit.Test)

Example 24 with PrincipalCollection

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

the class HelloController method home.

@SuppressWarnings("Duplicates")
@RequestMapping("/")
public String home(HttpServletRequest request, Model model) {
    String name = "World";
    Subject subject = SecurityUtils.getSubject();
    PrincipalCollection principalCollection = subject.getPrincipals();
    if (principalCollection != null && !principalCollection.isEmpty()) {
        Collection<Map> principalMaps = subject.getPrincipals().byType(Map.class);
        if (CollectionUtils.isEmpty(principalMaps)) {
            name = subject.getPrincipal().toString();
        } else {
            name = (String) principalMaps.iterator().next().get("username");
        }
    }
    model.addAttribute("name", name);
    return "hello";
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Map(java.util.Map) Subject(org.apache.shiro.subject.Subject) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with PrincipalCollection

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

the class AbstractAuthorizationAnnotationTest method bindUser.

protected void bindUser() {
    PrincipalCollection principals = new SimplePrincipalCollection("test", realm.getName());
    bind(new Subject.Builder(securityManager).principals(principals).buildSubject());
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Subject(org.apache.shiro.subject.Subject)

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