Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #320: Use fake OTP authentication in case user does not exist #321

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"request": {
"method": "POST",
"url": "/user/lookup",
"bodyPatterns": [
{
"matchesJsonPath": "$.identification[?(@.shouldFail == true)]"
}
]
},
"response": {
"status": 404
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,7 @@ public OnboardingServiceImpl(
@Transactional
public OnboardingStartResponse startOnboarding(OnboardingStartRequest request) throws OnboardingProcessException, OnboardingOtpDeliveryException, TooManyProcessesException, InvalidRequestObjectException {
final Map<String, Object> identification = request.getIdentification();

final String identificationData;
try {
identificationData = normalizedMapper.writeValueAsString(identification);
} catch (JsonProcessingException ex) {
logger.warn("Invalid identification data: {}", identification);
throw new InvalidRequestObjectException();
}
final String identificationData = parseIdentificationData(identification);

final OnboardingProcessEntity process = onboardingProcessRepository.findExistingProcessByIdentificationData(identificationData, OnboardingStatus.ACTIVATION_IN_PROGRESS)
.map(it -> resumeExistingProcess(it, identification))
Expand All @@ -148,23 +141,14 @@ public OnboardingStartResponse startOnboarding(OnboardingStartRequest request) t
throw new TooManyProcessesException();
}

// Create an OTP code
String otpCode = otpService.createOtpCode(process, OtpType.ACTIVATION);
// Send the OTP code
try {
final SendOtpCodeRequest sendOtpCodeRequest = SendOtpCodeRequest.builder()
.processId(process.getId())
.userId(userId)
.otpCode(otpCode)
.resend(false)
.locale(LocaleContextHolder.getLocale())
.otpType(SendOtpCodeRequest.OtpType.ACTIVATION)
.build();
onboardingProvider.sendOtpCode(sendOtpCodeRequest);
} catch (OnboardingProviderException e) {
logger.warn("OTP code delivery failed, error: {}", e.getMessage(), e);
throw new OnboardingOtpDeliveryException(e);
final String otpCode = otpService.createOtpCode(process, OtpType.ACTIVATION);
if (userId == null) {
logger.debug("User ID is null, OTP is not sent");
} else {
logger.debug("Sending OTP for user ID: {}", userId);
sendOtp(process, otpCode);
}

OnboardingStartResponse response = new OnboardingStartResponse();
response.setProcessId(process.getId());
response.setOnboardingStatus(process.getStatus());
Expand All @@ -178,27 +162,19 @@ public OnboardingStartResponse startOnboarding(OnboardingStartRequest request) t
* @throws OnboardingProcessException Thrown when OTP resend fails.
*/
@Transactional
public Response resendOtp(OnboardingOtpResendRequest request) throws OnboardingProcessException, OnboardingOtpDeliveryException {
String processId = request.getProcessId();
OnboardingProcessEntity process = findProcess(processId);
String userId = process.getUserId();
// Create an OTP code
String otpCode = otpService.createOtpCodeForResend(process, OtpType.ACTIVATION);
// Resend the OTP code
try {
final SendOtpCodeRequest sendOtpCodeRequest = SendOtpCodeRequest.builder()
.processId(processId)
.userId(userId)
.otpCode(otpCode)
.locale(LocaleContextHolder.getLocale())
.resend(true)
.otpType(SendOtpCodeRequest.OtpType.ACTIVATION)
.build();
onboardingProvider.sendOtpCode(sendOtpCodeRequest);
} catch (OnboardingProviderException e) {
logger.warn("OTP code resend failed, error: {}", e.getMessage(), e);
throw new OnboardingOtpDeliveryException(e);
public Response resendOtp(final OnboardingOtpResendRequest request) throws OnboardingProcessException, OnboardingOtpDeliveryException {
final OnboardingProcessEntity process = findProcess(request.getProcessId());
final String userId = process.getUserId();

final String otpCode = otpService.createOtpCodeForResend(process, OtpType.ACTIVATION);

if (userId == null) {
logger.debug("User ID is not present, OTP is not resent");
} else {
logger.debug("Resending OTP for user ID: {}", userId);
resendOtp(process, otpCode);
}

return new Response();
}

Expand Down Expand Up @@ -417,19 +393,20 @@ public boolean hasProcessExpired(OnboardingProcessEntity onboardingProcess) {
return onboardingProcess.getTimestampCreated().before(createdDateExpirationProcess);
}

private String lookupUser(final String processId, final Map<String, Object> identification) throws OnboardingProcessException {
private String lookupUser(final String processId, final Map<String, Object> identification) {
try {
final LookupUserRequest lookupUserRequest = LookupUserRequest.builder()
.identification(identification)
.processId(processId)
.build();
return onboardingProvider.lookupUser(lookupUserRequest).getUserId();
} catch (OnboardingProviderException e) {
throw new OnboardingProcessException("User lookup failed, error: " + e.getMessage(), e);
logger.info("User lookup failed, using null user ID, error: {}", e.getMessage());
logger.debug("User lookup failed, using null user ID", e);
return null;
}
}

@SneakyThrows(OnboardingProcessException.class)
private OnboardingProcessEntity createNewProcess(final Map<String, Object> identification, final String identificationData) {
final OnboardingProcessEntity process = createNewProcess(identificationData);
logger.debug("Created process ID: {}", process.getId());
Expand Down Expand Up @@ -458,4 +435,47 @@ private OnboardingProcessEntity resumeExistingProcess(final OnboardingProcessEnt
}
return process;
}
}

private String parseIdentificationData(final Map<String, Object> identification) throws InvalidRequestObjectException {
try {
return normalizedMapper.writeValueAsString(identification);
} catch (JsonProcessingException ex) {
logger.warn("Invalid identification data: {}", identification);
throw new InvalidRequestObjectException();
}
}

private void sendOtp(final OnboardingProcessEntity process, final String otpCode) throws OnboardingOtpDeliveryException {
final SendOtpCodeRequest sendOtpCodeRequest = SendOtpCodeRequest.builder()
.processId(process.getId())
.userId(process.getUserId())
.otpCode(otpCode)
.resend(false)
.locale(LocaleContextHolder.getLocale())
.otpType(SendOtpCodeRequest.OtpType.ACTIVATION)
.build();
try {
onboardingProvider.sendOtpCode(sendOtpCodeRequest);
} catch (OnboardingProviderException e) {
logger.warn("OTP code delivery failed, error: {}", e.getMessage(), e);
throw new OnboardingOtpDeliveryException(e);
}
}

private void resendOtp(final OnboardingProcessEntity process, final String otpCode) throws OnboardingOtpDeliveryException {
final SendOtpCodeRequest sendOtpCodeRequest = SendOtpCodeRequest.builder()
.processId(process.getId())
.userId(process.getUserId())
.otpCode(otpCode)
.locale(LocaleContextHolder.getLocale())
.resend(true)
.otpType(SendOtpCodeRequest.OtpType.ACTIVATION)
.build();
try {
onboardingProvider.sendOtpCode(sendOtpCodeRequest);
} catch (OnboardingProviderException e) {
logger.warn("OTP code resend failed, error: {}", e.getMessage(), e);
throw new OnboardingOtpDeliveryException(e);
}
}
}