From 9d6d291e44104ee4040bcab59f27cd2158bdc8bd Mon Sep 17 00:00:00 2001 From: gsd Date: Tue, 14 Mar 2023 17:07:26 +0300 Subject: [PATCH] =?UTF-8?q?=D1=8F=20=D0=BF=D1=80=D0=B8=D0=B4=D1=83=D0=BC?= =?UTF-8?q?=D0=B0=D0=BB=20=D1=85=D1=83=D0=B9=D0=BD=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/utils/SaltedCookie.java | 51 +++++++++++++++++++++-- src/main/resources/application.yaml | 1 - 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/main/java/app/utils/SaltedCookie.java b/src/main/java/app/utils/SaltedCookie.java index 36a9bd0..907c55c 100644 --- a/src/main/java/app/utils/SaltedCookie.java +++ b/src/main/java/app/utils/SaltedCookie.java @@ -1,21 +1,61 @@ package app.utils; +import app.updates.CountriesUpdater; +import jakarta.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.DigestUtils; +import java.util.Arrays; +import java.util.HashMap; + @Component public class SaltedCookie { @Value("${backend.auth.salt}") private String salt; - @Value("${backend.secret_key}") - private String secret_key; + private HashMap secret_keys = new HashMap<>(); + + private final Logger logger = LoggerFactory.getLogger(SaltedCookie.class); public String Hashed(String value) { return DigestUtils.md5DigestAsHex(String.format("%s+%s", value, salt).getBytes()); } + @PostConstruct + public void fillSecretKeys() { + //1. Когда указан только один глобальный + //grgd9743gsdgdrgdrg7gvcb7gdg790rdgg7drg + //2. Когда указан один именной + //global=43534543543534543543534 + //3. Когда указано несколько + //global=gdrg43g4gdgd;discord=gedg43g43g34g34g + String secret_keys_from_env; + try { + secret_keys_from_env = System.getenv("SECRET_KEY"); + } catch (NullPointerException err) { + logger.error("SECRET KEY NOT SETTED IN ENV\nDISABLE SECRET KEY AUTH"); + secret_keys = null; + return; + } + + String[] secret_keys_splitted = secret_keys_from_env.split(";"); + String[] secret_key_map; + // ДА Я МОГ ТУТ СТРИМЫ ИСПОЛЬЗОВАТЬ, НО МНЕ ЛЕНЬ С ЯДЕРНЫМИ ЧИСЛАМИ РАБОТАТЬ, ПОТОМ + for (int i = 0; i < secret_keys_splitted.length; i++) { + if (secret_keys_splitted[i].contains("=")) { + secret_key_map = secret_keys_splitted[i].split("="); + logger.info("Put into secret_keys: {}", secret_key_map[0]); + secret_keys.put(secret_key_map[1], secret_key_map[2]); + } else { + logger.info("Set global secret key"); + secret_keys.put("global", secret_keys_splitted[i]); + } + } + } + public boolean Validate(String value, String hashed_value) { return Hashed(value).equals(hashed_value); } @@ -25,6 +65,11 @@ public class SaltedCookie { } public boolean ValidateSecretKey(String secret_key) { - return this.secret_key.equals(secret_key); + if (secret_keys == null || secret_keys.isEmpty()) return false; + if (secret_keys.containsKey(secret_key)) { + logger.info("Use secret key: {}", secret_keys.get(secret_key)); + return true; + } + return false; } } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 8a2211a..83719ae 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -24,7 +24,6 @@ org: enabled: true backend: - secret_key: ${SECRET_KEY} servers_file: ${SERVERS_FILE} geoip_file: ${GEOIP_FILE} updates: