feat(安全): 内部令牌支持过渡期新旧两把并行接受
换 AIIMAGE_INTERNAL_TOKEN 会让没升级的老客户端立刻全部失败(这把令牌有 4 个客户端字面量 点位并已分发到用户机器)。照仓内 shop-credential-key-previous 的惯例加 internal-token-previous: 逗号分隔的旧值与当前值并入同一接受集合,置空即关窗。 原先 8 处各自持有 @Value 字段并重复实现比较,其中 2 处用的是 equals 而非常量时间比较; 判断收进 AdminAuthSupport.isTrustedSuppliedToken 一处,controller 改为委派。
This commit is contained in:
+3
-45
@@ -44,11 +44,7 @@ public class InvalidAsinDataController {
|
||||
private static final String INVALID_ASIN_DATA_COLUMN_KEY = "admin_invalid_asin_data";
|
||||
private static final String INVALID_ASIN_DATA_ROUTE_PATH = "invalid-asin-data";
|
||||
|
||||
@Value("${aiimage.security.internal-token:}")
|
||||
private String internalToken;
|
||||
|
||||
@Value("${aiimage.security.internal-token-file:}")
|
||||
private String internalTokenFile;
|
||||
|
||||
private final InvalidAsinDataService invalidAsinDataService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
@@ -165,50 +161,12 @@ public class InvalidAsinDataController {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isTrustedInternalRequest(String suppliedToken) {
|
||||
String expectedToken = resolveExpectedInternalToken();
|
||||
return !expectedToken.isBlank() && suppliedToken != null && !suppliedToken.isBlank()
|
||||
&& MessageDigest.isEqual(
|
||||
expectedToken.getBytes(StandardCharsets.UTF_8),
|
||||
suppliedToken.trim().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private String resolveExpectedInternalToken() {
|
||||
if (internalToken != null && !internalToken.isBlank()) {
|
||||
return internalToken.trim();
|
||||
}
|
||||
Path path = resolveInternalTokenFile();
|
||||
if (path == null || !Files.isRegularFile(path)) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
return Files.readString(path, StandardCharsets.UTF_8).trim();
|
||||
} catch (Exception ignored) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private Path resolveInternalTokenFile() {
|
||||
String configuredPath = internalTokenFile == null ? "" : internalTokenFile.trim();
|
||||
if (!configuredPath.isEmpty()) {
|
||||
if (configuredPath.equals("~") || configuredPath.startsWith("~/") || configuredPath.startsWith("~\\")) {
|
||||
String userHome = System.getProperty("user.home", "").trim();
|
||||
if (userHome.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
configuredPath = configuredPath.length() == 1
|
||||
? userHome
|
||||
: Path.of(userHome, configuredPath.substring(2)).toString();
|
||||
}
|
||||
Path configuredTokenPath = Path.of(configuredPath);
|
||||
return configuredTokenPath.isAbsolute() ? configuredTokenPath.normalize() : null;
|
||||
}
|
||||
String userHome = System.getProperty("user.home", "").trim();
|
||||
return userHome.isEmpty()
|
||||
? null
|
||||
: Path.of(userHome, ".aiimage", "internal-token").toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
private record RequestOperator(Long id, boolean superAdmin) {
|
||||
}
|
||||
private boolean isTrustedInternalRequest(String suppliedToken) {
|
||||
return adminAuthSupport.isTrustedSuppliedToken(suppliedToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user