@Test public void loadUserByUsernameWhenUserFoundThenNotNull() { assertThat(users.findByUsername("user").block()).isNotNull(); }
@Test public void authenticateWhenPasswordServiceAndUpgradeFalseThenNotUpdated() { when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(this.user)); when(this.encoder.matches(any(), any())).thenReturn(true); when(this.encoder.upgradeEncoding(any())).thenReturn(false); this.manager.setPasswordEncoder(this.encoder); this.manager.setUserDetailsPasswordService(this.userDetailsPasswordService); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( this.user, this.user.getPassword()); Authentication result = this.manager.authenticate(token).block(); verifyZeroInteractions(this.userDetailsPasswordService); }
@Test public void loadUserByUsernameWhenUserFoundThenNotNull() { assertThat(users.findByUsername("user").block()).isNotNull(); }
@Test public void findByUsernameWhenUserFoundThenNotNull() { assertThat(users.findByUsername("user").block()).isNotNull(); }
@Test public void authenticateWhenPasswordEncoderAndSuccessThenSuccess() { this.manager.setPasswordEncoder(this.passwordEncoder); when(this.passwordEncoder.matches(any(), any())).thenReturn(true); User user = new User(this.username, this.password, AuthorityUtils.createAuthorityList("ROLE_USER")); when(this.repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user)); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( this.username, this.password); Authentication authentication = this.manager.authenticate(token).block(); assertThat(authentication).isEqualTo(authentication); }
@Test public void authenticateWhenPostAuthenticationChecksNotSet() { when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(this.user)); when(this.encoder.matches(any(), any())).thenReturn(true); this.manager.setPasswordEncoder(this.encoder); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( this.user, this.user.getPassword()); this.manager.authenticate(token).block(); verifyZeroInteractions(this.postAuthenticationChecks); } }
@Test public void authenticateWhenPasswordServiceAndBadCredentialsThenNotUpdated() { when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(this.user)); when(this.encoder.matches(any(), any())).thenReturn(false); this.manager.setPasswordEncoder(this.encoder); this.manager.setUserDetailsPasswordService(this.userDetailsPasswordService); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( this.user, this.user.getPassword()); assertThatThrownBy(() -> this.manager.authenticate(token).block()) .isInstanceOf(BadCredentialsException.class); verifyZeroInteractions(this.userDetailsPasswordService); }
@Test public void authentiateWhenCustomSchedulerThenUsed() { when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(this.user)); when(this.encoder.matches(any(), any())).thenReturn(true); this.manager.setScheduler(this.scheduler); this.manager.setPasswordEncoder(this.encoder); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( this.user, this.user.getPassword()); Authentication result = this.manager.authenticate(token).block(); verify(this.scheduler).schedule(any()); }
@Test public void authenticateWhenPasswordServiceThenUpdated() { String encodedPassword = "encoded"; when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(this.user)); when(this.encoder.matches(any(), any())).thenReturn(true); when(this.encoder.upgradeEncoding(any())).thenReturn(true); when(this.encoder.encode(any())).thenReturn(encodedPassword); when(this.userDetailsPasswordService.updatePassword(any(), any())).thenReturn(Mono.just(this.user)); this.manager.setPasswordEncoder(this.encoder); this.manager.setUserDetailsPasswordService(this.userDetailsPasswordService); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( this.user, this.user.getPassword()); Authentication result = this.manager.authenticate(token).block(); verify(this.encoder).encode(this.user.getPassword()); verify(this.userDetailsPasswordService).updatePassword(eq(this.user), eq(encodedPassword)); }
@Test public void authenticateWhenPostAuthenticationChecksFail() { when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(this.user)); doThrow(new LockedException("account is locked")).when(this.postAuthenticationChecks).check(any()); when(this.encoder.matches(any(), any())).thenReturn(true); this.manager.setPasswordEncoder(this.encoder); this.manager.setPostAuthenticationChecks(this.postAuthenticationChecks); assertThatExceptionOfType(LockedException.class) .isThrownBy(() -> this.manager.authenticate(new UsernamePasswordAuthenticationToken(this.user, this.user.getPassword())).block()) .withMessage("account is locked"); verify(this.postAuthenticationChecks).check(eq(this.user)); }
@Test public void authenticateWhenUserNotFoundThenBadCredentials() { when(repository.findByUsername(username)).thenReturn(Mono.empty()); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); Mono<Authentication> authentication = manager.authenticate(token); StepVerifier .create(authentication) .expectError(BadCredentialsException.class) .verify(); }
@Test public void authenticateWhenSuccessThenSuccess() { UserDetails user = PasswordEncodedUser.withUsername(this.username) .password(this.password) .roles("USER") .build(); when(repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user)); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); Authentication authentication = manager.authenticate(token).block(); assertThat(authentication).isEqualTo(authentication); }
@Override public Mono<Authentication> authenticate(Authentication authentication) { final String username = authentication.getName(); final String presentedPassword = (String) authentication.getCredentials(); return this.userDetailsService.findByUsername(username) .publishOn(this.scheduler) .filter(u -> this.passwordEncoder.matches(presentedPassword, u.getPassword())) .switchIfEmpty(Mono.defer(() -> Mono.error(new BadCredentialsException("Invalid Credentials")))) .flatMap(u -> { boolean upgradeEncoding = this.userDetailsPasswordService != null && this.passwordEncoder.upgradeEncoding(u.getPassword()); if (upgradeEncoding) { String newPassword = this.passwordEncoder.encode(presentedPassword); return this.userDetailsPasswordService.updatePassword(u, newPassword); } return Mono.just(u); }) .doOnNext(this.postAuthenticationChecks::check) .map(u -> new UsernamePasswordAuthenticationToken(u, u.getPassword(), u.getAuthorities()) ); }
@Override public Mono<Authentication> authenticate(Authentication authentication) { final String username = authentication.getName(); final String presentedPassword = (String) authentication.getCredentials(); return this.userDetailsService.findByUsername(username) .publishOn(this.scheduler) .filter(u -> this.passwordEncoder.matches(presentedPassword, u.getPassword())) .switchIfEmpty(Mono.defer(() -> Mono.error(new BadCredentialsException("Invalid Credentials")))) .flatMap(u -> { boolean upgradeEncoding = this.userDetailsPasswordService != null && this.passwordEncoder.upgradeEncoding(u.getPassword()); if (upgradeEncoding) { String newPassword = this.passwordEncoder.encode(presentedPassword); return this.userDetailsPasswordService.updatePassword(u, newPassword); } return Mono.just(u); }) .map(u -> new UsernamePasswordAuthenticationToken(u, u.getPassword(), u.getAuthorities()) ); }
@Test public void authenticateWhenPasswordEncoderAndFailThenFail() { this.manager.setPasswordEncoder(this.passwordEncoder); when(this.passwordEncoder.matches(any(), any())).thenReturn(false); User user = new User(this.username, this.password, AuthorityUtils.createAuthorityList("ROLE_USER")); when(this.repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user)); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( this.username, this.password); Mono<Authentication> authentication = this.manager.authenticate(token); StepVerifier .create(authentication) .expectError(BadCredentialsException.class) .verify(); } }
@Test public void authenticateWhenPasswordNotEqualThenBadCredentials() { UserDetails user = PasswordEncodedUser.withUsername(this.username) .password(this.password) .roles("USER") .build(); when(repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user)); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, this.password + "INVALID"); Mono<Authentication> authentication = manager.authenticate(token); StepVerifier .create(authentication) .expectError(BadCredentialsException.class) .verify(); }
@Test public void passwordUpdateManagerUsed() { this.spring.register(MapReactiveUserDetailsServiceConfig.class).autowire(); WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.springSecurityFilterChain).build(); client .get() .uri("/") .headers(h -> h.setBasicAuth("user", "password")) .exchange() .expectStatus().isOk(); ReactiveUserDetailsService users = this.spring.getContext().getBean(ReactiveUserDetailsService.class); assertThat(users.findByUsername("user").block().getPassword()).startsWith("{bcrypt}"); }
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { return this.userDetailsService.findByUsername(username).block(); } }
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { return this.userDetailsService.findByUsername(username).block(); } }
public void onStartup() { userDetailsService .findByUsername(properties.getAdmin().getUsername()) // Check if the user already exists .doOnError(e -> e instanceof UsernameNotFoundException, e -> { // Doesn't exist. So, create it. log.debug("Creating first admin ... "); U user = createAdminUser(); userRepository.insert(user).doOnError(err -> { log.warn("Error creating initial admin " + err); }).subscribe(); log.debug("Created first admin."); }).subscribe(); }