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.totp;
018
019import org.jspecify.annotations.NonNull;
020import com.google.errorprone.annotations.CheckReturnValue;
021import javax.annotation.concurrent.Immutable;
022
023import static java.util.Objects.requireNonNull;
024
025/** Maintenance resealing of an exact authenticated durable record without changing semantic state.
026 * @since 1.0.0 */
027@Immutable
028public abstract sealed class TotpStoreResealResult permits
029        TotpStoreResealResult.Resealed, TotpStoreResealResult.AlreadyCurrent, TotpStoreResealResult.Absent, TotpStoreResealResult.Unavailable, TotpStoreResealResult.Indeterminate, TotpStoreResealResult.ReconciliationRequired {
030    private static final @NonNull Resealed RESEALED = new Resealed();
031    private static final @NonNull AlreadyCurrent ALREADY_CURRENT = new AlreadyCurrent();
032    private static final @NonNull Absent ABSENT = new Absent();
033    private static final @NonNull Indeterminate INDETERMINATE = new Indeterminate();
034    private static final @NonNull ReconciliationRequired RECONCILIATION_REQUIRED = new ReconciliationRequired();
035
036    TotpStoreResealResult() { }
037    static @NonNull TotpStoreResealResult resealed() {
038        return RESEALED;
039    }
040    static @NonNull TotpStoreResealResult alreadyCurrent() {
041        return ALREADY_CURRENT;
042    }
043    static @NonNull TotpStoreResealResult absent() {
044        return ABSENT;
045    }
046    static @NonNull TotpStoreResealResult unavailable(@NonNull TotpUnavailableReason reason) {
047        return new Unavailable(reason);
048    }
049    static @NonNull TotpStoreResealResult indeterminate() {
050        return INDETERMINATE;
051    }
052    @CheckReturnValue
053    static @NonNull TotpStoreResealResult reconciliationRequired() {
054        return RECONCILIATION_REQUIRED;
055    }
056    static @NonNull TotpStoreResealResult unavailable() {
057        return unavailable(TotpUnavailableReason.DEPENDENCY_UNAVAILABLE);
058    }
059
060    /** Redacts all account, binding and secret data.
061     * @return a fixed description
062     * @since 1.0.0 */
063    @Override public final @NonNull String toString() { return "TotpStoreResealResult{<redacted>}"; }
064
065    /** The authenticated record was resealed and the replacement definitely committed.
066     * @since 1.0.0 */
067    @Immutable
068    public static final class Resealed extends TotpStoreResealResult {
069        private Resealed() { }
070    }
071
072    /** The authenticated record already uses the configured active sealing key and current format.
073     * @since 1.0.0 */
074    @Immutable
075    public static final class AlreadyCurrent extends TotpStoreResealResult {
076        private AlreadyCurrent() { }
077    }
078
079    /** The requested record was authoritatively absent in this recovery epoch.
080     * @since 1.0.0 */
081    @Immutable
082    public static final class Absent extends TotpStoreResealResult {
083        private Absent() { }
084    }
085
086    /** Infrastructure or recovery admission was unavailable; no success is established.
087     * @since 1.0.0 */
088    @Immutable
089    public static final class Unavailable extends TotpStoreResealResult {
090        private final @NonNull TotpUnavailableReason reason;
091        private Unavailable(@NonNull TotpUnavailableReason reason) {
092            this.reason = requireNonNull(reason);
093        }
094        /** Returns the reason.
095         * @return reason
096         * @since 1.0.0 */
097        public @NonNull TotpUnavailableReason getReason() { return this.reason; }
098    }
099
100    /** A write may have occurred; reconcile management state and never infer authentication success.
101     * @since 1.0.0 */
102    @Immutable
103    public static final class Indeterminate extends TotpStoreResealResult {
104        private Indeterminate() { }
105    }
106
107    /** The requested transition is known committed, but normal output could not be admitted.
108     * No proof, secret or operation detail is released.
109     * @since 1.0.0 */
110    @Immutable
111    public static final class ReconciliationRequired extends TotpStoreResealResult {
112        private ReconciliationRequired() { }
113    }
114}