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 com.revetsec.OperationBinding;
020
021import org.jspecify.annotations.NonNull;
022import javax.annotation.concurrent.Immutable;
023import java.time.Instant;
024
025import static java.util.Objects.requireNonNull;
026
027/** Library-created proof of committed recovery-code consumption; apply a separate recovery-session policy.
028 * @since 1.0.0 */
029@Immutable
030public final class RecoveryCodeUse {
031    private final @NonNull String credentialNamespace;
032    private final @NonNull String recoveryEpoch;
033    private final @NonNull String accountId;
034    private final @NonNull String factorId;
035    private final @NonNull Instant verifiedAt;
036    private final @NonNull String operationBinding;
037    private final @NonNull Integer remainingRecoveryCodeCount;
038
039    RecoveryCodeUse(@NonNull String credentialNamespace, @NonNull String recoveryEpoch, @NonNull String accountId, @NonNull String factorId, @NonNull Instant verifiedAt, @NonNull String operationBinding, @NonNull Integer remainingRecoveryCodeCount) {
040        this.credentialNamespace = requireNonNull(credentialNamespace);
041        this.recoveryEpoch = requireNonNull(recoveryEpoch);
042        this.accountId = requireNonNull(accountId);
043        this.factorId = requireNonNull(factorId);
044        this.verifiedAt = requireNonNull(verifiedAt);
045        this.operationBinding = requireNonNull(operationBinding);
046        this.remainingRecoveryCodeCount = requireNonNull(remainingRecoveryCodeCount);
047    }
048
049    /** The application credential namespace.
050     * @return credentialNamespace
051     * @since 1.0.0 */
052    public @NonNull String getCredentialNamespace() { return this.credentialNamespace; }
053
054    /** The admitted recovery epoch; session completion must enforce it.
055     * @return recoveryEpoch
056     * @since 1.0.0 */
057    public @NonNull String getRecoveryEpoch() { return this.recoveryEpoch; }
058
059    /** The application-approved account identifier.
060     * @return accountId
061     * @since 1.0.0 */
062    public @NonNull String getAccountId() { return this.accountId; }
063
064    /** The active factor generation whose recovery code was consumed.
065     * @return factorId
066     * @since 1.0.0 */
067    public @NonNull String getFactorId() { return this.factorId; }
068
069    /** The confirmed consumption instant.
070     * @return verifiedAt
071     * @since 1.0.0 */
072    public @NonNull Instant getVerifiedAt() { return this.verifiedAt; }
073
074    /** The server-owned pending operation binding.
075     * @return operationBinding
076     * @since 1.0.0 */
077    public @NonNull OperationBinding getOperationBinding() {
078        return OperationBinding.fromTrustedPersistence(this.operationBinding);
079    }
080
081    /** The remaining recovery-code count after this consumption.
082     * @return remainingRecoveryCodeCount
083     * @since 1.0.0 */
084    public @NonNull Integer getRemainingRecoveryCodeCount() { return this.remainingRecoveryCodeCount; }
085
086    /** Returns the verified method provenance.
087     * @return authentication method
088     * @since 1.0.0 */
089    public @NonNull TotpAuthenticationMethod getMethod() { return TotpAuthenticationMethod.RECOVERY_CODE; }
090
091    /** Redacts all account, operation and secret data.
092     * @return a fixed description
093     * @since 1.0.0 */
094    @Override public @NonNull String toString() { return "RecoveryCodeUse{<redacted>}"; }
095}