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; }
@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 = userAuthenticated; Set<UserPrincipal> authenticatedUsers = subject.getPrincipals(UserPrincipal.class); Set<Principal> principals = subject.getPrincipals(); if (result) { principals.add(new UserPrincipal(username)); } // assign roles to any other UserPrincipal for (UserPrincipal authenticatedUser : authenticatedUsers) { List<String> roles = new ArrayList<>(); try { String dn = resolveDN(authenticatedUser.getName(), roles); resolveRolesForDN(context, dn, authenticatedUser.getName(), roles); } catch (NamingException e) { closeContext(); FailedLoginException ex = new FailedLoginException("Error contacting LDAP"); ex.initCause(e); throw ex; } } for (RolePrincipal gp : groups) { principals.add(gp); } clear(); return result; }
@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) { } }