001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.revetsec;
018
019import com.google.errorprone.annotations.CheckReturnValue;
020import com.revetsec.internal.encoding.Base64Url;
021import com.revetsec.internal.encoding.EncodingException;
022import org.jspecify.annotations.NonNull;
023
024import javax.annotation.concurrent.Immutable;
025
026import static java.util.Objects.requireNonNull;
027
028/**
029 * Immutable identities of an authority and its active data/recovery admission.
030 * <p>
031 * This carrier validates identifier syntax only. It does not authenticate an authority, establish admission,
032 * generate an identity or reinterpret an engine's existing recovery-epoch address.
033 *
034 * @since 1.0.0
035 */
036@Immutable
037@CheckReturnValue
038public final class StoreAdmission {
039        private final @NonNull String authorityId;
040        private final @NonNull String recoveryGeneration;
041
042        private StoreAdmission(@NonNull String authorityId, @NonNull String recoveryGeneration) {
043                validateIdentifier(authorityId);
044                validateIdentifier(recoveryGeneration);
045                this.authorityId = authorityId;
046                this.recoveryGeneration = recoveryGeneration;
047        }
048
049        /**
050         * Validates and retains the two separately provisioned identifiers.
051         *
052         * @param authorityId canonical unpadded Base64URL encoding of exactly 32 bytes
053         * @param recoveryGeneration canonical unpadded Base64URL encoding of exactly 32 bytes
054         * @return the immutable identities, without any admission or authenticity assertion
055         * @throws NullPointerException if an argument is {@code null}
056         * @throws IllegalArgumentException if an identifier is not in the exact canonical form
057         * @since 1.0.0
058         */
059        public static @NonNull StoreAdmission fromIdentifiers(@NonNull String authorityId,
060                        @NonNull String recoveryGeneration) {
061                return new StoreAdmission(authorityId, recoveryGeneration);
062        }
063
064        /**
065         * Returns the authority identity.
066         * @return the canonical authority identifier
067         * @since 1.0.0
068         */
069        public @NonNull String getAuthorityId() {
070                return this.authorityId;
071        }
072
073        /**
074         * Returns the active data/recovery generation.
075         * @return the canonical recovery-generation identifier
076         * @since 1.0.0
077         */
078        public @NonNull String getRecoveryGeneration() {
079                return this.recoveryGeneration;
080        }
081
082        /**
083         * Returns a fixed description that discloses neither identifier.
084         * @return the redacted description
085         * @since 1.0.0
086         */
087        @Override
088        public @NonNull String toString() {
089                return "StoreAdmission{admission=<redacted>}";
090        }
091
092        static void validateIdentifier(@NonNull String value) {
093                requireNonNull(value);
094                if (value.length() != 43)
095                        throw new IllegalArgumentException("Invalid store-time identifier.");
096                try {
097                        if (Base64Url.decode(value).length != 32)
098                                throw new IllegalArgumentException("Invalid store-time identifier.");
099                } catch (EncodingException exception) {
100                        throw new IllegalArgumentException("Invalid store-time identifier.");
101                }
102        }
103}