Search in sources :

Example 1 with UserNotFoundException

use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.

the class PasswordChangeController method changePassword.

@GetMapping
public String changePassword(Model model) {
    User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
    if (!user.isEmailVerified()) {
        log.warn("User not verified and tried to change the password!");
        model.addAttribute("error", "The user is not verified, and hence can't change the password");
    } else {
        if (!model.containsAttribute("password")) {
            PasswordChange passwordChange = new PasswordChange();
            model.addAttribute("password", passwordChange);
        }
        model.addAttribute("blacklist", Arrays.asList(user.getName(), user.getEmail(), user.getUserId()));
    }
    return "user/change_password";
}
Also used : UserNotFoundException(amu.zhcet.data.user.UserNotFoundException) User(amu.zhcet.data.user.User) PasswordChange(amu.zhcet.auth.password.PasswordChange) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with UserNotFoundException

use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.

the class TwoFAService method generate2FASecret.

/**
 * Generates a random secret to be be seed of TOTP secret and QR Code URL
 * @return {@link TwoFASecret} enclosing the user ID and secret
 */
TwoFASecret generate2FASecret() {
    User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
    if (user.getTotpSecret() != null) {
        log.warn("User {} is overwriting TOTP with new one", user.getUserId());
    }
    String secret = Base32.random();
    log.debug("Adding secret {} to user {}", secret, user.getUserId());
    TwoFASecret twoFASecret = new TwoFASecret(user.getUserId(), secret);
    log.debug("QR code URL: {}", twoFASecret.getQrUrl());
    return twoFASecret;
}
Also used : UserNotFoundException(amu.zhcet.data.user.UserNotFoundException) User(amu.zhcet.data.user.User)

Example 3 with UserNotFoundException

use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.

the class TwoFAService method disable2FA.

void disable2FA() {
    User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
    user.setUsing2fa(false);
    user.setTotpSecret(null);
    userService.save(user);
}
Also used : UserNotFoundException(amu.zhcet.data.user.UserNotFoundException) User(amu.zhcet.data.user.User)

Example 4 with UserNotFoundException

use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.

the class PasswordChangeController method savePassword.

@PostMapping
public String savePassword(@Valid PasswordChange passwordChange, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
    User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
    if (bindingResult.hasErrors()) {
        redirectAttributes.addFlashAttribute("password", passwordChange);
        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.password", bindingResult);
    } else {
        try {
            passwordChangeService.changePassword(user, passwordChange);
            redirectAttributes.addFlashAttribute("flash_messages", Flash.success("Password was changed successfully"));
            return "redirect:/profile/settings#account";
        } catch (PasswordValidationException pve) {
            redirectAttributes.addFlashAttribute("pass_errors", pve.getMessage());
        }
    }
    return "redirect:/profile/password/change";
}
Also used : UserNotFoundException(amu.zhcet.data.user.UserNotFoundException) User(amu.zhcet.data.user.User) PasswordValidationException(amu.zhcet.auth.password.PasswordValidationException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with UserNotFoundException

use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.

the class TwoFAService method enable2FA.

/**
 * Takes in the secret and OTP from frontend and enables 2 factor authentication if they are verified
 * @param secret String secret for the user
 * @param code String OTP code
 */
void enable2FA(String secret, String code) {
    User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
    if (!isValidOtp(secret, code)) {
        throw new RuntimeException("Could not verify code, please try again");
    }
    user.setUsing2fa(true);
    user.setTotpSecret(secret);
    userService.save(user);
}
Also used : UserNotFoundException(amu.zhcet.data.user.UserNotFoundException) User(amu.zhcet.data.user.User)

Aggregations

User (amu.zhcet.data.user.User)7 UserNotFoundException (amu.zhcet.data.user.UserNotFoundException)7 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 PasswordChange (amu.zhcet.auth.password.PasswordChange)1 PasswordValidationException (amu.zhcet.auth.password.PasswordValidationException)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1