Search in sources :

Example 1 with UserPrincipal

use of org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal in project activemq-artemis by apache.

the class ActiveMQJAASSecurityManager method getUserFromSubject.

public String getUserFromSubject(Subject subject) {
    String validatedUser = "";
    Set<UserPrincipal> users = subject.getPrincipals(UserPrincipal.class);
    // should only ever be 1 UserPrincipal
    for (UserPrincipal userPrincipal : users) {
        validatedUser = userPrincipal.getName();
    }
    return validatedUser;
}
Also used : UserPrincipal(org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal)

Example 2 with UserPrincipal

use of org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal in project activemq-artemis by apache.

the class CertificateLoginModuleTest method checkPrincipalsMatch.

private void checkPrincipalsMatch(Subject subject) {
    boolean nameFound = false;
    boolean[] rolesFound = new boolean[ROLE_NAMES.size()];
    for (int i = 0; i < rolesFound.length; ++i) {
        rolesFound[i] = false;
    }
    for (Principal currentPrincipal : subject.getPrincipals()) {
        if (currentPrincipal instanceof UserPrincipal) {
            if (currentPrincipal.getName().equals(USER_NAME)) {
                if (!nameFound) {
                    nameFound = true;
                } else {
                    fail("UserPrincipal found twice.");
                }
            } else {
                fail("Unknown UserPrincipal found.");
            }
        } else if (currentPrincipal instanceof RolePrincipal) {
            int principalIdx = ROLE_NAMES.indexOf(((RolePrincipal) currentPrincipal).getName());
            if (principalIdx < 0) {
                fail("Unknown RolePrincipal found.");
            }
            if (!rolesFound[principalIdx]) {
                rolesFound[principalIdx] = true;
            } else {
                fail("RolePrincipal found twice.");
            }
        } else {
            fail("Unknown Principal type found.");
        }
    }
}
Also used : RolePrincipal(org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal) RolePrincipal(org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal) UserPrincipal(org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal) Principal(java.security.Principal) UserPrincipal(org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal)

Example 3 with UserPrincipal

use of org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal in project activemq-artemis by apache.

the class GuestLoginModuleTest method testLoginWithDefaults.

@Test
public void testLoginWithDefaults() throws LoginException {
    LoginContext context = new LoginContext("GuestLoginWithDefaults", new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            assertEquals("Should have no Callbacks", 0, callbacks.length);
        }
    });
    context.login();
    Subject subject = context.getSubject();
    assertEquals("Should have two principals", 2, subject.getPrincipals().size());
    assertEquals("Should have one user principal", 1, subject.getPrincipals(UserPrincipal.class).size());
    assertTrue("User principal is 'guest'", subject.getPrincipals(UserPrincipal.class).contains(new UserPrincipal("guest")));
    assertEquals("Should have one group principal", 1, subject.getPrincipals(RolePrincipal.class).size());
    assertTrue("Role principal is 'guests'", subject.getPrincipals(RolePrincipal.class).contains(new RolePrincipal("guests")));
    context.logout();
    assertEquals("Should have zero principals", 0, subject.getPrincipals().size());
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) Callback(javax.security.auth.callback.Callback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RolePrincipal(org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal) Subject(javax.security.auth.Subject) UserPrincipal(org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal) Test(org.junit.Test)

Example 4 with UserPrincipal

use of org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal in project activemq-artemis by apache.

the class GuestLoginModuleTest method testLogin.

@Test
public void testLogin() throws LoginException {
    LoginContext context = new LoginContext("GuestLogin", new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            assertEquals("Should have no Callbacks", 0, callbacks.length);
        }
    });
    context.login();
    Subject subject = context.getSubject();
    assertEquals("Should have two principals", 2, subject.getPrincipals().size());
    assertEquals("Should have one user principal", 1, subject.getPrincipals(UserPrincipal.class).size());
    assertTrue("User principal is 'foo'", subject.getPrincipals(UserPrincipal.class).contains(new UserPrincipal("foo")));
    assertEquals("Should have one group principal", 1, subject.getPrincipals(RolePrincipal.class).size());
    assertTrue("Role principal is 'bar'", subject.getPrincipals(RolePrincipal.class).contains(new RolePrincipal("bar")));
    context.logout();
    assertEquals("Should have zero principals", 0, subject.getPrincipals().size());
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) Callback(javax.security.auth.callback.Callback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RolePrincipal(org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal) Subject(javax.security.auth.Subject) UserPrincipal(org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal) Test(org.junit.Test)

Example 5 with UserPrincipal

use of org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal in project activemq-artemis by apache.

the class UserPrincipalTest method testArguments.

@Test
public void testArguments() {
    UserPrincipal principal = new UserPrincipal("FOO");
    assertEquals("FOO", principal.getName());
    try {
        new UserPrincipal(null);
        fail("Should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException ignore) {
    }
}
Also used : UserPrincipal(org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal) Test(org.junit.Test)

Aggregations

UserPrincipal (org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal)7 Test (org.junit.Test)5 RolePrincipal (org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal)3 IOException (java.io.IOException)2 Subject (javax.security.auth.Subject)2 Callback (javax.security.auth.callback.Callback)2 CallbackHandler (javax.security.auth.callback.CallbackHandler)2 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)2 LoginContext (javax.security.auth.login.LoginContext)2 Principal (java.security.Principal)1