chore(oidc): backfill + add mobile redirect URI whitelist setting to the backend (#1268)
This commit is contained in:
@@ -26,6 +26,8 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.net.URI;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
@@ -37,7 +39,10 @@ import java.util.regex.Pattern;
|
||||
@AllArgsConstructor
|
||||
public class OidcAuthService {
|
||||
|
||||
private static final String MOBILE_SCHEME = "booklore://";
|
||||
private static final String DEFAULT_MOBILE_REDIRECT_URI = "grimmory://oauth2-callback";
|
||||
// TODO(grimmory-cleanup): Remove after the Booklore to Grimmory migration window closes.
|
||||
private static final String LEGACY_MOBILE_REDIRECT_URI = "booklore://oauth2-callback";
|
||||
private static final String WILDCARD_REDIRECT_URI = "*";
|
||||
private static final String OAUTH2_CALLBACK_PATH = "/oauth2-callback";
|
||||
private static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/+$");
|
||||
|
||||
@@ -123,8 +128,8 @@ public class OidcAuthService {
|
||||
throw ApiError.OIDC_INVALID_REDIRECT_URI.createException();
|
||||
}
|
||||
|
||||
if (redirectUri.startsWith(MOBILE_SCHEME)) {
|
||||
if (!redirectUri.equals(MOBILE_SCHEME + "oauth2-callback")) {
|
||||
if (isMobileRedirectUri(redirectUri)) {
|
||||
if (!isAllowedMobileRedirectUri(redirectUri)) {
|
||||
throw ApiError.OIDC_INVALID_REDIRECT_URI.createException();
|
||||
}
|
||||
return;
|
||||
@@ -168,11 +173,48 @@ public class OidcAuthService {
|
||||
}
|
||||
|
||||
public void validateAppRedirectUri(String appRedirectUri) {
|
||||
if (appRedirectUri == null || !appRedirectUri.startsWith(MOBILE_SCHEME)) {
|
||||
if (!isMobileRedirectUri(appRedirectUri) || !isAllowedMobileRedirectUri(appRedirectUri)) {
|
||||
throw ApiError.OIDC_INVALID_REDIRECT_URI.createException();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAllowedMobileRedirectUri(String redirectUri) {
|
||||
List<String> effectiveRedirectUris = getEffectiveMobileRedirectUris();
|
||||
return effectiveRedirectUris.contains(WILDCARD_REDIRECT_URI) || effectiveRedirectUris.contains(redirectUri);
|
||||
}
|
||||
|
||||
private List<String> getEffectiveMobileRedirectUris() {
|
||||
List<String> configuredRedirectUris = appSettingService.getAppSettings().getOidcRedirectUris();
|
||||
List<String> effectiveRedirectUris = configuredRedirectUris == null || configuredRedirectUris.isEmpty()
|
||||
? new ArrayList<>(List.of(DEFAULT_MOBILE_REDIRECT_URI))
|
||||
: new ArrayList<>(configuredRedirectUris);
|
||||
|
||||
if (effectiveRedirectUris.contains(DEFAULT_MOBILE_REDIRECT_URI)
|
||||
&& !effectiveRedirectUris.contains(LEGACY_MOBILE_REDIRECT_URI)) {
|
||||
effectiveRedirectUris.add(LEGACY_MOBILE_REDIRECT_URI);
|
||||
}
|
||||
|
||||
return effectiveRedirectUris;
|
||||
}
|
||||
|
||||
private boolean isMobileRedirectUri(String redirectUri) {
|
||||
if (redirectUri == null || redirectUri.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
URI uri = URI.create(redirectUri);
|
||||
String scheme = uri.getScheme();
|
||||
return scheme != null
|
||||
&& !scheme.isBlank()
|
||||
&& !"http".equalsIgnoreCase(scheme)
|
||||
&& !"https".equalsIgnoreCase(scheme)
|
||||
&& uri.getFragment() == null;
|
||||
} catch (IllegalArgumentException _) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void persistOidcSession(BookLoreUserEntity user, String subject, String issuer,
|
||||
OidcTokenClient.TokenResponse tokenResponse, String rawIdToken,
|
||||
JWTClaimsSet claims) {
|
||||
|
||||
@@ -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_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)),
|
||||
OIDC_SESSION_DURATION_HOURS ("oidc_session_duration_hours", false, false, List.of(PermissionType.ADMIN)),
|
||||
@@ -69,4 +70,4 @@ public enum AppSettingKey {
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown setting key: " + dbKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ public class AppSettings {
|
||||
private boolean metadataDownloadOnBookdrop;
|
||||
private boolean oidcEnabled;
|
||||
private OidcProviderDetails oidcProviderDetails;
|
||||
private List<String> oidcRedirectUris;
|
||||
private OidcAutoProvisionDetails oidcAutoProvisionDetails;
|
||||
private MetadataProviderSettings metadataProviderSettings;
|
||||
private MetadataMatchWeights metadataMatchWeights;
|
||||
@@ -39,4 +40,4 @@ public class AppSettings {
|
||||
private String oidcGroupSyncMode;
|
||||
private boolean oidcForceOnlyMode;
|
||||
private String diskType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,13 +22,18 @@ import org.springframework.stereotype.Service;
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.core.type.TypeReference;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@DependsOnDatabaseInitialization
|
||||
public class AppSettingService {
|
||||
private static final String DEFAULT_MOBILE_REDIRECT_URI = "grimmory://oauth2-callback";
|
||||
private static final String WILDCARD_REDIRECT_URI = "*";
|
||||
|
||||
private final AppProperties appProperties;
|
||||
private final SettingPersistenceHelper settingPersistenceHelper;
|
||||
@@ -57,6 +62,10 @@ public class AppSettingService {
|
||||
|
||||
validatePermission(key, user);
|
||||
|
||||
if (key == AppSettingKey.OIDC_REDIRECT_URIS) {
|
||||
val = validateAndNormalizeOidcRedirectUris(val);
|
||||
}
|
||||
|
||||
if (key == AppSettingKey.OIDC_FORCE_ONLY_MODE) {
|
||||
validateOidcForceOnlyMode(val);
|
||||
}
|
||||
@@ -107,6 +116,64 @@ public class AppSettingService {
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> validateAndNormalizeOidcRedirectUris(Object val) {
|
||||
if (val == null) {
|
||||
return List.of();
|
||||
}
|
||||
if (!(val instanceof List<?> rawValues)) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("OIDC redirect URIs must be an array");
|
||||
}
|
||||
|
||||
List<String> redirectUris = rawValues.stream()
|
||||
.map(value -> {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (!(value instanceof String stringValue)) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("OIDC redirect URIs must be an array of strings");
|
||||
}
|
||||
return stringValue.trim();
|
||||
})
|
||||
.toList();
|
||||
|
||||
if (redirectUris.contains(WILDCARD_REDIRECT_URI) && redirectUris.size() > 1) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Wildcard redirect URI must be the only value");
|
||||
}
|
||||
|
||||
Set<String> uniqueUris = new LinkedHashSet<>();
|
||||
for (String redirectUri : redirectUris) {
|
||||
if (redirectUri == null || redirectUri.isBlank()) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Redirect URI cannot be blank");
|
||||
}
|
||||
if (!WILDCARD_REDIRECT_URI.equals(redirectUri)) {
|
||||
validateMobileRedirectUriShape(redirectUri);
|
||||
}
|
||||
if (!uniqueUris.add(redirectUri)) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Duplicate redirect URI: " + redirectUri);
|
||||
}
|
||||
}
|
||||
|
||||
return List.copyOf(uniqueUris);
|
||||
}
|
||||
|
||||
private void validateMobileRedirectUriShape(String redirectUri) {
|
||||
try {
|
||||
URI uri = URI.create(redirectUri);
|
||||
String scheme = uri.getScheme();
|
||||
if (scheme == null || scheme.isBlank()) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Redirect URI must include a scheme");
|
||||
}
|
||||
if ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme)) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Redirect URI must use a custom mobile scheme");
|
||||
}
|
||||
if (uri.getFragment() != null) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Redirect URI must not contain a fragment");
|
||||
}
|
||||
} catch (IllegalArgumentException _) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Redirect URI is not a valid URI: " + redirectUri);
|
||||
}
|
||||
}
|
||||
|
||||
@Cacheable("publicSettings")
|
||||
public PublicAppSetting getPublicSettings() {
|
||||
return buildPublicSetting();
|
||||
@@ -144,6 +211,8 @@ public class AppSettingService {
|
||||
builder.libraryMetadataRefreshOptions(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.LIBRARY_METADATA_REFRESH_OPTIONS, new TypeReference<>() {
|
||||
}, List.of(), true));
|
||||
builder.oidcProviderDetails(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.OIDC_PROVIDER_DETAILS, OidcProviderDetails.class, null, false));
|
||||
builder.oidcRedirectUris(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.OIDC_REDIRECT_URIS, new TypeReference<>() {
|
||||
}, List.of(DEFAULT_MOBILE_REDIRECT_URI), true));
|
||||
builder.oidcAutoProvisionDetails(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.OIDC_AUTO_PROVISION_DETAILS, OidcAutoProvisionDetails.class, new OidcAutoProvisionDetails(), true));
|
||||
builder.metadataProviderSettings(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.METADATA_PROVIDER_SETTINGS, MetadataProviderSettings.class, settingPersistenceHelper.getDefaultMetadataProviderSettings(), true));
|
||||
builder.metadataMatchWeights(settingPersistenceHelper.getJsonSetting(settingsMap, AppSettingKey.METADATA_MATCH_WEIGHTS, MetadataMatchWeights.class, settingPersistenceHelper.getDefaultMetadataMatchWeights(), true));
|
||||
|
||||
Reference in New Issue
Block a user