private void resolveRolesForDN(DirContext context, String dn, String username, List<String> roles) throws NamingException { addRoles(context, dn, username, roles); if (logger.isDebugEnabled()) { logger.debug("Roles " + roles + " for user " + username); } for (String role : roles) { groups.add(new RolePrincipal(role)); } }
@Override public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) { this.subject = subject; this.callbackHandler = callbackHandler; debug = "true".equalsIgnoreCase((String) options.get("debug")); credentialsInvalidate = "true".equalsIgnoreCase((String) options.get("credentialsInvalidate")); if (options.get(GUEST_USER) != null) { userName = (String) options.get(GUEST_USER); } if (options.get(GUEST_ROLE) != null) { roleName = (String) options.get(GUEST_ROLE); } principals.add(new UserPrincipal(userName)); principals.add(new RolePrincipal(roleName)); if (debug) { logger.debug("Initialized debug=" + debug + " guestUser=" + userName + " guestGroup=" + roleName); } }
/** * Overriding to complete login process. Standard JAAS. */ @Override public boolean commit() throws LoginException { principals.add(new UserPrincipal(username)); for (String role : getUserRoles(username)) { principals.add(new RolePrincipal(role)); } subject.getPrincipals().addAll(principals); clear(); if (debug) { logger.debug("commit"); } return true; }
@Override public boolean commit() throws LoginException { boolean result = loginSucceeded; if (result) { principals.add(new UserPrincipal(user)); List<String> roles = configuration.getRole(user); if (roles != null) { for (String role : roles) { principals.add(new RolePrincipal(role)); } } subject.getPrincipals().addAll(principals); } // will whack loginSucceeded clear(); logger.debug("commit, result: " + result); return result; }
@Override public boolean commit() throws LoginException { boolean result = loginSucceeded; Set<UserPrincipal> authenticatedUsers = subject.getPrincipals(UserPrincipal.class); if (result) { UserPrincipal userPrincipal = new UserPrincipal(user); principals.add(userPrincipal); authenticatedUsers.add(userPrincipal); } // populate roles for UserPrincipal from other login modules too for (UserPrincipal userPrincipal : authenticatedUsers) { Set<String> matchedRoles = roles.get(userPrincipal.getName()); if (matchedRoles != null) { for (String entry : matchedRoles) { principals.add(new RolePrincipal(entry)); } } } subject.getPrincipals().addAll(principals); // will whack loginSucceeded clear(); if (debug) { logger.debug("commit, result: " + result); } return result; }
@Override public boolean commit() throws LoginException { boolean result = loginSucceeded; try { if (result) { if (existingSubject == null) { UserPrincipal userPrincipal = new UserPrincipal(user); principals.add(userPrincipal); LOG.debugv("Adding user principal for: {0}", user); for (String entry : roles) { LOG.debugv("Adding role principal for: {0}", entry); principals.add(new RolePrincipal(entry)); } subject.getPrincipals().addAll(principals); if(remoteConnection != null) { AUTHENTICATED_CONNECTIONS.put(remoteConnection, subject); } } else { subject.getPrincipals().addAll(existingSubject.getPrincipals()); } } clear(); LOG.debugv("commit, result: {0}", result); } catch (RuntimeException | Error e) { LOG.error(e); throw e; } return result; }
@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")); // String password = users.getProperty(user); /*if (password == null) { throw new FailedLoginException("User does not exist: " + user); }*/ return true; }
@Test public void testArguments() { RolePrincipal principal = new RolePrincipal("FOO"); assertEquals("FOO", principal.getName()); try { new RolePrincipal(null); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException ingore) { } }
@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()); }
@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()); } }
@Test public void testHash() { RolePrincipal p1 = new RolePrincipal("FOO"); RolePrincipal p2 = new RolePrincipal("FOO"); assertEquals(p1.hashCode(), p1.hashCode()); assertEquals(p1.hashCode(), p2.hashCode()); }
@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)); } }