Browse Source

я придумал хуйню

master
gsd 2 years ago
parent
commit
9d6d291e44
  1. 51
      src/main/java/app/utils/SaltedCookie.java
  2. 1
      src/main/resources/application.yaml

51
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<String, String> 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;
}
}

1
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:

Loading…
Cancel
Save