Search in sources :

Example 86 with PrincipalCollection

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

the class ShiroSecurityContext method getUserPrincipal.

@Override
public Principal getUserPrincipal() {
    Principal result;
    Subject subject = getSubject();
    PrincipalCollection shiroPrincipals = subject.getPrincipals();
    if (shiroPrincipals != null) {
        result = shiroPrincipals.oneByType(Principal.class);
        if (result == null) {
            result = new ObjectPrincipal(shiroPrincipals.getPrimaryPrincipal());
        }
    } else {
        result = originalSecurityContext.getUserPrincipal();
    }
    return result;
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Principal(java.security.Principal) Subject(org.apache.shiro.subject.Subject)

Example 87 with PrincipalCollection

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

the class TestKillBillAuth0Realm method testCheckAuth0Connection.

@Test(groups = "external", enabled = false)
public void testCheckAuth0Connection() throws Exception {
    // Convenience method to verify your Auth0 connectivity
    final Properties props = new Properties();
    props.setProperty("org.killbill.security.auth0.url", "https://XXX.us.auth0.com");
    props.setProperty("org.killbill.security.auth0.clientId", "YYY");
    props.setProperty("org.killbill.security.auth0.clientSecret", "ZZZ");
    props.setProperty("org.killbill.security.auth0.apiIdentifier", "WWW");
    props.setProperty("org.killbill.security.auth0.databaseConnectionName", "Username-Password-Authentication");
    props.setProperty("org.killbill.security.auth0.allowedClockSkew", "2000s");
    final ConfigSource customConfigSource = new SimplePropertyConfigSource(props);
    final SecurityConfig securityConfig = new ConfigurationObjectFactory(customConfigSource).build(SecurityConfig.class);
    final KillBillAuth0Realm auth0Realm = new KillBillAuth0Realm(securityConfig, clock);
    final String username = "test@example.com";
    final String password = "password";
    // Check authentication
    final AuthenticationToken token = new UsernamePasswordToken(username, password);
    final AuthenticationInfo authenticationInfo = auth0Realm.getAuthenticationInfo(token);
    System.out.println(authenticationInfo);
    // Check permissions
    final PrincipalCollection principals = new SimplePrincipalCollection(username, username);
    final AuthorizationInfo authorizationInfo = auth0Realm.doGetAuthorizationInfo(principals);
    System.out.println("Roles: " + authorizationInfo.getRoles());
    System.out.println("Permissions: " + authorizationInfo.getStringPermissions());
    // Check JWT
    final Claims claims = auth0Realm.verifyJWT("JWT");
    System.out.println("Token claims: " + claims);
}
Also used : SimplePropertyConfigSource(org.skife.config.SimplePropertyConfigSource) AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) Claims(io.jsonwebtoken.Claims) ConfigurationObjectFactory(org.skife.config.ConfigurationObjectFactory) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Properties(java.util.Properties) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) AuthenticationInfo(org.apache.shiro.authc.AuthenticationInfo) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) SimplePropertyConfigSource(org.skife.config.SimplePropertyConfigSource) ConfigSource(org.skife.config.ConfigSource) SecurityConfig(org.killbill.billing.util.config.definition.SecurityConfig) Test(org.testng.annotations.Test)

Example 88 with PrincipalCollection

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

the class ApacheShiroAuthorizationManager method getCurrentUserName.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("rawtypes")
public String getCurrentUserName() {
    String username = "N/A";
    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)) {
            username = subject.getPrincipal().toString();
        } else {
            username = (String) principalMaps.iterator().next().get(PRINCIPAL_USERNAME);
        }
    }
    return username;
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Map(java.util.Map) Subject(org.apache.shiro.subject.Subject)

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