001/* 002 * SPDX-License-Identifier: Apache-2.0 003 * 004 * Copyright 2025-2026 The Enola <https://enola.dev> Authors 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package dev.enola.common.secret; 019 020import java.io.IOException; 021import java.util.Optional; 022 023/** 024 * PrefixingSecretManager is a {@link SecretManager} that prefixes all keys with a given prefix. 025 * 026 * <p>This is useful, for example, to use a single {@link SecretManager} in a server environment for 027 * multiple users (or any other Subject), or in a desktop environment to store application-specific 028 * secrets on the user's desktop secret manager (like GNOME Keyring or macOS Keychain on Apple's 029 * Secure Enclave). 030 */ 031public class PrefixingSecretManager implements SecretManager { 032 033 private final String prefix; 034 private final SecretManager delegate; 035 036 public PrefixingSecretManager(String prefix, SecretManager delegate) { 037 this.prefix = prefix; 038 this.delegate = delegate; 039 } 040 041 @Override 042 public void store(String key, char[] value) throws IOException { 043 delegate.store(prefix + key, value); 044 } 045 046 @Override 047 public Optional<Secret> getOptional(String key) throws IOException { 048 return delegate.getOptional(prefix + key); 049 } 050 051 @Override 052 public void delete(String key) throws IOException { 053 delegate.delete(prefix + key); 054 } 055}