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 javax.annotation.concurrent.Immutable;
021import java.util.List;
022import java.time.Instant;
023
024import static java.util.Objects.requireNonNull;
025
026/** A newly committed recovery-code batch; plaintext codes cannot be retrieved again from durable storage.
027 * @since 1.0.0 */
028@Immutable
029public final class RecoveryCodes {
030    private final @NonNull String credentialNamespace;
031    private final @NonNull String recoveryEpoch;
032    private final @NonNull String accountId;
033    private final @NonNull String factorId;
034    private final @NonNull String batchId;
035    private final @NonNull Instant issuedAt;
036    private final @NonNull List<@NonNull String> codes;
037
038    RecoveryCodes(@NonNull String credentialNamespace, @NonNull String recoveryEpoch, @NonNull String accountId, @NonNull String factorId, @NonNull String batchId, @NonNull Instant issuedAt, @NonNull List<@NonNull String> codes) {
039        this.credentialNamespace = requireNonNull(credentialNamespace);
040        this.recoveryEpoch = requireNonNull(recoveryEpoch);
041        this.accountId = requireNonNull(accountId);
042        this.factorId = requireNonNull(factorId);
043        this.batchId = requireNonNull(batchId);
044        this.issuedAt = requireNonNull(issuedAt);
045        this.codes = List.copyOf(requireNonNull(codes));
046    }
047
048    /** The application credential namespace.
049     * @return credentialNamespace
050     * @since 1.0.0 */
051    public @NonNull String getCredentialNamespace() { return this.credentialNamespace; }
052
053    /** The admitted recovery epoch.
054     * @return recoveryEpoch
055     * @since 1.0.0 */
056    public @NonNull String getRecoveryEpoch() { return this.recoveryEpoch; }
057
058    /** The application-approved account identifier.
059     * @return accountId
060     * @since 1.0.0 */
061    public @NonNull String getAccountId() { return this.accountId; }
062
063    /** The active factor generation for this batch.
064     * @return factorId
065     * @since 1.0.0 */
066    public @NonNull String getFactorId() { return this.factorId; }
067
068    /** The recovery-code batch generation identifier.
069     * @return batchId
070     * @since 1.0.0 */
071    public @NonNull String getBatchId() { return this.batchId; }
072
073    /** The confirmed issue instant.
074     * @return issuedAt
075     * @since 1.0.0 */
076    public @NonNull Instant getIssuedAt() { return this.issuedAt; }
077
078    /** The immutable secret-bearing list of plaintext recovery codes.
079     * @return codes
080     * @since 1.0.0 */
081    public @NonNull List<@NonNull String> getCodes() { return this.codes; }
082
083    /** Redacts all account, operation and secret data.
084     * @return a fixed description
085     * @since 1.0.0 */
086    @Override public @NonNull String toString() { return "RecoveryCodes{<redacted>}"; }
087}