Skip to content

Commit ea88671

Browse files
Kehrlannrwinch
authored andcommitted
Update webauthn4j usage, use non-deprecated methods
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent 9dde697 commit ea88671

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

webauthn/src/main/java/org/springframework/security/web/webauthn/management/Webauthn4JRelyingPartyOperations.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
import java.util.Collections;
2323
import java.util.HashSet;
2424
import java.util.List;
25+
import java.util.Objects;
2526
import java.util.Set;
2627
import java.util.function.Consumer;
2728
import java.util.stream.Collectors;
2829

2930
import com.webauthn4j.WebAuthnManager;
30-
import com.webauthn4j.authenticator.Authenticator;
31-
import com.webauthn4j.authenticator.AuthenticatorImpl;
3231
import com.webauthn4j.converter.util.CborConverter;
3332
import com.webauthn4j.converter.util.ObjectConverter;
33+
import com.webauthn4j.credential.CredentialRecordImpl;
3434
import com.webauthn4j.data.AuthenticationData;
3535
import com.webauthn4j.data.AuthenticationParameters;
3636
import com.webauthn4j.data.RegistrationData;
@@ -248,9 +248,7 @@ public CredentialRecord registerCredential(RelyingPartyRegistrationRequest rpReg
248248
byte[] attestationObject = response.getAttestationObject().getBytes();
249249
byte[] clientDataJSON = response.getClientDataJSON().getBytes();
250250
Challenge challenge = new DefaultChallenge(base64Challenge);
251-
byte[] tokenBindingId = null /* set tokenBindingId */; // FIXME:
252-
// https://www.w3.org/TR/webauthn-1/#dom-collectedclientdata-tokenbinding
253-
ServerProperty serverProperty = new ServerProperty(origins, rpId, challenge, tokenBindingId);
251+
ServerProperty serverProperty = new ServerProperty(origins, rpId, challenge);
254252
boolean userVerificationRequired = creationOptions.getAuthenticatorSelection()
255253
.getUserVerification() == UserVerificationRequirement.REQUIRED;
256254
// requireUserPresence The constant Boolean value true
@@ -263,7 +261,7 @@ public CredentialRecord registerCredential(RelyingPartyRegistrationRequest rpReg
263261
transports);
264262
RegistrationParameters registrationParameters = new RegistrationParameters(serverProperty, pubKeyCredParams,
265263
userVerificationRequired, userPresenceRequired);
266-
RegistrationData wa4jRegistrationData = this.webAuthnManager.validate(webauthn4jRegistrationRequest,
264+
RegistrationData wa4jRegistrationData = this.webAuthnManager.verify(webauthn4jRegistrationRequest,
267265
registrationParameters);
268266
AttestationObject wa4jAttestationObject = wa4jRegistrationData.getAttestationObject();
269267
Assert.notNull(wa4jAttestationObject, "attestationObject cannot be null");
@@ -306,7 +304,7 @@ public CredentialRecord registerCredential(RelyingPartyRegistrationRequest rpReg
306304

307305
private List<com.webauthn4j.data.PublicKeyCredentialParameters> convertCredentialParamsToWebauthn4j(
308306
List<PublicKeyCredentialParameters> parameters) {
309-
return parameters.stream().map(this::convertParamToWebauthn4j).collect(Collectors.toUnmodifiableList());
307+
return parameters.stream().map(this::convertParamToWebauthn4j).toList();
310308
}
311309

312310
private com.webauthn4j.data.PublicKeyCredentialParameters convertParamToWebauthn4j(
@@ -382,28 +380,29 @@ public PublicKeyCredentialUserEntity authenticate(RelyingPartyAuthenticationRequ
382380
.getAuthenticatorData();
383381
AttestedCredentialData wa4jCredData = wa4jAuthData.getAttestedCredentialData();
384382
Assert.notNull(wa4jCredData, "attestedCredentialData cannot be null");
385-
AttestedCredentialData data = new AttestedCredentialData(wa4jCredData.getAaguid(), keyId.getBytes(),
386-
wa4jCredData.getCOSEKey());
387383

388-
Authenticator authenticator = new AuthenticatorImpl(data, wa4jAttestationObject.getAttestationStatement(),
389-
credentialRecord.getSignatureCount());
390384
Set<Origin> origins = toOrigins();
391385
Challenge challenge = new DefaultChallenge(requestOptions.getChallenge().getBytes());
392-
// FIXME: should populate this
393-
byte[] tokenBindingId = null /* set tokenBindingId */;
394386
String rpId = requestOptions.getRpId();
395387
Assert.notNull(rpId, "rpId cannot be null");
396-
ServerProperty serverProperty = new ServerProperty(origins, rpId, challenge, tokenBindingId);
388+
ServerProperty serverProperty = new ServerProperty(origins, rpId, challenge);
397389
boolean userVerificationRequired = request.getRequestOptions()
398390
.getUserVerification() == UserVerificationRequirement.REQUIRED;
399391

400392
com.webauthn4j.data.AuthenticationRequest authenticationRequest = new com.webauthn4j.data.AuthenticationRequest(
401393
request.getPublicKey().getId().getBytes(), assertionResponse.getAuthenticatorData().getBytes(),
402394
assertionResponse.getClientDataJSON().getBytes(), assertionResponse.getSignature().getBytes());
403-
AuthenticationParameters authenticationParameters = new AuthenticationParameters(serverProperty, authenticator,
404-
userVerificationRequired);
405395

406-
AuthenticationData wa4jAuthenticationData = this.webAuthnManager.validate(authenticationRequest,
396+
// CollectedClientData and ExtensionsClientOutputs is registration data, and can
397+
// be null at authentication time.
398+
com.webauthn4j.credential.CredentialRecord wa4jCredentialRecord = new CredentialRecordImpl(
399+
wa4jAttestationObject, null, null, convertTransportsToWebauthn4j(credentialRecord.getTransports()));
400+
List<byte[]> allowCredentials = convertAllowedCredentialsToWebauthn4j(
401+
request.getRequestOptions().getAllowCredentials());
402+
AuthenticationParameters authenticationParameters = new AuthenticationParameters(serverProperty,
403+
wa4jCredentialRecord, allowCredentials.isEmpty() ? null : allowCredentials, userVerificationRequired);
404+
405+
AuthenticationData wa4jAuthenticationData = this.webAuthnManager.verify(authenticationRequest,
407406
authenticationParameters);
408407

409408
AuthenticatorData<AuthenticationExtensionAuthenticatorOutput> wa4jValidatedAuthData = wa4jAuthenticationData
@@ -424,4 +423,21 @@ public PublicKeyCredentialUserEntity authenticate(RelyingPartyAuthenticationRequ
424423
return userEntity;
425424
}
426425

426+
private static Set<com.webauthn4j.data.AuthenticatorTransport> convertTransportsToWebauthn4j(
427+
Set<AuthenticatorTransport> transports) {
428+
return transports.stream()
429+
.map(AuthenticatorTransport::getValue)
430+
.map(com.webauthn4j.data.AuthenticatorTransport::create)
431+
.collect(Collectors.toSet());
432+
}
433+
434+
private static List<byte[]> convertAllowedCredentialsToWebauthn4j(
435+
List<PublicKeyCredentialDescriptor> allowedCredentials) {
436+
return allowedCredentials.stream()
437+
.map(PublicKeyCredentialDescriptor::getId)
438+
.filter(Objects::nonNull)
439+
.map(Bytes::getBytes)
440+
.collect(Collectors.toList());
441+
}
442+
427443
}

0 commit comments

Comments
 (0)