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.webauthn;
018
019import org.jspecify.annotations.NonNull;
020
021/** An explicit authoritative observation: a versioned sealed record or confirmed absence.
022 * @since 1.0.0 */
023@javax.annotation.concurrent.Immutable
024public sealed interface WebAuthnStoreEntry permits WebAuthnStoreEntry.Present, WebAuthnStoreEntry.Absent {
025 /** A record and its opaque, non-reused compare-and-commit version.
026  * @since 1.0.0 */
027 @javax.annotation.concurrent.Immutable
028 final class Present implements WebAuthnStoreEntry {
029  private final byte @NonNull [] version;
030  private final byte @NonNull [] sealedBytes;
031
032  private Present(byte @NonNull [] version, byte @NonNull [] sealedBytes) {
033   if (version.length == 0 || version.length > 128 || sealedBytes.length == 0
034     || sealedBytes.length > 262144) throw new IllegalArgumentException("Invalid store entry length");
035   this.version = version.clone(); this.sealedBytes = sealedBytes.clone();
036  }
037
038  /** Copies an opaque version and a bounded sealed record.
039   * @param version the nonempty, non-reused opaque version
040   * @param sealedBytes the bounded sealed record
041   * @return the present observation
042   * @since 1.0.0 */
043  public static WebAuthnStoreEntry.@NonNull Present fromVersionAndSealedBytes(
044    byte @NonNull [] version, byte @NonNull [] sealedBytes) {
045   return new Present(version, sealedBytes);
046  }
047
048  /** Returns a defensive copy of the opaque version.
049   * @return the opaque version
050   * @since 1.0.0 */
051  public byte @NonNull [] getVersion() { return this.version.clone(); }
052
053  /** Returns a defensive copy of the sealed record.
054   * @return the sealed bytes
055   * @since 1.0.0 */
056  public byte @NonNull [] getSealedBytes() { return this.sealedBytes.clone(); }
057
058  /** Redacts the record and version. @since 1.0.0 */
059  @Override public @NonNull String toString() { return "Present{record=<redacted>}"; }
060 }
061
062 /** Confirmed absence, never an omitted read result.
063  * @since 1.0.0 */
064 @javax.annotation.concurrent.Immutable
065 final class Absent implements WebAuthnStoreEntry {
066  private static final WebAuthnStoreEntry.@NonNull Absent INSTANCE = new Absent();
067  private Absent() { }
068
069  /** Returns the singleton confirmed-absence observation.
070   * @return the confirmed absence
071   * @since 1.0.0 */
072  public static WebAuthnStoreEntry.@NonNull Absent confirmed() { return INSTANCE; }
073
074  /** Returns a fixed description. @since 1.0.0 */
075  @Override public @NonNull String toString() { return "Absent{}"; }
076 }
077}