Skip to content

Commit

Permalink
Merge pull request #1640 from /issues/1639-extract-duplicity
Browse files Browse the repository at this point in the history
Fix #1639: Extract duplicity in AuthenticationService
  • Loading branch information
banterCZ authored May 9, 2024
2 parents 8dcff20 + fc9a946 commit c77c1cd
Showing 1 changed file with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.getlime.security.powerauth.lib.nextstep.model.exception.*;
import io.getlime.security.powerauth.lib.nextstep.model.request.*;
import io.getlime.security.powerauth.lib.nextstep.model.response.*;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -421,19 +422,12 @@ public OtpAuthenticationResponse authenticateWithOtp(OtpAuthenticationRequest re
}
}

final Integer remainingAttempts = resolveRemainingAttempts(credential, otp, operation);
if (Integer.valueOf(0).equals(remainingAttempts) && otp.getStatus() == OtpStatus.ACTIVE) {
logger.debug("OTP was blocked because there are no remaining attempts left, OTP ID: {}", otp.getOtpId());
otp.setStatus(OtpStatus.BLOCKED);
otp.setTimestampBlocked(new Date());
// OTP was updated, save authentication with OTP
authenticationRepository.save(authentication);
}
final Integer remainingAttempts = resolveRemainingAttemptsAndBlockOtpIfNeeded(credential, otp, operation, authentication);

final String mode = checkOnly ? "check" : "authentication";

logger.info("OTP " + mode + " result: {}, remaining attempts: {}, user ID: {}, user identity status: {}, OTP status: {}, credential status: {}, operation failed: {}",
authenticationResult, remainingAttempts, user == null ? null : user.getUserId(), user == null ? null : user.getStatus(), otp.getStatus(), credential == null ? null : credential.getStatus(), operationFailed);
logger.info("OTP {} result: {}, remaining attempts: {}, user ID: {}, user identity status: {}, OTP status: {}, credential status: {}, operation failed: {}",
mode, authenticationResult, remainingAttempts, user == null ? null : user.getUserId(), user == null ? null : user.getStatus(), otp.getStatus(), credential == null ? null : credential.getStatus(), operationFailed);
audit.info("OTP authentication result", AuditDetail.builder()
.type(AUDIT_TYPE_AUTHENTICATION)
.param("userId", user != null ? user.getUserId() : null)
Expand Down Expand Up @@ -669,14 +663,7 @@ public CombinedAuthenticationResponse authenticateCombined(CombinedAuthenticatio
}
}

final Integer remainingAttempts = resolveRemainingAttempts(credential, otp, operation);
if (Integer.valueOf(0).equals(remainingAttempts) && otp.getStatus() == OtpStatus.ACTIVE) {
logger.debug("OTP was blocked because there are no remaining attempts left, OTP ID: {}", otp.getOtpId());
otp.setStatus(OtpStatus.BLOCKED);
otp.setTimestampBlocked(new Date());
// OTP was updated, save authentication with OTP
authenticationRepository.save(authentication);
}
final Integer remainingAttempts = resolveRemainingAttemptsAndBlockOtpIfNeeded(credential, otp, operation, authentication);

logger.info("Combined authentication result: {}, credential authentication result: {}, OTP authentication result: {}, remaining attempts: {}, user ID: {}, user identity status: {}, OTP status: {}, credential status: {}, operation failed: {}",
authenticationResult, credentialAuthenticationResult, otpAuthenticationResult, remainingAttempts, user.getUserId(), user.getStatus(), otp.getStatus(), credential.getStatus(), operationFailed);
Expand Down Expand Up @@ -981,6 +968,18 @@ private UpdateOperationResponse updateOperation(String userId, OperationEntity o
}
}

private Integer resolveRemainingAttemptsAndBlockOtpIfNeeded(final CredentialEntity credential, final @NotNull OtpEntity otp, final OperationEntity operation, final AuthenticationEntity authentication) {
final Integer remainingAttempts = resolveRemainingAttempts(credential, otp, operation);
if (Integer.valueOf(0).equals(remainingAttempts) && otp.getStatus() == OtpStatus.ACTIVE) {
logger.debug("OTP was blocked because there are no remaining attempts left, OTP ID: {}", otp.getOtpId());
otp.setStatus(OtpStatus.BLOCKED);
otp.setTimestampBlocked(new Date());
// OTP was updated, save authentication with OTP
authenticationRepository.save(authentication);
}
return remainingAttempts;
}

/**
* Resolve remaining attempts for a credential entity, OTP entity and operation.
* @param credential Credential entity or null for no credential.
Expand Down

0 comments on commit c77c1cd

Please sign in to comment.