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 org.jspecify.annotations.Nullable;
021
022import javax.annotation.concurrent.Immutable;
023import java.util.Optional;
024
025import static java.util.Objects.requireNonNull;
026
027/** Non-secret account status at an authoritative read point; no authentication proof is conferred.
028 * @since 1.0.0 */
029@Immutable
030public final class TotpStatus {
031    private final @Nullable TotpFactor activeFactor;
032    private final @Nullable TotpPendingEnrollment pendingEnrollment;
033    private final @Nullable String recoveryBatchId;
034    private final @NonNull Integer remainingRecoveryCodeCount;
035
036    TotpStatus(@Nullable TotpFactor activeFactor, @Nullable TotpPendingEnrollment pendingEnrollment,
037            @Nullable String recoveryBatchId, @NonNull Integer remainingRecoveryCodeCount) {
038        this.activeFactor = activeFactor;
039        this.pendingEnrollment = pendingEnrollment;
040        this.recoveryBatchId = recoveryBatchId;
041        this.remainingRecoveryCodeCount = requireNonNull(remainingRecoveryCodeCount);
042    }
043
044    /** Returns the current active factor generation when enrolled.
045     * @return active factor
046     * @since 1.0.0 */
047    public @NonNull Optional<@NonNull TotpFactor> getActiveFactor() {
048        return Optional.ofNullable(this.activeFactor);
049    }
050
051    /** Returns the pending enrollment for reconciling management operations.
052     * @return pending enrollment
053     * @since 1.0.0 */
054    public @NonNull Optional<@NonNull TotpPendingEnrollment> getPendingEnrollment() {
055        return Optional.ofNullable(this.pendingEnrollment);
056    }
057
058    /** Returns the current recovery-code batch generation.
059     * @return recovery batch identifier
060     * @since 1.0.0 */
061    public @NonNull Optional<@NonNull String> getRecoveryBatchId() {
062        return Optional.ofNullable(this.recoveryBatchId);
063    }
064
065    /** Returns the number of currently usable recovery codes.
066     * @return remaining count, zero when no batch is active
067     * @since 1.0.0 */
068    public @NonNull Integer getRemainingRecoveryCodeCount() { return this.remainingRecoveryCodeCount; }
069
070    /** Redacts account and generation information.
071     * @return a fixed description
072     * @since 1.0.0 */
073    @Override public @NonNull String toString() { return "TotpStatus{<redacted>}"; }
074}