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());
}
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));
}
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;
}
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();
}
Aggregations