@Test public void authenticate() { UsersConnectionRepository repo = mock(UsersConnectionRepository.class); SocialUserDetailsService userDetailsService = mock(SocialUserDetailsService.class); SocialAuthenticationProvider provider = new SocialAuthenticationProvider(repo, userDetailsService); DummyUserDetails userDetails = new DummyUserDetails("user1", "pass", "moderator"); Connection<Object> dummyConnection1 = dummyConnection("providerUser1"); Connection<Object> dummyConnection2 = dummyConnection("providerUser2"); Connection<Object> dummyConnection3 = dummyConnection("providerUser3"); Mockito.when(repo.findUserIdsWithConnection(dummyConnection1)).thenReturn(list("user1")); Mockito.when(repo.findUserIdsWithConnection(dummyConnection2)).thenReturn(list("user1", "user2")); Mockito.when(repo.findUserIdsWithConnection(dummyConnection3)).thenReturn(list("user3")); Mockito.when(userDetailsService.loadUserByUserId("user1")).thenReturn(userDetails); Mockito.when(userDetailsService.loadUserByUserId("user2")).thenReturn(new DummyUserDetails("user2", "pass", "moderator")); SocialAuthenticationToken token = new SocialAuthenticationToken(dummyConnection1, null); SocialAuthenticationToken authToken = (SocialAuthenticationToken) provider.authenticate(token); assertNotNull(authToken); assertTrue(token.getPrincipal() == null); assertEquals(token.getConnection(), dummyConnection1); assertTrue(authToken.getPrincipal() instanceof UserDetails); assertEquals(userDetails.getUsername(), authToken.getName()); assertEquals(token.getProviderId(), authToken.getProviderId()); assertEquals(token.getProviderAccountData(), authToken.getProviderAccountData()); try { provider.authenticate(new SocialAuthenticationToken(dummyConnection("someUnknownProviderUser"), null)); fail(); } catch (BadCredentialsException e) { } try { provider.authenticate(new SocialAuthenticationToken(dummyConnection2, null)); fail(); } catch (BadCredentialsException e) { } try { provider.authenticate(new SocialAuthenticationToken(dummyConnection3, null)); fail(); } catch (UsernameNotFoundException e) { } }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(SocialAuthenticationToken.class, authentication, "unsupported authentication type"); Assert.isTrue(!authentication.isAuthenticated(), "already authenticated"); SocialAuthenticationToken authToken = (SocialAuthenticationToken) authentication; String providerId = authToken.getProviderId(); Connection<?> connection = authToken.getConnection(); String userId = toUserId(connection); if (userId == null) { throw new BadCredentialsException("Unknown access token"); } UserDetails userDetails = userDetailsService.loadUserByUserId(userId); if (userDetails == null) { throw new UsernameNotFoundException("Unknown connected account id"); } return new SocialAuthenticationToken(connection, userDetails, authToken.getProviderAccountData(), getAuthorities(providerId, userDetails)); }
SocialAuthenticationProvider implements AuthenticationProvider { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(SocialAuthenticationToken.class, authentication, "unsupported authentication type"); Assert.isTrue(!authentication.isAuthenticated(), "already authenticated"); SocialAuthenticationToken authToken = (SocialAuthenticationToken) authentication; String providerId = authToken.getProviderId(); Connection<?> connection = authToken.getConnection(); String userId = toUserId(connection); if (userId == null) { throw new BadCredentialsException("Unknown access token"); } UserDetails userDetails = userDetailsService.loadUserByUserId(userId); if (userDetails == null) { throw new UsernameNotFoundException("Unknown connected account id"); } return new SocialAuthenticationToken(connection, userDetails, authToken.getProviderAccountData(), getAuthorities(providerId, userDetails)); } }
SocialAuthenticationProvider implements AuthenticationProvider { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(SocialAuthenticationToken.class, authentication, "unsupported authentication type"); Assert.isTrue(!authentication.isAuthenticated(), "already authenticated"); SocialAuthenticationToken authToken = (SocialAuthenticationToken) authentication; String providerId = authToken.getProviderId(); Connection<?> connection = authToken.getConnection(); String userId = toUserId(connection); if (userId == null) { throw new BadCredentialsException("Unknown access token"); } UserDetails userDetails = userDetailsService.loadUserByUserId(userId); if (userDetails == null) { throw new UsernameNotFoundException("Unknown connected account id"); } return new SocialAuthenticationToken(connection, userDetails, authToken.getProviderAccountData(), getAuthorities(providerId, userDetails)); } SocialAuthenticationProvider(UsersConnectionRepository usersConnectionRepository, SocialUserDetailsService userDetailsService); }
SocialAuthenticationProvider implements AuthenticationProvider { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(SocialAuthenticationToken.class, authentication, "unsupported authentication type"); Assert.isTrue(!authentication.isAuthenticated(), "already authenticated"); SocialAuthenticationToken authToken = (SocialAuthenticationToken) authentication; String providerId = authToken.getProviderId(); Connection<?> connection = authToken.getConnection(); String userId = toUserId(connection); if (userId == null) { throw new BadCredentialsException("Unknown access token"); } UserDetails userDetails = userDetailsService.loadUserByUserId(userId); if (userDetails == null) { throw new UsernameNotFoundException("Unknown connected account id"); } return new SocialAuthenticationToken(connection, userDetails, authToken.getProviderAccountData(), getAuthorities(providerId, userDetails)); } SocialAuthenticationProvider(UsersConnectionRepository usersConnectionRepository, SocialUserDetailsService userDetailsService); boolean supports(Class<? extends Object> authentication); Authentication authenticate(Authentication authentication); }
SocialAuthenticationProvider implements AuthenticationProvider { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(SocialAuthenticationToken.class, authentication, "unsupported authentication type"); Assert.isTrue(!authentication.isAuthenticated(), "already authenticated"); SocialAuthenticationToken authToken = (SocialAuthenticationToken) authentication; String providerId = authToken.getProviderId(); Connection<?> connection = authToken.getConnection(); String userId = toUserId(connection); if (userId == null) { throw new BadCredentialsException("Unknown access token"); } UserDetails userDetails = userDetailsService.loadUserByUserId(userId); if (userDetails == null) { throw new UsernameNotFoundException("Unknown connected account id"); } return new SocialAuthenticationToken(connection, userDetails, authToken.getProviderAccountData(), getAuthorities(providerId, userDetails)); } SocialAuthenticationProvider(UsersConnectionRepository usersConnectionRepository, SocialUserDetailsService userDetailsService); boolean supports(Class<? extends Object> authentication); Authentication authenticate(Authentication authentication);  }