@Override
    public Mono<UserDetails> findByUsername(String username) {
        return userService.getUser(username)
            .onErrorMap(UserNotFoundException.class,
                e -> new BadCredentialsException("Invalid Credentials"))
            .flatMap(user -> {
                var name = user.getMetadata().getName();
                var userBuilder = User.withUsername(name)
                    .password(user.getSpec().getPassword())
                    .disabled(requireNonNullElse(user.getSpec().getDisabled(), false));
                var setAuthorities = roleService.getRolesByUsername(name)
                    // every authenticated user should have authenticated and anonymous roles.
                    .concatWithValues(AUTHENTICATED_ROLE_NAME, ANONYMOUS_ROLE_NAME)
                    .map(roleName -> new SimpleGrantedAuthority(ROLE_PREFIX + roleName))
                    .distinct()
                    .collectList()
                    .doOnNext(userBuilder::authorities);

                return setAuthorities.then(Mono.fromSupplier(() -> {
                    var twoFactorAuthSettings = TwoFactorUtils.getTwoFactorAuthSettings(user);
                    return new HaloUser.Builder(userBuilder.build())
                        .twoFactorAuthEnabled(
                            (!twoFactorAuthDisabled) && twoFactorAuthSettings.isAvailable()
                        )
                        .totpEncryptedSecret(user.getSpec().getTotpEncryptedSecret())
                        .build();
                }));
            });
    }
@Test
    void shouldFindHaloUserDetailsWith2faEnabledWhen2faEnabledAndTotpConfigured() {
        var fakeUser = createFakeUser();
        fakeUser.getSpec().setTwoFactorAuthEnabled(true);
        fakeUser.getSpec().setTotpEncryptedSecret("fake-totp-encrypted-secret");
        when(userService.getUser("faker")).thenReturn(Mono.just(fakeUser));
        when(roleService.getRolesByUsername("faker")).thenReturn(Flux.empty());
        userDetailService.findByUsername("faker")
            .as(StepVerifier::create)
            .assertNext(userDetails -> {
                assertInstanceOf(HaloUserDetails.class, userDetails);
                assertTrue(((HaloUserDetails) userDetails).isTwoFactorAuthEnabled());
            })
            .verifyComplete();
    }