Search in sources :

Example 1 with UserSecret

use of org.molgenis.security.twofactor.model.UserSecret in project molgenis by molgenis.

the class RecoveryServiceImpl method useRecoveryCode.

@Override
@Transactional
public void useRecoveryCode(String recoveryCode) {
    String userId = getUser().getId();
    RecoveryCode existingCode = runAsSystem(() -> dataService.query(RECOVERY_CODE, RecoveryCode.class).eq(USER_ID, userId).and().eq(CODE, recoveryCode).findOne());
    if (existingCode != null) {
        runAsSystem(() -> dataService.delete(RECOVERY_CODE, existingCode));
        UserSecret secret = runAsSystem(() -> dataService.query(USER_SECRET, UserSecret.class).eq(UserSecretMetaData.USER_ID, userId).findOne());
        secret.setFailedLoginAttempts(0);
        runAsSystem(() -> dataService.update(USER_SECRET, secret));
    } else {
        throw new BadCredentialsException("Invalid recovery code or code already used");
    }
}
Also used : RecoveryCode(org.molgenis.security.twofactor.model.RecoveryCode) UserSecret(org.molgenis.security.twofactor.model.UserSecret) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserSecret

use of org.molgenis.security.twofactor.model.UserSecret in project molgenis by molgenis.

the class TwoFactorAuthenticationServiceImpl method resetSecretForUser.

@Override
public void resetSecretForUser() {
    User user = getUser();
    Stream<UserSecret> userSecrets = runAsSystem(() -> dataService.query(USER_SECRET, UserSecret.class).eq(USER_ID, user.getId()).findAll());
    // noinspection RedundantCast
    runAsSystem((Runnable) () -> dataService.delete(USER_SECRET, userSecrets));
}
Also used : User(org.molgenis.data.security.auth.User) UserSecret(org.molgenis.security.twofactor.model.UserSecret)

Example 3 with UserSecret

use of org.molgenis.security.twofactor.model.UserSecret in project molgenis by molgenis.

the class TwoFactorAuthenticationServiceImpl method isVerificationCodeValidForUser.

@Override
public boolean isVerificationCodeValidForUser(String verificationCode) {
    boolean isValid = false;
    UserSecret userSecret = getSecret();
    if (!userIsBlocked()) {
        try {
            if (otpService.tryVerificationCode(verificationCode, userSecret.getSecret())) {
                isValid = true;
                updateFailedLoginAttempts(0);
            }
        } catch (InvalidVerificationCodeException err) {
            updateFailedLoginAttempts(userSecret.getFailedLoginAttempts() + FAILED_LOGIN_ATTEMPT_ITERATION);
            if (!userIsBlocked()) {
                throw err;
            }
        }
    }
    return isValid;
}
Also used : UserSecret(org.molgenis.security.twofactor.model.UserSecret) InvalidVerificationCodeException(org.molgenis.security.twofactor.exceptions.InvalidVerificationCodeException)

Example 4 with UserSecret

use of org.molgenis.security.twofactor.model.UserSecret in project molgenis by molgenis.

the class TwoFactorAuthenticationServiceImpl method saveSecretForUser.

@Override
public void saveSecretForUser(String secret) {
    if (secret == null) {
        throw new InternalAuthenticationServiceException("No secretKey found");
    } else {
        User user = getUser();
        UserSecret userSecret = userSecretFactory.create();
        userSecret.setUserId(user.getId());
        userSecret.setSecret(secret);
        runAsSystem(() -> dataService.add(USER_SECRET, userSecret));
    }
}
Also used : User(org.molgenis.data.security.auth.User) UserSecret(org.molgenis.security.twofactor.model.UserSecret) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException)

Example 5 with UserSecret

use of org.molgenis.security.twofactor.model.UserSecret in project molgenis by molgenis.

the class TwoFactorAuthenticationServiceImpl method getSecret.

private UserSecret getSecret() {
    User user = getUser();
    UserSecret secret = runAsSystem(() -> dataService.query(USER_SECRET, UserSecret.class).eq(UserSecretMetaData.USER_ID, user.getId()).findOne());
    if (secret != null) {
        return secret;
    } else {
        throw new InternalAuthenticationServiceException(format("Secret not found, user: [{0}] is not configured for two factor authentication", user.getUsername()));
    }
}
Also used : User(org.molgenis.data.security.auth.User) UserSecret(org.molgenis.security.twofactor.model.UserSecret) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException)

Aggregations

UserSecret (org.molgenis.security.twofactor.model.UserSecret)7 User (org.molgenis.data.security.auth.User)4 InternalAuthenticationServiceException (org.springframework.security.authentication.InternalAuthenticationServiceException)2 InvalidVerificationCodeException (org.molgenis.security.twofactor.exceptions.InvalidVerificationCodeException)1 RecoveryCode (org.molgenis.security.twofactor.model.RecoveryCode)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 Transactional (org.springframework.transaction.annotation.Transactional)1