ru.foodtechlab.lib.auth.app.config.DefaultPreferenceGenerator Maven / Gradle / Ivy
The newest version!
package ru.foodtechlab.lib.auth.app.config;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import ru.foodtechlab.lib.auth.integration.core.preference.ServicePreferenceFacade;
import ru.foodtechlab.lib.auth.service.facade.confirmationCode.dto.responses.ConfirmationCodeResponse;
import ru.foodtechlab.lib.auth.service.facade.preference.dto.requests.UpdateServicePreferenceRequest;
import ru.foodtechlab.lib.auth.service.facade.preference.dto.responses.*;
@Component("defaultPreferenceGenerator")
@RequiredArgsConstructor
public class DefaultPreferenceGenerator {
private final ServicePreferenceFacade servicePreferenceFacade;
@Value("${foodtechlab.preferences.need-rewrite:#{false}}")
private boolean needRewrite;
@Value("${foodtechlab.preferences.default-role-code:USER}")
private String defaultRoleCode;
@Value("${foodtechlab.preferences.auth-session-with-confirmation-ttl.one-time-confirmation-code-ttl:#{100L}}")
private long authSessionOneTimeConfirmationTTL;
@Value("${foodtechlab.preferences.auth-session-with-confirmation-ttl.personal-confirmation-code-ttl:#{100L}}")
private long authSessionPersonalConfirmationTTL;
@Value("${foodtechlab.preferences.auth-session-with-confirmation-ttl.general-confirmation-code-ttl:#{100L}}")
private long authSessionGeneralConfirmationTTL;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.one-time.phone-number-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitOneTimeCodeByPhoneNumber;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.one-time.email-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitOneTimeCodeByEmail;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.one-time.username-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitOneTimeCodeByUsername;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.personal.phone-number-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitPersonalCodeByPhoneNumber;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.personal.email-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitPersonalCodeByEmail;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.personal.username-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitPersonalCodeByUsername;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.general.phone-number-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitGeneralCodeByPhoneNumber;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.general.email-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitGeneralCodeByEmail;
@Value("${foodtechlab.preferences.confirmation-code-day-attempt-limit.general.username-limit:#{100L}}")
private long confirmationCodeDayAttemptLimitGeneralCodeByUsername;
@Value("${foodtechlab.preferences.default-confirmation-code-type:ONE_TIME}")
private String defaultConfirmationCodeType;
@Value("${foodtechlab.preferences.default-iso-two-letter-country-code:ru}")
private String defaultIsoTwoLetterCountryCode;
@Value("${foodtechlab.preferences.general-confirmation-code:4321}")
private String generalConfirmationCode;
@Value("${foodtechlab.preferences.default-personal-confirmation-code:1234}")
private String defaultPersonalConfirmationCode;
@Value("${foodtechlab.preferences.device-id-day-attempt-limit.single-factor-with-confirmation.phone-number-limit:#{500L}}")
private long deviceIdDayAttemptLimitSingleFactorWithConfirmationByPhoneNumber;
@Value("${foodtechlab.preferences.device-id-day-attempt-limit.single-factor-with-confirmation.email-limit:#{500L}}")
private long deviceIdDayAttemptLimitSingleFactorWithConfirmationByEmail;
@Value("${foodtechlab.preferences.device-id-day-attempt-limit.single-factor-with-confirmation.username-limit:#{500L}}")
private long deviceIdDayAttemptLimitSingleFactorWithConfirmationByUsername;
@Value("${foodtechlab.preferences.device-id-day-attempt-limit.single-factor-with-password.phone-number-limit:#{500L}}")
private long deviceIdDayAttemptLimitSingleFactorWithPasswordByPhoneNumber;
@Value("${foodtechlab.preferences.device-id-day-attempt-limit.single-factor-with-password.email-limit:#{500L}}")
private long deviceIdDayAttemptLimitSingleFactorWithPasswordByEmail;
@Value("${foodtechlab.preferences.device-id-day-attempt-limit.single-factor-with-password.username-limit:#{500L}}")
private long deviceIdDayAttemptLimitSingleFactorWithPasswordByUsername;
@Value("${foodtechlab.preferences.ip-v4-hour-attempt-limit.single-factor-with-confirmation.phone-number-limit:#{500L}}")
private long ipV4HourAttemptLimitSingleFactorWithConfirmationByPhoneNumber;
@Value("${foodtechlab.preferences.ip-v4-hour-attempt-limit.single-factor-with-confirmation.email-limit:#{500L}}")
private long ipV4HourAttemptLimitSingleFactorWithConfirmationByEmail;
@Value("${foodtechlab.preferences.ip-v4-hour-attempt-limit.single-factor-with-confirmation.username-limit:#{500L}}")
private long ipV4HourAttemptLimitSingleFactorWithConfirmationByUsername;
@Value("${foodtechlab.preferences.ip-v4-hour-attempt-limit.single-factor-with-password.phone-number-limit:#{500L}}")
private long ipV4HourAttemptLimitSingleFactorWithPasswordByPhoneNumber;
@Value("${foodtechlab.preferences.ip-v4-hour-attempt-limit.single-factor-with-password.email-limit:#{500L}}")
private long ipV4HourAttemptLimitSingleFactorWithPasswordByEmail;
@Value("${foodtechlab.preferences.ip-v4-hour-attempt-limit.single-factor-with-password.username-limit:#{500L}}")
private long ipV4HourAttemptLimitSingleFactorWithPasswordByUsername;
@Value("${foodtechlab.preferences.auth-session-login-confirm-attempt-limit.phone-number-limit:#{10L}}")
private long authSessionLoginConfirmAttemptLimitByPhoneNumber;
@Value("${foodtechlab.preferences.auth-session-login-confirm-attempt-limit.email-limit:#{10L}}")
private long authSessionLoginConfirmAttemptLimitByEmail;
@Value("${foodtechlab.preferences.auth-session-login-confirm-attempt-limit.username-limit:#{10L}}")
private long authSessionLoginConfirmAttemptLimitByUsername;
@Value("${foodtechlab.preferences.default-confirmation-code-message-webhook-provider.sms.url:}")
private String defaultConfirmationCodeMessageWebhookSmsUrl;
@Value("${foodtechlab.preferences.default-confirmation-code-message-webhook-provider.sms.attempt-limit:#{1L}}")
private long defaultConfirmationCodeMessageWebhookSmsAttemptLimit;
@Value("${foodtechlab.preferences.default-confirmation-code-message-webhook-provider.sms.attempt-timeout:#{10L}}")
private long defaultConfirmationCodeMessageWebhookSmsAttemptTimeout;
@Value("${foodtechlab.preferences.default-confirmation-code-message-webhook-provider.email.url:}")
private String defaultConfirmationCodeMessageWebhookEmailUrl;
@Value("${foodtechlab.preferences.default-confirmation-code-message-webhook-provider.email.attempt-limit:#{1L}}")
private long defaultConfirmationCodeMessageWebhookEmailAttemptLimit;
@Value("${foodtechlab.preferences.default-confirmation-code-message-webhook-provider.email.attempt-timeout:#{10L}}")
private long defaultConfirmationCodeMessageWebhookEmailAttemptTimeout;
@Value("${foodtechlab.preferences.password-validation-hour-attempt-limit.phone-number-limit:#{100L}}")
private long passwordValidationHourAttemptLimitByPhoneNumber;
@Value("${foodtechlab.preferences.password-validation-hour-attempt-limit.email-limit:#{100L}}")
private long passwordValidationHourAttemptLimitByEmail;
@Value("${foodtechlab.preferences.password-validation-hour-attempt-limit.username-limit:#{100L}}")
private long passwordValidationHourAttemptLimitByUsername;
public void generate() {
// если было создано, но при этом пустые данные
if (needRewrite) {
UpdateServicePreferenceRequest request = UpdateServicePreferenceRequest
.builder()
.defaultRoleCode(defaultRoleCode)
.authSessionWithConfirmationTTLs(new AuthSessionWithConfirmationTTLs(
authSessionOneTimeConfirmationTTL,
authSessionPersonalConfirmationTTL,
authSessionGeneralConfirmationTTL))
.confirmationCodeDayAttemptLimits(new ConfirmationCodeDayAttemptLimit(
new AttemptLimitsByLoginType(
confirmationCodeDayAttemptLimitOneTimeCodeByPhoneNumber,
confirmationCodeDayAttemptLimitOneTimeCodeByEmail,
confirmationCodeDayAttemptLimitOneTimeCodeByUsername),
new AttemptLimitsByLoginType(confirmationCodeDayAttemptLimitPersonalCodeByPhoneNumber,
confirmationCodeDayAttemptLimitPersonalCodeByEmail,
confirmationCodeDayAttemptLimitPersonalCodeByUsername),
new AttemptLimitsByLoginType(confirmationCodeDayAttemptLimitGeneralCodeByPhoneNumber,
confirmationCodeDayAttemptLimitGeneralCodeByEmail,
confirmationCodeDayAttemptLimitGeneralCodeByUsername)))
.defaultConfirmationCodeType(ConfirmationCodeResponse.Type.valueOf(defaultConfirmationCodeType))
.defaultIsoTwoLetterCountryCode(defaultIsoTwoLetterCountryCode)
.generalConfirmationCode(generalConfirmationCode)
.defaultPersonalConfirmationCode(defaultPersonalConfirmationCode)
.deviceIdDayAttemptLimits(new AuthSessionTypeAttemptLimits(
new AttemptLimitsByLoginType(deviceIdDayAttemptLimitSingleFactorWithConfirmationByPhoneNumber,
deviceIdDayAttemptLimitSingleFactorWithConfirmationByEmail,
deviceIdDayAttemptLimitSingleFactorWithConfirmationByUsername),
new AttemptLimitsByLoginType(deviceIdDayAttemptLimitSingleFactorWithPasswordByPhoneNumber,
deviceIdDayAttemptLimitSingleFactorWithPasswordByEmail,
deviceIdDayAttemptLimitSingleFactorWithPasswordByUsername)
))
.ipV4HourAttemptLimits(new AuthSessionTypeAttemptLimits(
new AttemptLimitsByLoginType(ipV4HourAttemptLimitSingleFactorWithConfirmationByPhoneNumber,
ipV4HourAttemptLimitSingleFactorWithConfirmationByEmail,
ipV4HourAttemptLimitSingleFactorWithConfirmationByUsername),
new AttemptLimitsByLoginType(ipV4HourAttemptLimitSingleFactorWithPasswordByPhoneNumber,
ipV4HourAttemptLimitSingleFactorWithPasswordByEmail,
ipV4HourAttemptLimitSingleFactorWithPasswordByUsername)
))
.authSessionLoginConfirmAttemptsLimits(new AttemptLimitsByLoginType(
authSessionLoginConfirmAttemptLimitByPhoneNumber,
authSessionLoginConfirmAttemptLimitByEmail,
authSessionLoginConfirmAttemptLimitByUsername
))
.defaultConfirmationCodeMessageWebhookProviders(new ConfirmationCodeMessageWebhookProviders(
new ConfirmationCodeMessageWebhookProviders.Provider(
defaultConfirmationCodeMessageWebhookSmsUrl,
defaultConfirmationCodeMessageWebhookSmsAttemptLimit,
defaultConfirmationCodeMessageWebhookSmsAttemptTimeout
),
new ConfirmationCodeMessageWebhookProviders.Provider(
defaultConfirmationCodeMessageWebhookEmailUrl,
defaultConfirmationCodeMessageWebhookEmailAttemptLimit,
defaultConfirmationCodeMessageWebhookEmailAttemptTimeout
)
))
.defaultConfirmationCodeMessageBodies(new DefaultConfirmationCodeMessageBodies())
.passwordValidationHourAttemptLimits(new AttemptLimitsByLoginType(
passwordValidationHourAttemptLimitByPhoneNumber,
passwordValidationHourAttemptLimitByEmail,
passwordValidationHourAttemptLimitByUsername
))
.build();
servicePreferenceFacade.update(request);
} else{
// если было создано, но при этом пустые данные
ServicePreferenceResponse preferenceResponse = servicePreferenceFacade.get();
boolean defConfCheck = preferenceResponse.getDefaultConfirmationCodeMessageWebhookProviders() == null;
boolean defConfCheckSms = preferenceResponse.getDefaultConfirmationCodeMessageWebhookProviders() == null ||
preferenceResponse.getDefaultConfirmationCodeMessageWebhookProviders().getSmsWebhookProvider() == null;
if(defConfCheck || defConfCheckSms){
UpdateServicePreferenceRequest request = UpdateServicePreferenceRequest
.builder()
.defaultRoleCode(preferenceResponse.getDefaultRoleCode())
.authSessionWithConfirmationTTLs(preferenceResponse.getAuthSessionWithConfirmationTTLs())
.confirmationCodeDayAttemptLimits(preferenceResponse.getConfirmationCodeDayAttemptLimits())
.defaultConfirmationCodeType(preferenceResponse.getDefaultConfirmationCodeType())
.defaultIsoTwoLetterCountryCode(preferenceResponse.getDefaultIsoTwoLetterCountryCode())
.generalConfirmationCode(preferenceResponse.getGeneralConfirmationCode())
.defaultPersonalConfirmationCode(preferenceResponse.getDefaultPersonalConfirmationCode())
.deviceIdDayAttemptLimits(preferenceResponse.getDeviceIdDayAttemptLimits())
.ipV4HourAttemptLimits(preferenceResponse.getIpV4HourAttemptLimits())
.authSessionLoginConfirmAttemptsLimits(preferenceResponse.getAuthSessionLoginConfirmAttemptsLimits())
.defaultConfirmationCodeMessageWebhookProviders(preferenceResponse.getDefaultConfirmationCodeMessageWebhookProviders())
.defaultConfirmationCodeMessageBodies(preferenceResponse.getDefaultConfirmationCodeMessageBodies())
.passwordValidationHourAttemptLimits(preferenceResponse.getPasswordValidationHourAttemptLimits())
.build();
if(defConfCheck) {
request.setDefaultConfirmationCodeMessageWebhookProviders(new ConfirmationCodeMessageWebhookProviders(
new ConfirmationCodeMessageWebhookProviders.Provider(
defaultConfirmationCodeMessageWebhookSmsUrl,
defaultConfirmationCodeMessageWebhookSmsAttemptLimit,
defaultConfirmationCodeMessageWebhookSmsAttemptTimeout
),
new ConfirmationCodeMessageWebhookProviders.Provider(
defaultConfirmationCodeMessageWebhookEmailUrl,
defaultConfirmationCodeMessageWebhookEmailAttemptLimit,
defaultConfirmationCodeMessageWebhookEmailAttemptTimeout
)
));
}
if(defConfCheckSms){
request.getDefaultConfirmationCodeMessageWebhookProviders().setSmsWebhookProvider(
new ConfirmationCodeMessageWebhookProviders.Provider(
defaultConfirmationCodeMessageWebhookSmsUrl,
defaultConfirmationCodeMessageWebhookSmsAttemptLimit,
defaultConfirmationCodeMessageWebhookSmsAttemptTimeout
)
);
}
servicePreferenceFacade.update(request);
}
}
}
}