Search in sources :

Example 26 with SimplePrincipalCollection

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

the class RestSecurityTest method testNotSetSubjectOnClient.

@Test
public void testNotSetSubjectOnClient() throws Exception {
    Element samlToken = readDocument("/saml.xml").getDocumentElement();
    Subject subject = mock(Subject.class);
    SecurityAssertion assertion = mock(SecurityAssertion.class);
    SecurityToken token = new SecurityToken(UUID.randomUUID().toString(), samlToken, new Date(), new Date());
    when(assertion.getSecurityToken()).thenReturn(token);
    when(subject.getPrincipals()).thenReturn(new SimplePrincipalCollection(assertion, "sts"));
    WebClient client = WebClient.create("http://example.org");
    RestSecurity.setSubjectOnClient(subject, client);
    assertNull(client.getHeaders().get(RestSecurity.AUTH_HEADER));
}
Also used : SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) Element(org.w3c.dom.Element) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SecurityAssertion(ddf.security.assertion.SecurityAssertion) WebClient(org.apache.cxf.jaxrs.client.WebClient) Subject(ddf.security.Subject) Date(java.util.Date) Test(org.junit.Test)

Example 27 with SimplePrincipalCollection

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

the class AbstractStsRealm method doGetAuthenticationInfo.

/**
     * Perform authentication based on the supplied token.
     */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
    String method = "doGetAuthenticationInfo(    AuthenticationToken token )";
    Object credential;
    if (token instanceof SAMLAuthenticationToken) {
        credential = token.getCredentials();
    } else if (token instanceof BaseAuthenticationToken) {
        credential = ((BaseAuthenticationToken) token).getCredentialsAsXMLString();
    } else {
        credential = token.getCredentials().toString();
    }
    if (credential == null) {
        String msg = "Unable to authenticate credential.  A NULL credential was provided in the supplied authentication token. This may be due to an error with the SSO server that created the token.";
        LOGGER.info(msg);
        throw new AuthenticationException(msg);
    } else {
        //removed the credentials from the log message for now, I don't think we should be dumping user/pass into log
        LOGGER.debug("Received credentials.");
    }
    SecurityToken securityToken;
    if (token instanceof SAMLAuthenticationToken && credential instanceof SecurityToken) {
        securityToken = renewSecurityToken((SecurityToken) credential);
    } else {
        securityToken = requestSecurityToken(credential);
    }
    LOGGER.debug("Creating token authentication information with SAML.");
    SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
    SimplePrincipalCollection principals = new SimplePrincipalCollection();
    SecurityAssertion assertion = new SecurityAssertionImpl(securityToken);
    principals.add(assertion.getPrincipal(), NAME);
    principals.add(assertion, NAME);
    simpleAuthenticationInfo.setPrincipals(principals);
    simpleAuthenticationInfo.setCredentials(credential);
    return simpleAuthenticationInfo;
}
Also used : SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) AuthenticationException(org.apache.shiro.authc.AuthenticationException) BaseAuthenticationToken(org.codice.ddf.security.handler.api.BaseAuthenticationToken) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SecurityAssertion(ddf.security.assertion.SecurityAssertion) SAMLAuthenticationToken(org.codice.ddf.security.handler.api.SAMLAuthenticationToken) SecurityAssertionImpl(ddf.security.assertion.impl.SecurityAssertionImpl)

Example 28 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection 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 29 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection 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 30 with SimplePrincipalCollection

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

the class AbstractDownloadsStatusEventPublisherTest 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)

Aggregations

SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)55 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)26 Test (org.junit.Test)25 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)11 ArrayList (java.util.ArrayList)7 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)7 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)7 Realm (org.apache.shiro.realm.Realm)7 Principal (java.security.Principal)6 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)6 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)6 Subject (org.apache.shiro.subject.Subject)6 Subject (ddf.security.Subject)5 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)5 SimpleAccount (org.apache.shiro.authc.SimpleAccount)5 AuthorizingRealm (org.apache.shiro.realm.AuthorizingRealm)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 SecurityAssertion (ddf.security.assertion.SecurityAssertion)4 HashSet (java.util.HashSet)4 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)4