All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.webauthn4j.data.PublicKeyCredential Maven / Gradle / Ivy

There is a newer version: 0.9.2.RELEASE
Show newest version
/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.webauthn4j.data;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.webauthn4j.data.extension.client.AuthenticationExtensionsClientOutputs;
import com.webauthn4j.data.extension.client.ExtensionClientOutput;
import com.webauthn4j.util.ArrayUtil;
import com.webauthn4j.util.Base64UrlUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Objects;

/**
 * The PublicKeyCredential interface contains the attributes that are returned to the caller
 * when a new credential is created, or a new assertion is requested.
 *
 * @see §5.1. PublicKeyCredential Interface
 */
public class PublicKeyCredential {

    // ~ Instance fields
    // ================================================================================================

    private final String id;
    private final byte[] rawId;
    private final R response;
    private final AuthenticatorAttachment authenticatorAttachment;
    private final AuthenticationExtensionsClientOutputs clientExtensionResults;

    // ~ Constructor
    // ========================================================================================================

    public PublicKeyCredential(
            @Nullable byte[] credentialId,
            @Nullable R response,
            @Nullable AuthenticationExtensionsClientOutputs clientExtensionResults) {
        this(credentialId, response, null, clientExtensionResults);
    }

    public PublicKeyCredential(
            @Nullable byte[] credentialId,
            @Nullable R response,
            @Nullable AuthenticatorAttachment authenticatorAttachment,
            @Nullable AuthenticationExtensionsClientOutputs clientExtensionResults) {
        this.id = credentialId == null ? null : Base64UrlUtil.encodeToString(credentialId);
        this.rawId = credentialId;
        this.response = response;
        this.authenticatorAttachment = authenticatorAttachment;
        this.clientExtensionResults = clientExtensionResults;
    }

    @JsonCreator
    public PublicKeyCredential(
            @Nullable @JsonProperty("id") String id,
            @Nullable @JsonProperty("rawId") byte[] rawId,
            @Nullable @JsonProperty("response") R response,
            @Nullable @JsonProperty("authenticatorAttachment") AuthenticatorAttachment authenticatorAttachment,
            @Nullable @JsonProperty("clientExtensionResults") AuthenticationExtensionsClientOutputs clientExtensionResults) {
        this.id = id;
        this.rawId = rawId;
        this.response = response;
        this.authenticatorAttachment = authenticatorAttachment;
        this.clientExtensionResults = clientExtensionResults;
    }

    public @NotNull String getType() {
        return PublicKeyCredentialType.PUBLIC_KEY.getValue();
    }

    public @Nullable String getId() {
        return id;
    }

    public @Nullable byte[] getRawId() {
        return ArrayUtil.clone(rawId);
    }

    public @Nullable R getResponse() {
        return response;
    }

    public @Nullable AuthenticatorAttachment getAuthenticatorAttachment() { return authenticatorAttachment; }

    public @Nullable AuthenticationExtensionsClientOutputs getClientExtensionResults() {
        return clientExtensionResults;
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PublicKeyCredential that = (PublicKeyCredential) o;
        return Objects.equals(id, that.id) &&
                Arrays.equals(rawId, that.rawId) &&
                Objects.equals(response, that.response) &&
                Objects.equals(authenticatorAttachment, that.authenticatorAttachment) &&
                Objects.equals(clientExtensionResults, that.clientExtensionResults);
    }

    @Override
    public int hashCode() {
        int result = Objects.hash(id, response, authenticatorAttachment, clientExtensionResults);
        result = 31 * result + Arrays.hashCode(rawId);
        return result;
    }

    @Override
    public String toString() {
        return "PublicKeyCredential(" +
                "id=" + id +
                ", response=" + response +
                ", authenticatorAttachment=" + authenticatorAttachment +
                ", clientExtensionResults=" + clientExtensionResults +
                ')';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy