@Test public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws IOException { UsernamePasswordAuthenticationToken expectedToken = createToken(); UsernamePasswordAuthenticationToken token = mapper .readValue(AUTHENTICATED_STRINGPRINCIPAL_JSON, UsernamePasswordAuthenticationToken.class); assertThat(token).isNotNull(); assertThat(token.isAuthenticated()).isTrue(); assertThat(token.getAuthorities()).isEqualTo(expectedToken.getAuthorities()); }
@Test public void deserializeUnauthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws IOException, JSONException { UsernamePasswordAuthenticationToken token = mapper .readValue(UNAUTHENTICATED_STRINGPRINCIPAL_JSON, UsernamePasswordAuthenticationToken.class); assertThat(token).isNotNull(); assertThat(token.isAuthenticated()).isEqualTo(false); assertThat(token.getAuthorities()).isNotNull().hasSize(0); }
.setUserGroupServiceName(userGroupServiceName); if (auth.getAuthorities().contains(GeoServerRole.AUTHENTICATED_ROLE) == false) { List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>(); roles.addAll(auth.getAuthorities()); roles.add(GeoServerRole.AUTHENTICATED_ROLE); UsernamePasswordAuthenticationToken newAuth =
@Test public void gettersReturnCorrectData() { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( "Test", "Password", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO")); assertThat(token.getPrincipal()).isEqualTo("Test"); assertThat(token.getCredentials()).isEqualTo("Password"); assertThat(AuthorityUtils.authorityListToSet(token.getAuthorities())).contains("ROLE_ONE"); assertThat(AuthorityUtils.authorityListToSet(token.getAuthorities())).contains("ROLE_TWO"); }
@Test public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithUserTest() throws IOException { UsernamePasswordAuthenticationToken token = mapper .readValue(AUTHENTICATED_JSON, UsernamePasswordAuthenticationToken.class); assertThat(token).isNotNull(); assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class); assertThat(((User) token.getPrincipal()).getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER")); assertThat(token.isAuthenticated()).isEqualTo(true); assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER")); }
@Test public void testAuthenticates() { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( "rod", "koala"); token.setDetails("192.168.0.1"); DaoAuthenticationProvider provider = createProvider(); provider.setUserDetailsService(new MockUserDetailsServiceUserRod()); provider.setUserCache(new MockUserCache()); Authentication result = provider.authenticate(token); if (!(result instanceof UsernamePasswordAuthenticationToken)) { fail("Should have returned instance of UsernamePasswordAuthenticationToken"); } UsernamePasswordAuthenticationToken castResult = (UsernamePasswordAuthenticationToken) result; assertThat(castResult.getPrincipal().getClass()).isEqualTo(User.class); assertThat(castResult.getCredentials()).isEqualTo("koala"); assertThat( AuthorityUtils.authorityListToSet(castResult.getAuthorities())).contains( "ROLE_ONE", "ROLE_TWO"); assertThat(castResult.getDetails()).isEqualTo("192.168.0.1"); }
/** * Login endpoint serves as the endpoint to validate a user details is valid and to get any associated * needed properties of the user * @param principal the {@link java.security.Principal} * @return a map of principal properties */ @RequestMapping(value = "/login", method = RequestMethod.POST) public Map<String, Object> login(Principal principal) { Map<String, Object> userDetails = new HashMap<>(); Collection<GrantedAuthority> authorities = ((UsernamePasswordAuthenticationToken) principal).getAuthorities(); ArrayList<String> roles = authorities.stream().map(auth -> auth.getAuthority()) .collect(Collectors.toCollection(ArrayList::new)); userDetails.put("roles", roles); userDetails.put("username", principal.getName()); // TODO refactor -> getting email via getName is wrong! userDetails.put("id", userRepository.findByEmail(principal.getName()).getId()); return userDetails; }
@GetMapping("/paver/v1/userinfo") public Map<String, Object> home(Principal user) { UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) user; return ImmutableMap .<String, Object> builder() .put("username", token.getName()) .put("authorities", token.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(toList())) .build(); } }
roles.addAll(auth.getAuthorities()); if (!auth.getAuthorities().contains(GeoServerRole.AUTHENTICATED_ROLE)) { roles.add(GeoServerRole.AUTHENTICATED_ROLE); && !adminRole.equals("") && !roles.contains(GeoServerRole.ADMIN_ROLE)) { for (GrantedAuthority authority : auth.getAuthorities()) { if (authority.getAuthority().equalsIgnoreCase("ROLE_" + adminRole)) { roles.add(GeoServerRole.ADMIN_ROLE); && !groupAdminRole.equals("") && !roles.contains(GeoServerRole.GROUP_ADMIN_ROLE)) { for (GrantedAuthority authority : auth.getAuthorities()) { if (authority.getAuthority().equalsIgnoreCase("ROLE_" + groupAdminRole)) { roles.add(GeoServerRole.GROUP_ADMIN_ROLE);
roles.addAll(inTok.getAuthorities()); roles.add(GeoServerRole.AUTHENTICATED_ROLE); if (authUser.getRole() == AuthUser.Role.ADMIN) {
roles.addAll(inTok.getAuthorities()); roles.add(GeoServerRole.AUTHENTICATED_ROLE); if (authUser.getRole() == AuthUser.Role.ADMIN) {
AuditElements.AUTHENTICATION_CATEGORY, null, AuditElements.LOGIN_EVENT, Result.SUCCESS, null, isAuthenticated, authentication, "Successfully authenticated, with entitlements: " + token1.getAuthorities()); return token1; }); username[0], token.getAuthorities()); } else { AuthContextUtils.execWithAuthContext(domainKey, () -> {
AuditElements.AUTHENTICATION_CATEGORY, null, AuditElements.LOGIN_EVENT, Result.SUCCESS, null, isAuthenticated, authentication, "Successfully authenticated, with entitlements: " + token1.getAuthorities()); return token1; }); username[0], token.getAuthorities()); } else { AuthContextUtils.execWithAuthContext(domainKey, () -> {