/** * Get the principals of the logged in user, in this case the distinguished name. * * @return the distinguished name of the logged in user. */ public String getPrincipal() { Authentication authentication = SecurityContextHolder.getContext() .getAuthentication(); if (authentication == null) { log.warn("No Authentication object set in SecurityContext - returning empty String as Principal"); return ""; } Object principal = authentication.getPrincipal(); if (principal instanceof LdapUserDetails) { LdapUserDetails details = (LdapUserDetails) principal; return details.getDn(); } else if (authentication instanceof AnonymousAuthenticationToken) { if (log.isDebugEnabled()) { log.debug("Anonymous Authentication, returning empty String as Principal"); } return ""; } else { throw new IllegalArgumentException( "The principal property of the authentication object" + "needs to be an LdapUserDetails."); } }
private static LdapUserDetails mockLdapUserDetails() { userDetails = mock(LdapUserDetails.class); setupGeneralExpectations(userDetails); when(userDetails.getDn()).thenReturn(DN); return userDetails; }
return new AuthenticationResponse(userDetails.getDn(), credentials.getUsername(), expiration, issuer); } else { logger.warn(String.format("Unable to determine user DN for %s, using username.", authentication.getName()));
@Test public void testShadowUserCreationDisabled() throws Exception { String dn = "cn="+userName+",ou=Users,dc=test,dc=com"; String origin = LDAP; LdapUserDetails ldapUserDetails = mock(LdapUserDetails.class); mockUserDetails(ldapUserDetails); when(ldapUserDetails.getDn()).thenReturn(dn); manager = new LdapLoginAuthenticationManager(null) { @Override protected boolean isAddNewShadowUser() { return false; } }; setupManager(); manager.setOrigin(origin); when(uaaUserDatabase.retrieveUserByName(eq(userName), eq(origin))).thenReturn(null); when(inputAuth.getPrincipal()).thenReturn(ldapUserDetails); try { manager.authenticate(inputAuth); fail("Expected authentication to fail with an exception."); } catch (AccountNotPreCreatedException ex) { assertThat(ex.getMessage(), containsString("user account must be pre-created")); } verify(applicationEventPublisher, times(0)).publishEvent(any()); }
@Test public void testAuthenticateLdapUserDetailsPrincipal() throws Exception { String dn = "cn="+userName+",ou=Users,dc=test,dc=com"; String origin = LDAP; LdapUserDetails ldapUserDetails = mock(LdapUserDetails.class); mockUserDetails(ldapUserDetails); when(ldapUserDetails.getDn()).thenReturn(dn); manager = new LdapLoginAuthenticationManager(null); setupManager(); manager.setProviderProvisioning(null); manager.setOrigin(origin); when(user.getOrigin()).thenReturn(origin); when(uaaUserDatabase.retrieveUserByName(eq(userName), eq(origin))).thenReturn(user); when(inputAuth.getPrincipal()).thenReturn(ldapUserDetails); Authentication result = manager.authenticate(inputAuth); assertNotNull(result); assertEquals(UaaAuthentication.class, result.getClass()); UaaAuthentication uaaAuthentication = (UaaAuthentication)result; assertEquals(userName,uaaAuthentication.getPrincipal().getName()); assertEquals(origin,uaaAuthentication.getPrincipal().getOrigin()); assertEquals(userId, uaaAuthentication.getPrincipal().getId()); }
when(baseLdapUserDetails.getDn()).thenReturn(dn); HashMap<String, String[]> ldapAttrs = new HashMap<>(); String ldapMailAttrName = "email";
public Essence(LdapUserDetails copyMe) { setDn(copyMe.getDn()); setUsername(copyMe.getUsername()); setPassword(copyMe.getPassword()); setEnabled(copyMe.isEnabled()); setAccountNonExpired(copyMe.isAccountNonExpired()); setCredentialsNonExpired(copyMe.isCredentialsNonExpired()); setAccountNonLocked(copyMe.isAccountNonLocked()); setAuthorities(copyMe.getAuthorities()); }
public ExtendedLdapUserImpl(LdapUserDetails details) { setDn(details.getDn()); setUsername(details.getUsername()); setPassword(details.getPassword()); setEnabled(details.isEnabled()); setAccountNonExpired(details.isAccountNonExpired()); setCredentialsNonExpired(details.isCredentialsNonExpired()); setAccountNonLocked(details.isAccountNonLocked()); setAuthorities(details.getAuthorities()); } public ExtendedLdapUserImpl(LdapUserDetails details, Map<String,String[]> attributes) {
public String getDn() { return delegate.getDn(); }
/** * Get the principals of the logged in user, in this case the distinguished * name. * * @return the distinguished name of the logged in user. */ public String getPrincipal() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { log.warn("No Authentication object set in SecurityContext - returning empty String as Principal"); return ""; } Object principal = authentication.getPrincipal(); if (principal instanceof LdapUserDetails) { LdapUserDetails details = (LdapUserDetails) principal; return details.getDn(); } else if (authentication instanceof AnonymousAuthenticationToken) { if (log.isDebugEnabled()) { log.debug("Anonymous Authentication, returning empty String as Principal"); } return ""; } else { throw new IllegalArgumentException("The principal property of the authentication object" + "needs to be an LdapUserDetails."); } }
/** * Get the principals of the logged in user, in this case the distinguished name. * * @return the distinguished name of the logged in user. */ public String getPrincipal() { Authentication authentication = SecurityContextHolder.getContext() .getAuthentication(); if (authentication == null) { log.warn("No Authentication object set in SecurityContext - returning empty String as Principal"); return ""; } Object principal = authentication.getPrincipal(); if (principal instanceof LdapUserDetails) { LdapUserDetails details = (LdapUserDetails) principal; return details.getDn(); } else if (authentication instanceof AnonymousAuthenticationToken) { if (log.isDebugEnabled()) { log.debug("Anonymous Authentication, returning empty String as Principal"); } return ""; } else { throw new IllegalArgumentException( "The principal property of the authentication object" + "needs to be an LdapUserDetails."); } }
private Collection<String> getGroups(LdapUserDetails userDetails, String username) { String groupSearchBase = settings.getGroupSearchBase(); if (StringUtils.isNotBlank(groupSearchBase)) { String groupSearchFilter = settings.getGroupSearchFilter(); if (StringUtils.isBlank(groupSearchFilter)) { groupSearchFilter = "(member={0})"; } String groupNameAttribute = settings.getGroupNameAttribute(); if (StringUtils.isBlank(groupNameAttribute)) { groupNameAttribute = "cn"; } return ldapTemplate.searchForSingleAttributeValues( groupSearchBase, groupSearchFilter, new String[]{userDetails.getDn()}, groupNameAttribute ); } else { return Collections.emptySet(); } }
return new AuthenticationResponse(userDetails.getDn(), username, expiration, issuer); } else { logger.warn(String.format("Unable to determine user DN for %s, using username.", authentication.getName()));
public Essence(LdapUserDetails copyMe) { setDn(copyMe.getDn()); setUsername(copyMe.getUsername()); setPassword(copyMe.getPassword()); setEnabled(copyMe.isEnabled()); setAccountNonExpired(copyMe.isAccountNonExpired()); setCredentialsNonExpired(copyMe.isCredentialsNonExpired()); setAccountNonLocked(copyMe.isAccountNonLocked()); setAuthorities(copyMe.getAuthorities()); }
public Essence(LdapUserDetails copyMe) { setDn(copyMe.getDn()); setUsername(copyMe.getUsername()); setPassword(copyMe.getPassword()); setEnabled(copyMe.isEnabled()); setAccountNonExpired(copyMe.isAccountNonExpired()); setCredentialsNonExpired(copyMe.isCredentialsNonExpired()); setAccountNonLocked(copyMe.isAccountNonLocked()); setAuthorities(copyMe.getAuthorities()); }