refactor: move OIDC client secret to non public app setting (#2558)

This commit is contained in:
James Ward
2026-09-08 17:05:10 -04:00
committed by GitHub
parent 32b6384aff
commit 53abc8bf81
15 changed files with 147 additions and 50 deletions
@@ -71,14 +71,17 @@ public class OidcAuthService {
throw ApiError.FORBIDDEN.createException("OIDC is not enabled");
}
OidcProviderDetails providerDetails = appSettingService.getAppSettings().getOidcProviderDetails();
var appSettings = appSettingService.getAppSettings();
OidcProviderDetails providerDetails = appSettings.getOidcProviderDetails();
String providerClientSecret = appSettings.getOidcProviderClientSecret();
if (providerDetails == null || providerDetails.getIssuerUri() == null) {
throw ApiError.FORBIDDEN.createException("OIDC is not properly configured");
}
validateRedirectUri(redirectUri, httpRequest);
var tokenResponse = oidcTokenClient.exchangeAuthorizationCode(code, codeVerifier, redirectUri, providerDetails);
var tokenResponse = oidcTokenClient.exchangeAuthorizationCode(code, codeVerifier, redirectUri, providerDetails, providerClientSecret);
String idToken = tokenResponse.idToken();
if (idToken == null) {
@@ -40,7 +40,7 @@ public class OidcTokenClient {
Integer expiresIn
) {}
public TokenResponse exchangeAuthorizationCode(String code, String codeVerifier, String redirectUri, OidcProviderDetails providerDetails) {
public TokenResponse exchangeAuthorizationCode(String code, String codeVerifier, String redirectUri, OidcProviderDetails providerDetails, String providerClientSecret) {
var discovery = discoveryService.discover(providerDetails.getIssuerUri());
String tokenEndpoint = discovery.tokenEndpoint();
@@ -51,8 +51,8 @@ public class OidcTokenClient {
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("grant_type", "authorization_code");
body.add("client_id", providerDetails.getClientId());
if (providerDetails.getClientSecret() != null && !providerDetails.getClientSecret().isBlank()) {
body.add("client_secret", providerDetails.getClientSecret());
if (providerClientSecret != null && !providerClientSecret.isBlank()) {
body.add("client_secret", providerClientSecret);
}
body.add("code", code);
body.add("redirect_uri", redirectUri);
@@ -10,6 +10,7 @@ public enum AppSettingKey {
// @formatter:off
// ADMIN only (public settings)
OIDC_PROVIDER_DETAILS ("oidc_provider_details", true, true, List.of(PermissionType.ADMIN)),
OIDC_PROVIDER_CLIENT_SECRET ("oidc_provider_client_secret", false, false, List.of(PermissionType.ADMIN)),
OIDC_REDIRECT_URIS ("oidc_redirect_uris", true, false, List.of(PermissionType.ADMIN)),
OIDC_ENABLED ("oidc_enabled", false, true, List.of(PermissionType.ADMIN)),
OIDC_AUTO_PROVISION_DETAILS ("oidc_auto_provision_details", true, false, List.of(PermissionType.ADMIN)),
@@ -27,6 +27,7 @@ public class AppSettings {
private boolean metadataDownloadOnBookdrop;
private boolean oidcEnabled;
private OidcProviderDetails oidcProviderDetails;
private String oidcProviderClientSecret;
private List<String> oidcRedirectUris;
private OidcAutoProvisionDetails oidcAutoProvisionDetails;
private MetadataProviderSettings metadataProviderSettings;
@@ -6,7 +6,6 @@ import lombok.Data;
public class OidcProviderDetails {
private String providerName;
private String clientId;
private String clientSecret;
private String issuerUri;
private String scopes;
private ClaimMapping claimMapping;
@@ -252,9 +252,6 @@ public class AppSettingService {
builder.remoteAuthEnabled(appProperties.getRemoteAuth().isEnabled());
OidcProviderDetails details = getJsonSetting(settingsMap, AppSettingKey.OIDC_PROVIDER_DETAILS, OidcProviderDetails.class, null);
if (details != null) {
details.setClientSecret(null);
}
boolean oidcEnabled = Boolean.parseBoolean(settingsMap.getOrDefault(AppSettingKey.OIDC_ENABLED, "false"));
boolean oidcForceOnlyMode = Boolean.parseBoolean(settingsMap.getOrDefault(AppSettingKey.OIDC_FORCE_ONLY_MODE, "false"));
@@ -299,6 +296,7 @@ public class AppSettingService {
builder.pdfCacheSizeInMb(Integer.parseInt(settingsMap.getOrDefault(AppSettingKey.PDF_CACHE_SIZE_IN_MB, "5120")));
builder.maxFileUploadSizeInMb(Integer.parseInt(settingsMap.getOrDefault(AppSettingKey.MAX_FILE_UPLOAD_SIZE_IN_MB, "100")));
builder.metadataDownloadOnBookdrop(Boolean.parseBoolean(settingsMap.getOrDefault(AppSettingKey.METADATA_DOWNLOAD_ON_BOOKDROP, "true")));
builder.oidcProviderClientSecret(settingsMap.getOrDefault(AppSettingKey.OIDC_PROVIDER_CLIENT_SECRET, ""));
String sessionDurationStr = settingsMap.get(AppSettingKey.OIDC_SESSION_DURATION_HOURS);
if (sessionDurationStr != null && !sessionDurationStr.isBlank()) {
@@ -20,6 +20,7 @@ public class AppMigrationStartup {
private final MigrateProgressToFileProgressMigration migrateProgressToFileProgressMigration;
private final PopulateAuthorSortNameMigration populateAuthorSortNameMigration;
private final RemoveBundledCustomSvgIconsMigration removeBundledCustomSvgIconsMigration;
private final OIDCClientSecretSeparateKey oidcClientSecretSeparateKey;
@EventListener(ApplicationReadyEvent.class)
public void runMigrationsOnce() {
@@ -32,5 +33,6 @@ public class AppMigrationStartup {
appMigrationService.executeMigration(migrateProgressToFileProgressMigration);
appMigrationService.executeMigration(populateAuthorSortNameMigration);
appMigrationService.executeMigration(removeBundledCustomSvgIconsMigration);
appMigrationService.executeMigration(oidcClientSecretSeparateKey);
}
}
@@ -0,0 +1,79 @@
package org.booklore.service.migration.migrations;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.booklore.model.dto.settings.AppSettingKey;
import org.booklore.model.entity.AppSettingEntity;
import org.booklore.repository.AppSettingsRepository;
import org.booklore.service.migration.Migration;
import org.springframework.stereotype.Component;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.node.ObjectNode;
@Slf4j
@Component
@RequiredArgsConstructor
public class OIDCClientSecretSeparateKey implements Migration {
private static final String KEY_CLIENT_SECRET = "clientSecret";
private final AppSettingsRepository appSettingsRepository;
private final ObjectMapper objectMapper;
@Override
public String getKey() {
return "oidcClientSecretSeparateKey";
}
@Override
public String getDescription() {
return "Migrate existing OIDC Provider Client Secret from Provider Details to a dedicated key";
}
@Override
public void execute() {
log.info("Executing migration: {}", getKey());
AppSettingEntity providerDetailsSetting = appSettingsRepository.findByName(AppSettingKey.OIDC_PROVIDER_DETAILS.getDbKey());
AppSettingEntity providerClientSecretSetting = appSettingsRepository.findByName(AppSettingKey.OIDC_PROVIDER_CLIENT_SECRET.getDbKey());
if (providerDetailsSetting == null) {
log.debug("Setting does not exist, skipping");
log.info("Completed migration: {}", getKey());
return;
}
if (providerClientSecretSetting == null) {
providerClientSecretSetting = new AppSettingEntity();
providerClientSecretSetting.setName(AppSettingKey.OIDC_PROVIDER_CLIENT_SECRET.getDbKey());
}
try {
var node = objectMapper.readTree(providerDetailsSetting.getVal());
if (node instanceof ObjectNode objectNode) {
if (providerClientSecretSetting.getVal() != null) {
log.info("Skipping persisting client secret to new field: already exists");
} else if (objectNode.has(KEY_CLIENT_SECRET) && objectNode.get(KEY_CLIENT_SECRET).isString()) {
log.debug("Migrated client secret to separate key");
providerClientSecretSetting.setVal(objectNode.get(KEY_CLIENT_SECRET).asString());
}
log.debug("Removing client secret from public settings");
objectNode.remove(KEY_CLIENT_SECRET);
providerDetailsSetting.setVal(objectMapper.writeValueAsString(objectNode));
} else {
log.info("Unable to read OIDC Provider Details JSON, purging value");
providerDetailsSetting.setVal(null);
}
} catch (Exception e) {
log.info("Unable to read OIDC Provider Details JSON, purging value");
providerDetailsSetting.setVal(null);
}
appSettingsRepository.save(providerDetailsSetting);
appSettingsRepository.save(providerClientSecretSetting);
log.info("Completed migration: {}", getKey());
}
}