Fix auth tests

This commit is contained in:
2026-01-12 17:40:07 +01:00
parent 8a76ab2fb9
commit 68e3a29268
4 changed files with 35 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package dev.kruhlmann.imgfloat.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
@@ -14,9 +15,14 @@ public class OAuth2AuthorizedClientPersistenceConfig {
@Bean
OAuth2AuthorizedClientService oauth2AuthorizedClientService(
JdbcOperations jdbcOperations,
ClientRegistrationRepository clientRegistrationRepository
ClientRegistrationRepository clientRegistrationRepository,
Environment environment
) {
return new SQLiteOAuth2AuthorizedClientService(jdbcOperations, clientRegistrationRepository);
return new SQLiteOAuth2AuthorizedClientService(
jdbcOperations,
clientRegistrationRepository,
OAuthTokenCipher.fromEnvironment(environment)
);
}
@Bean

View File

@@ -12,6 +12,7 @@ import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
public class OAuthTokenCipher {
@@ -52,6 +53,28 @@ public class OAuthTokenCipher {
return new OAuthTokenCipher(primaryKey, keys);
}
public static OAuthTokenCipher fromEnvironment(Environment environment) {
String base64Key = environment.getProperty(KEY_ENV);
if (base64Key == null || base64Key.isBlank()) {
throw new IllegalStateException(KEY_ENV + " is required to encrypt OAuth tokens");
}
SecretKey primaryKey = decodeKey(base64Key, KEY_ENV);
List<SecretKey> keys = new ArrayList<>();
keys.add(primaryKey);
String previousKeys = environment.getProperty(PREVIOUS_KEYS_ENV);
if (previousKeys != null && !previousKeys.isBlank()) {
for (String value : previousKeys.split(",")) {
String trimmed = value.trim();
if (!trimmed.isEmpty()) {
keys.add(decodeKey(trimmed, PREVIOUS_KEYS_ENV));
}
}
}
return new OAuthTokenCipher(primaryKey, keys);
}
public String encrypt(String plaintext) {
if (plaintext == null) {
return null;

View File

@@ -29,6 +29,8 @@ import org.springframework.test.web.servlet.MockMvc;
properties = {
"spring.security.oauth2.client.registration.twitch.client-id=test-client-id",
"spring.security.oauth2.client.registration.twitch.client-secret=test-client-secret",
"spring.datasource.url=jdbc:sqlite:target/test-${random.uuid}.db",
"IMGFLOAT_TOKEN_ENCRYPTION_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
}
)
@AutoConfigureMockMvc

View File

@@ -18,6 +18,8 @@ import org.springframework.test.web.servlet.MockMvc;
properties = {
"spring.security.oauth2.client.registration.twitch.client-id=test-client-id",
"spring.security.oauth2.client.registration.twitch.client-secret=test-client-secret",
"spring.datasource.url=jdbc:sqlite:target/test-${random.uuid}.db",
"IMGFLOAT_TOKEN_ENCRYPTION_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
}
)
@AutoConfigureMockMvc