Search in sources :

Example 6 with RolePrincipal

use of org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal 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 7 with RolePrincipal

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

the class RolePrincipalTest method testEquals.

@Test
public void testEquals() {
    RolePrincipal p1 = new RolePrincipal("FOO");
    RolePrincipal p2 = new RolePrincipal("FOO");
    RolePrincipal p3 = new RolePrincipal("BAR");
    assertTrue(p1.equals(p1));
    assertTrue(p1.equals(p2));
    assertFalse(p1.equals(null));
    assertFalse(p1.equals("FOO"));
    assertFalse(p1.equals(p3));
}
Also used : RolePrincipal(org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal) Test(org.junit.Test)

Example 8 with RolePrincipal

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

the class DummyLoginModule method login.

@Override
public boolean login() throws LoginException {
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PasswordCallback("Password: ", false);
    try {
        callbackHandler.handle(callbacks);
    } catch (IOException ioe) {
        throw new LoginException(ioe.getMessage());
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException(uce.getMessage() + " not available to obtain information from user");
    }
    String user = ((NameCallback) callbacks[0]).getName();
    char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
    if (tmpPassword == null) {
        tmpPassword = new char[0];
    }
    if (user == null) {
        throw new FailedLoginException("User is null");
    }
    subject.getPrincipals().add(new RolePrincipal("amq"));
    return true;
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RolePrincipal(org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal)

Example 9 with RolePrincipal

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

the class LDAPModuleRoleExpansionTest method testRoleExpansion.

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

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (int i = 0; i < callbacks.length; i++) {
                if (callbacks[i] instanceof NameCallback) {
                    ((NameCallback) callbacks[i]).setName("first");
                } else if (callbacks[i] instanceof PasswordCallback) {
                    ((PasswordCallback) callbacks[i]).setPassword("secret".toCharArray());
                } else {
                    throw new UnsupportedCallbackException(callbacks[i]);
                }
            }
        }
    });
    context.login();
    Subject subject = context.getSubject();
    boolean isAdmin = false;
    boolean isUser = false;
    for (Principal principal : subject.getPrincipals()) {
        if (principal instanceof RolePrincipal) {
            RolePrincipal groupPrincipal = (RolePrincipal) principal;
            if (groupPrincipal.getName().equalsIgnoreCase("admins"))
                isAdmin = true;
            if (groupPrincipal.getName().equalsIgnoreCase("users"))
                isUser = true;
        }
    }
    // Should be in users by virtue of being in admins
    assertTrue(isAdmin && isUser);
    context.logout();
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) IOException(java.io.IOException) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RolePrincipal(org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal) RolePrincipal(org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal) Principal(java.security.Principal) Test(org.junit.Test)

Aggregations

RolePrincipal (org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal)9 Test (org.junit.Test)6 IOException (java.io.IOException)4 Subject (javax.security.auth.Subject)4 Callback (javax.security.auth.callback.Callback)4 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)4 UserPrincipal (org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal)4 Principal (java.security.Principal)3 CallbackHandler (javax.security.auth.callback.CallbackHandler)3 LoginContext (javax.security.auth.login.LoginContext)3 NameCallback (javax.security.auth.callback.NameCallback)2 PasswordCallback (javax.security.auth.callback.PasswordCallback)2 LoginException (javax.security.auth.login.LoginException)2 HashSet (java.util.HashSet)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1