Skip to content

Commit

Permalink
Make PublicKeyCredentialRequestOptions Serializable
Browse files Browse the repository at this point in the history
Closes gh-16432

Signed-off-by: Max Batischev <[email protected]>
  • Loading branch information
franticticktick committed Jan 20, 2025
1 parent 6f3e2ac commit 24a3e46
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -30,6 +30,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -170,9 +171,21 @@
import org.springframework.security.web.csrf.MissingCsrfTokenException;
import org.springframework.security.web.firewall.RequestRejectedException;
import org.springframework.security.web.server.firewall.ServerExchangeRejectedException;
import org.springframework.security.web.webauthn.api.AuthenticationExtensionsClientInputs;
import org.springframework.security.web.webauthn.api.AuthenticatorTransport;
import org.springframework.security.web.webauthn.api.Bytes;
import org.springframework.security.web.webauthn.api.CredProtectAuthenticationExtensionsClientInput;
import org.springframework.security.web.webauthn.api.ImmutableAuthenticationExtensionsClientInput;
import org.springframework.security.web.webauthn.api.ImmutableAuthenticationExtensionsClientInputs;
import org.springframework.security.web.webauthn.api.PublicKeyCredentialDescriptor;
import org.springframework.security.web.webauthn.api.PublicKeyCredentialRequestOptions;
import org.springframework.security.web.webauthn.api.PublicKeyCredentialType;
import org.springframework.security.web.webauthn.api.UserVerificationRequirement;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.springframework.security.web.webauthn.api.CredProtectAuthenticationExtensionsClientInput.*;
import static org.springframework.security.web.webauthn.api.CredProtectAuthenticationExtensionsClientInput.CredProtect.ProtectionPolicy.*;

/**
* Tests that Spring Security classes that implements {@link Serializable} and have the
Expand Down Expand Up @@ -322,6 +335,38 @@ class SpringSecurityCoreVersionSerializableTests {
generatorByClassName.put(OAuth2IntrospectionException.class,
(r) -> new OAuth2IntrospectionException("message", new RuntimeException()));

//webauthn
CredProtect credProtect = new CredProtect(USER_VERIFICATION_OPTIONAL, true);
Bytes id = new Bytes(("test").getBytes());
AuthenticationExtensionsClientInputs inputs = new ImmutableAuthenticationExtensionsClientInputs(ImmutableAuthenticationExtensionsClientInput.credProps);
// @formatter:off
PublicKeyCredentialDescriptor descriptor = PublicKeyCredentialDescriptor.builder()
.id(id)
.type(PublicKeyCredentialType.PUBLIC_KEY)
.transports(Set.of(AuthenticatorTransport.USB))
.build();
// @formatter:on
generatorByClassName.put(AuthenticatorTransport.class, (a) -> AuthenticatorTransport.USB);
generatorByClassName.put(PublicKeyCredentialType.class, (k) -> PublicKeyCredentialType.PUBLIC_KEY);
generatorByClassName.put(UserVerificationRequirement.class, (r) -> UserVerificationRequirement.REQUIRED);
generatorByClassName.put(CredProtect.class, (c) -> credProtect);
generatorByClassName.put(CredProtectAuthenticationExtensionsClientInput.class, (c) -> new CredProtectAuthenticationExtensionsClientInput(credProtect));
generatorByClassName.put(ImmutableAuthenticationExtensionsClientInputs.class, (i) -> inputs);
generatorByClassName.put(ImmutableAuthenticationExtensionsClientInput.class, (i) -> ImmutableAuthenticationExtensionsClientInput.credProps);
generatorByClassName.put(Bytes.class, (b) -> id);
generatorByClassName.put(PublicKeyCredentialDescriptor.class, (d) -> descriptor);
// @formatter:off
generatorByClassName.put(PublicKeyCredentialRequestOptions.class, (o) -> PublicKeyCredentialRequestOptions.builder()
.allowCredentials(List.of(descriptor))
.rpId("example.localhost")
.challenge(Bytes.fromBase64("I69THX904Q8ONhCgUgOu2PCQCcEjTDiNmokdbgsAsYU"))
.userVerification(UserVerificationRequirement.REQUIRED)
.extensions(inputs)
.timeout(Duration.ofMinutes(5))
.build()
);
// @formatter:on

// core
generatorByClassName.put(RunAsUserToken.class, (r) -> {
RunAsUserToken token = new RunAsUserToken("key", user, "creds", user.getAuthorities(),
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package org.springframework.security.web.webauthn.api;

import java.io.Serial;
import java.io.Serializable;

import org.springframework.security.core.SpringSecurityCoreVersion;

/**
* <a href=
* "https://www.w3.org/TR/webauthn-3/#enumdef-authenticatortransport">AuthenticatorTransport</a>
Expand All @@ -31,7 +30,8 @@
*/
public final class AuthenticatorTransport implements Serializable {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = -5617945441117386982L;

/**
* <a href="https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-usb">usbc</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package org.springframework.security.web.webauthn.api;

import java.io.Serial;
import java.io.Serializable;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.util.Assert;

/**
Expand All @@ -32,7 +32,8 @@
*/
public final class Bytes implements Serializable {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = -7420539646106189663L;

private static final SecureRandom RANDOM = new SecureRandom();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package org.springframework.security.web.webauthn.api;

import java.io.Serial;
import java.io.Serializable;

import org.springframework.security.core.SpringSecurityCoreVersion;

/**
* Implements <a href=
* "https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#sctn-credProtect-extension">
Expand All @@ -31,7 +30,8 @@
public class CredProtectAuthenticationExtensionsClientInput
implements AuthenticationExtensionsClientInput<CredProtectAuthenticationExtensionsClientInput.CredProtect> {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = -6418175591005843455L;

private final CredProtect input;

Expand All @@ -51,7 +51,8 @@ public CredProtect getInput() {

public static class CredProtect implements Serializable {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = 109597301115842688L;

private final ProtectionPolicy credProtectionPolicy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.security.web.webauthn.api;

import org.springframework.security.core.SpringSecurityCoreVersion;
import java.io.Serial;

/**
* An immutable {@link AuthenticationExtensionsClientInput}.
Expand All @@ -28,7 +28,8 @@
*/
public class ImmutableAuthenticationExtensionsClientInput<T> implements AuthenticationExtensionsClientInput<T> {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = -1738152485672656808L;

/**
* https://www.w3.org/TR/webauthn-3/#sctn-authenticator-credential-properties-extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package org.springframework.security.web.webauthn.api;

import java.io.Serial;
import java.util.Arrays;
import java.util.List;

import org.springframework.security.core.SpringSecurityCoreVersion;

/**
* An immutable implementation of {@link AuthenticationExtensionsClientInputs}.
*
Expand All @@ -29,7 +28,8 @@
*/
public class ImmutableAuthenticationExtensionsClientInputs implements AuthenticationExtensionsClientInputs {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = 4277817521578485720L;

private final List<AuthenticationExtensionsClientInput> inputs;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package org.springframework.security.web.webauthn.api;

import java.io.Serial;
import java.io.Serializable;
import java.util.Set;

import org.springframework.security.core.SpringSecurityCoreVersion;

/**
* <a href=
* "https://www.w3.org/TR/webauthn-3/#dictdef-publickeycredentialdescriptor">PublicKeyCredentialDescriptor</a>
Expand All @@ -34,7 +33,8 @@
*/
public final class PublicKeyCredentialDescriptor implements Serializable {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = 8793385059692676240L;

private final PublicKeyCredentialType type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.springframework.security.web.webauthn.api;

import java.io.Serial;
import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.util.Assert;

/**
Expand All @@ -36,7 +36,8 @@
*/
public final class PublicKeyCredentialRequestOptions implements Serializable {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = -2970057592835694354L;

private final Bytes challenge;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package org.springframework.security.web.webauthn.api;

import java.io.Serial;
import java.io.Serializable;

import org.springframework.security.core.SpringSecurityCoreVersion;

/**
* The <a href=
* "https://www.w3.org/TR/webauthn-3/#enum-credentialType">PublicKeyCredentialType</a>
Expand All @@ -30,7 +29,8 @@
*/
public final class PublicKeyCredentialType implements Serializable {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = 7025333122210061679L;

/**
* The only credential type that currently exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package org.springframework.security.web.webauthn.api;

import java.io.Serial;
import java.io.Serializable;

import org.springframework.security.core.SpringSecurityCoreVersion;

/**
* <a href=
* "https://www.w3.org/TR/webauthn-3/#enumdef-userverificationrequirement">UserVerificationRequirement</a>
Expand All @@ -30,7 +29,8 @@
*/
public final class UserVerificationRequirement implements Serializable {

private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
@Serial
private static final long serialVersionUID = -2801001231345540040L;

/**
* The <a href=
Expand Down

0 comments on commit 24a3e46

Please sign in to comment.