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
021import javax.annotation.concurrent.Immutable;
022import java.util.ArrayList;
023import java.util.List;
024
025/** App-authorized, authoritative credential listing result. No identity proof is conferred.
026 * @since 1.0.0 */
027@Immutable
028public abstract sealed class WebAuthnCredentialListResult permits
029        WebAuthnCredentialListResult.Listed, WebAuthnCredentialListResult.Unavailable {
030    private static final @NonNull Unavailable UNAVAILABLE = new Unavailable();
031    WebAuthnCredentialListResult() { }
032    static @NonNull WebAuthnCredentialListResult listed(@NonNull List<byte @NonNull []> ids) {
033        return new Listed(ids);
034    }
035    static @NonNull WebAuthnCredentialListResult unavailable() { return UNAVAILABLE; }
036    /** Redacts credential identifiers.
037     * @return fixed description
038     * @since 1.0.0 */
039    @Override public final @NonNull String toString() { return "WebAuthnCredentialListResult{<redacted>}"; }
040
041    /** The account's active credential IDs at the authoritative read point.
042     * @since 1.0.0 */
043    @Immutable public static final class Listed extends WebAuthnCredentialListResult {
044        private final byte @NonNull [][] credentialIds;
045        private Listed(@NonNull List<byte @NonNull []> ids) {
046            this.credentialIds = new byte[ids.size()][];
047            for (int i = 0; i < ids.size(); i++) this.credentialIds[i] = ids.get(i).clone();
048        }
049        /** Returns defensive copies of active credential IDs.
050         * @return immutable list containing copied IDs
051         * @since 1.0.0 */
052        public @NonNull List<byte @NonNull []> getCredentialIds() {
053            List<byte[]> copy = new ArrayList<>(this.credentialIds.length);
054            for (byte[] id : this.credentialIds) copy.add(id.clone());
055            return List.copyOf(copy);
056        }
057    }
058
059    /** The authoritative listing could not be established.
060     * @since 1.0.0 */
061    @Immutable public static final class Unavailable extends WebAuthnCredentialListResult {
062        private Unavailable() { }
063    }
064}