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 #558: SMS does not respect accept-language #559

Merged
merged 1 commit into from
Dec 8, 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
1 change: 1 addition & 0 deletions docs/onboarding/Database-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Stores onboarding processes created for tracking status of user onboarding.
|---|---|---|---|
| `id` | `VARCHAR(36)` | `NOT NULL PRIMARY KEY` | Autogenerated record identifier (UUID). |
| `identification_data` | `VARCHAR(1024)` | `NOT NULL` | Data sent by the customer as JSON. |
| `custom_data` | `VARCHAR(1024)` | `NOT NULL` | Custom data as JSON. |
| `user_id` | `VARCHAR(256)` | | Resolved user identifier. |
| `activation_id` | `VARCHAR(36)` | | Identifier of created activation. |
| `status` | `VARCHAR(32)` | `NOT NULL` | Status of onboarding process (`ACTIVATION_IN_PROGRESS`, `VERIFICATION_IN_PROGRESS`, `FINISHED`, `FAILED`). |
Expand Down
1 change: 1 addition & 0 deletions docs/sql/mysql/onboarding/create-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CREATE TABLE es_onboarding_process (
error_detail VARCHAR(256),
error_origin VARCHAR(256),
error_score INTEGER NOT NULL DEFAULT 0,
custom_data VARCHAR(1024) NOT NULL,
timestamp_created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
timestamp_last_updated DATETIME,
timestamp_finished DATETIME,
Expand Down
1 change: 1 addition & 0 deletions docs/sql/oracle/onboarding/create-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CREATE TABLE ES_ONBOARDING_PROCESS (
ERROR_DETAIL VARCHAR2(256 CHAR),
ERROR_ORIGIN VARCHAR2(256 CHAR),
ERROR_SCORE INTEGER DEFAULT 0 NOT NULL,
CUSTOM_DATA VARCHAR2(1024 CHAR) NOT NULL,
TIMESTAMP_CREATED TIMESTAMP(6) NOT NULL,
TIMESTAMP_LAST_UPDATED TIMESTAMP(6),
TIMESTAMP_FINISHED TIMESTAMP(6),
Expand Down
1 change: 1 addition & 0 deletions docs/sql/postgresql/onboarding/create-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ CREATE TABLE es_onboarding_process (
error_detail VARCHAR(256),
error_origin VARCHAR(256),
error_score INTEGER NOT NULL DEFAULT 0,
custom_data VARCHAR(1024) NOT NULL,
timestamp_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
timestamp_last_updated TIMESTAMP,
timestamp_finished TIMESTAMP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public class OnboardingProcessEntity implements Serializable {
public static final String ERROR_MAX_PROCESS_ERROR_SCORE_EXCEEDED = "maxProcessErrorScoreExceeded";
public static final String ERROR_USER_LOOKUP = "userLookupFailed";

/**
* Key for {@link #customData} storing locale.
*/
public static final String CUSTOM_DATA_LOCALE = "locale";

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
Expand All @@ -66,6 +71,12 @@ public class OnboardingProcessEntity implements Serializable {
@Column(name = "identification_data", nullable = false)
private String identificationData;

/**
* Json with custom data such as preferred locale.
*/
@Column(name = "custom_data", nullable = false)
private String customData = "{}";

@Column(name = "user_id")
private String userId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.wultra.app.onboardingserver.impl.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wultra.app.enrollmentserver.api.model.onboarding.response.OtpVerifyResponse;
import com.wultra.app.enrollmentserver.model.enumeration.*;
import com.wultra.app.enrollmentserver.model.integration.OwnerId;
Expand All @@ -37,11 +39,12 @@
import com.wultra.app.onboardingserver.provider.model.request.SendOtpCodeRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Locale;
import java.util.Map;

import static com.wultra.app.enrollmentserver.model.enumeration.IdentityVerificationPhase.OTP_VERIFICATION;
import static com.wultra.app.enrollmentserver.model.enumeration.IdentityVerificationPhase.PRESENCE_CHECK;
Expand Down Expand Up @@ -73,6 +76,8 @@ public class IdentityVerificationOtpService {

private final AuditService auditService;

private final ObjectMapper mapper = new ObjectMapper();
banterCZ marked this conversation as resolved.
Show resolved Hide resolved

/**
* Service constructor.
*
Expand Down Expand Up @@ -263,7 +268,7 @@ private void sendOtpCode(String processId, boolean isResend) throws OnboardingPr
.userId(userId)
.otpCode(otpCode)
.resend(isResend)
.locale(LocaleContextHolder.getLocale())
.locale(getLocale(process))
.otpType(SendOtpCodeRequest.OtpType.USER_VERIFICATION)
.build();
try {
Expand All @@ -276,6 +281,18 @@ private void sendOtpCode(String processId, boolean isResend) throws OnboardingPr
auditService.auditOnboardingProvider(process, "{} user verification OTP for user: {}", resentPrefix, userId);
}

@SuppressWarnings("unchecked") // unchecked readValue
private Locale getLocale(final OnboardingProcessEntity process) throws OnboardingProcessException {
try {
logger.debug("Getting locale from custom_data: {} of process ID: {}", process.getCustomData(), process.getId());
final Map<String, Object> json = mapper.readValue(process.getCustomData(), Map.class);
final String language = json.get(OnboardingProcessEntity.CUSTOM_DATA_LOCALE).toString();
return new Locale(language);
} catch (JsonProcessingException e) {
throw new OnboardingProcessException("Problem to parse custom_data of process ID: " + process.getId(), e);
}
}

private String createOtpCode(final boolean isResend, final OnboardingProcessEntity process) throws OnboardingOtpDeliveryException, OnboardingProcessException {
if (isResend) {
return otpService.createOtpCodeForResend(process, OtpType.USER_VERIFICATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ private String lookupUser(final OnboardingProcessEntity process, final Map<Strin
}
}

@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 All @@ -422,18 +423,20 @@ private OnboardingProcessEntity createNewProcess(final Map<String, Object> ident
return process;
}

private OnboardingProcessEntity createNewProcess(final String identificationData) {
private OnboardingProcessEntity createNewProcess(final String identificationData) throws OnboardingProcessException {
final OnboardingProcessEntity process = new OnboardingProcessEntity();
process.setIdentificationData(identificationData);
process.setStatus(OnboardingStatus.ACTIVATION_IN_PROGRESS);
process.setTimestampCreated(new Date());
setLocaleToCustomData(process);
return onboardingProcessRepository.save(process);
}

@SneakyThrows(OnboardingProcessException.class)
private OnboardingProcessEntity resumeExistingProcess(final OnboardingProcessEntity process, final Map<String, Object> identification) {
logger.debug("Resuming process ID: {}", process.getId());
process.setTimestampLastUpdated(new Date());
setLocaleToCustomData(process);
final String userId = lookupUser(process, identification);
if (!process.getUserId().equals(userId)) {
throw new OnboardingProcessException(
Expand All @@ -444,6 +447,18 @@ private OnboardingProcessEntity resumeExistingProcess(final OnboardingProcessEnt
return process;
}

@SuppressWarnings("unchecked") // unchecked readValue
private void setLocaleToCustomData(final OnboardingProcessEntity process) throws OnboardingProcessException {
try {
logger.debug("Setting locale to custom_data: {} of process ID: {}", process.getCustomData(), process.getId());
final Map<String, Object> json = normalizedMapper.readValue(process.getCustomData(), Map.class);
json.put(OnboardingProcessEntity.CUSTOM_DATA_LOCALE, LocaleContextHolder.getLocale().getLanguage());
process.setCustomData(normalizedMapper.writeValueAsString(json));
} catch (JsonProcessingException e) {
throw new OnboardingProcessException("Problem to parse custom_data of process ID: " + process.getId(), e);
}
}

private void removeActivation(final OnboardingProcessEntity process) throws OnboardingProcessException {
final String activationId = process.getActivationId();
if (activationId != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INSERT INTO es_onboarding_process(id, identification_data, status, activation_id, activation_removed, error_score, timestamp_created) VALUES
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 'a1', false, 0, now()),
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'FAILED', 'a2', false, 0, now()),
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'FAILED', 'a3', true, 0, now());
INSERT INTO es_onboarding_process(id, identification_data, status, activation_id, activation_removed, error_score, custom_data, timestamp_created) VALUES
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 'a1', false, 0, '{}', now()),
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'FAILED', 'a2', false, 0, '{}', now()),
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'FAILED', 'a3', true, 0, '{}', now());
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
INSERT INTO es_onboarding_process(id, identification_data, status, error_score, timestamp_created) VALUES
('b4662611-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, now());
INSERT INTO es_onboarding_process(id, identification_data, status, error_score, custom_data, timestamp_created) VALUES
('b4662611-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, '{}', now());

INSERT INTO es_onboarding_otp(id, process_id, otp_code, failed_attempts, status, type, timestamp_created, timestamp_expiration) VALUES
('f50b8c04-649d-43a7-8079-4dbf9b0bbc72', 'b4662611-df91-4053-bb3d-3970979baf5d', '123', 0, 'ACTIVE', 'USER_VERIFICATION', now(), now()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
INSERT INTO es_onboarding_process(id, identification_data, status, error_score, timestamp_created) VALUES
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, now() - interval '301' second),
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, now() - interval '301' second),
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, now() - interval '301' second),
('44444444-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, now()); -- to be kept
INSERT INTO es_onboarding_process(id, identification_data, status, error_score, custom_data, timestamp_created) VALUES
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, '{}', now() - interval '301' second),
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, '{}', now() - interval '301' second),
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, '{}', now() - interval '301' second),
('44444444-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, '{}', now()); -- to be kept

INSERT INTO es_identity_verification(id, activation_id, user_id, process_id, status, phase, timestamp_created, timestamp_last_updated) VALUES
('11111111-4ac0-45dd-b68e-29f4cd991a5c', 'a1', 'u1', '11111111-df91-4053-bb3d-3970979baf5d', 'IN_PROGRESS', 'PRESENCE_CHECK', now(), now()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
INSERT INTO es_onboarding_process(id, identification_data, status, error_score, timestamp_created) VALUES
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, now() - interval '10801' second),
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, now() - interval '10801' second),
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, now() - interval '10801' second),
('44444444-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, now()); -- to be kept
INSERT INTO es_onboarding_process(id, identification_data, status, error_score, custom_data, timestamp_created) VALUES
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, '{}', now() - interval '10801' second),
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, '{}', now() - interval '10801' second),
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, '{}', now() - interval '10801' second),
('44444444-df91-4053-bb3d-3970979baf5d', '{}', 'VERIFICATION_IN_PROGRESS', 0, '{}', now()); -- to be kept

INSERT INTO es_identity_verification(id, activation_id, user_id, process_id, status, phase, timestamp_created, timestamp_last_updated) VALUES
('11111111-4ac0-45dd-b68e-29f4cd991a5c', 'a1', 'u1', '11111111-df91-4053-bb3d-3970979baf5d', 'IN_PROGRESS', 'PRESENCE_CHECK', now(), now()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INSERT INTO es_onboarding_process(id, identification_data, status, error_score, timestamp_created) VALUES
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, now() - interval '10801' second),
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, now()),
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'FINISHED', 0, now() - interval '301' second);
INSERT INTO es_onboarding_process(id, identification_data, status, error_score, custom_data, timestamp_created) VALUES
('11111111-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, '{}', now() - interval '10801' second),
('22222222-df91-4053-bb3d-3970979baf5d', '{}', 'ACTIVATION_IN_PROGRESS', 0, '{}', now()),
('33333333-df91-4053-bb3d-3970979baf5d', '{}', 'FINISHED', 0, '{}', now() - interval '301' second);