Skip to content

Commit 7905906

Browse files
Implement Serial for WebAuthnAuthentication
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 539f2c5 commit 7905906

File tree

7 files changed

+51
-3
lines changed

7 files changed

+51
-3
lines changed

config/src/test/java/org/springframework/security/SpringSecurityCoreVersionSerializableTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@
193193
import org.springframework.security.web.session.HttpSessionCreatedEvent;
194194
import org.springframework.security.web.webauthn.api.Bytes;
195195
import org.springframework.security.web.webauthn.api.ImmutablePublicKeyCredentialUserEntity;
196+
import org.springframework.security.web.webauthn.api.PublicKeyCredentialUserEntity;
197+
import org.springframework.security.web.webauthn.api.TestBytes;
196198
import org.springframework.security.web.webauthn.api.TestPublicKeyCredentialUserEntity;
199+
import org.springframework.security.web.webauthn.authentication.WebAuthnAuthentication;
197200

198201
import static org.assertj.core.api.Assertions.assertThat;
199202
import static org.assertj.core.api.Assertions.fail;
@@ -513,9 +516,18 @@ class SpringSecurityCoreVersionSerializableTests {
513516
(r) -> new HttpSessionCreatedEvent(new MockHttpSession()));
514517

515518
// webauthn
516-
generatorByClassName.put(Bytes.class, (r) -> Bytes.random());
519+
generatorByClassName.put(Bytes.class, (r) -> TestBytes.getInstance());
517520
generatorByClassName.put(ImmutablePublicKeyCredentialUserEntity.class,
518-
(r) -> TestPublicKeyCredentialUserEntity.userEntity().build());
521+
(r) -> TestPublicKeyCredentialUserEntity.userEntity().id(TestBytes.getInstance()).build());
522+
generatorByClassName.put(WebAuthnAuthentication.class, (r) -> {
523+
PublicKeyCredentialUserEntity userEntity = TestPublicKeyCredentialUserEntity.userEntity()
524+
.id(TestBytes.getInstance())
525+
.build();
526+
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
527+
WebAuthnAuthentication webAuthnAuthentication = new WebAuthnAuthentication(userEntity, authorities);
528+
webAuthnAuthentication.setDetails(details);
529+
return webAuthnAuthentication;
530+
});
519531
}
520532

521533
@ParameterizedTest

web/src/main/java/org/springframework/security/web/webauthn/authentication/WebAuthnAuthentication.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.web.webauthn.authentication;
1818

19+
import java.beans.Transient;
20+
import java.io.Serial;
1921
import java.util.Collection;
2022

2123
import org.springframework.security.authentication.AbstractAuthenticationToken;
@@ -33,6 +35,9 @@
3335
*/
3436
public class WebAuthnAuthentication extends AbstractAuthenticationToken {
3537

38+
@Serial
39+
private static final long serialVersionUID = -4879907158750659197L;
40+
3641
private final PublicKeyCredentialUserEntity principal;
3742

3843
public WebAuthnAuthentication(PublicKeyCredentialUserEntity principal,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.webauthn.api;
18+
19+
public final class TestBytes {
20+
21+
private static final Bytes INSTANCE = Bytes.random();
22+
23+
public static Bytes getInstance() {
24+
25+
return INSTANCE;
26+
}
27+
28+
private TestBytes() {
29+
}
30+
31+
}

0 commit comments

Comments
 (0)