Skip to content

Commit 871c4e2

Browse files
committed
Remove Optional from field types
1 parent 8e79ab1 commit 871c4e2

21 files changed

+492
-289
lines changed

webauthn-server-core/src/main/java/com/yubico/webauthn/AssertionRequest.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import com.fasterxml.jackson.annotation.JsonProperty;
2929
import com.yubico.webauthn.data.PublicKeyCredentialRequestOptions;
3030
import java.util.Optional;
31-
import lombok.AccessLevel;
32-
import lombok.AllArgsConstructor;
3331
import lombok.Builder;
3432
import lombok.NonNull;
3533
import lombok.Value;
@@ -39,7 +37,6 @@
3937
* A combination of a {@link PublicKeyCredentialRequestOptions} and, optionally, a {@link #getUsername() username}.
4038
*/
4139
@Value
42-
@AllArgsConstructor(access = AccessLevel.PRIVATE)
4340
@Builder(toBuilder = true)
4441
public class AssertionRequest {
4542

@@ -58,23 +55,35 @@ public class AssertionRequest {
5855
* credential</a>, and identification of the user has been deferred until the response is received.
5956
* </p>
6057
*/
61-
@NonNull
62-
private final Optional<String> username;
58+
private final String username;
6359

6460
@JsonCreator
6561
private AssertionRequest(
6662
@NonNull @JsonProperty("publicKeyCredentialRequestOptions") PublicKeyCredentialRequestOptions publicKeyCredentialRequestOptions,
6763
@JsonProperty("username") String username
6864
) {
69-
this(publicKeyCredentialRequestOptions, Optional.ofNullable(username));
65+
this.publicKeyCredentialRequestOptions = publicKeyCredentialRequestOptions;
66+
this.username = username;
67+
}
68+
69+
/**
70+
* The username of the user to authenticate, if the user has already been identified.
71+
* <p>
72+
* If this is absent, this indicates that this is a request for an assertion by a <a
73+
* href="https://www.w3.org/TR/2019/PR-webauthn-20190117/#client-side-resident-public-key-credential-source">client-side-resident
74+
* credential</a>, and identification of the user has been deferred until the response is received.
75+
* </p>
76+
*/
77+
public Optional<String> getUsername() {
78+
return Optional.ofNullable(username);
7079
}
7180

7281
public static AssertionRequestBuilder.MandatoryStages builder() {
7382
return new AssertionRequestBuilder.MandatoryStages();
7483
}
7584

7685
public static class AssertionRequestBuilder {
77-
private Optional<String> username = Optional.empty();
86+
private String username = null;
7887

7988
public static class MandatoryStages {
8089
private final AssertionRequestBuilder builder = new AssertionRequestBuilder();
@@ -97,8 +106,7 @@ public AssertionRequestBuilder publicKeyCredentialRequestOptions(PublicKeyCreden
97106
* </p>
98107
*/
99108
public AssertionRequestBuilder username(@NonNull Optional<String> username) {
100-
this.username = username;
101-
return this;
109+
return this.username(username.orElse(null));
102110
}
103111

104112
/**
@@ -109,8 +117,9 @@ public AssertionRequestBuilder username(@NonNull Optional<String> username) {
109117
* credential</a>, and identification of the user has been deferred until the response is received.
110118
* </p>
111119
*/
112-
public AssertionRequestBuilder username(@NonNull String username) {
113-
return this.username(Optional.of(username));
120+
public AssertionRequestBuilder username(String username) {
121+
this.username = username;
122+
return this;
114123
}
115124
}
116125

webauthn-server-core/src/main/java/com/yubico/webauthn/FinishAssertionOptions.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,24 @@ public class FinishAssertionOptions {
6060
*
6161
* @see <a href="https://tools.ietf.org/html/rfc8471">The Token Binding Protocol Version 1.0</a>
6262
*/
63-
@NonNull
64-
private final Optional<ByteArray> callerTokenBindingId;
63+
private final ByteArray callerTokenBindingId;
64+
65+
/**
66+
* The <a href="https://tools.ietf.org/html/rfc8471#section-3.2">token binding ID</a> of the connection to the
67+
* client, if any.
68+
*
69+
* @see <a href="https://tools.ietf.org/html/rfc8471">The Token Binding Protocol Version 1.0</a>
70+
*/
71+
public Optional<ByteArray> getCallerTokenBindingId() {
72+
return Optional.ofNullable(callerTokenBindingId);
73+
}
6574

6675
public static FinishAssertionOptionsBuilder.MandatoryStages builder() {
6776
return new FinishAssertionOptionsBuilder.MandatoryStages();
6877
}
6978

7079
public static class FinishAssertionOptionsBuilder {
71-
private Optional<ByteArray> callerTokenBindingId = Optional.empty();
80+
private ByteArray callerTokenBindingId = null;
7281

7382
public static class MandatoryStages {
7483
private final FinishAssertionOptionsBuilder builder = new FinishAssertionOptionsBuilder();
@@ -92,7 +101,7 @@ public FinishAssertionOptionsBuilder response(PublicKeyCredential<AuthenticatorA
92101
* @see <a href="https://tools.ietf.org/html/rfc8471">The Token Binding Protocol Version 1.0</a>
93102
*/
94103
public FinishAssertionOptionsBuilder callerTokenBindingId(@NonNull Optional<ByteArray> callerTokenBindingId) {
95-
this.callerTokenBindingId = callerTokenBindingId;
104+
this.callerTokenBindingId = callerTokenBindingId.orElse(null);
96105
return this;
97106
}
98107

webauthn-server-core/src/main/java/com/yubico/webauthn/FinishRegistrationOptions.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,24 @@ public class FinishRegistrationOptions {
6161
*
6262
* @see <a href="https://tools.ietf.org/html/rfc8471">The Token Binding Protocol Version 1.0</a>
6363
*/
64-
@NonNull
65-
private final Optional<ByteArray> callerTokenBindingId;
64+
private final ByteArray callerTokenBindingId;
65+
66+
/**
67+
* The <a href="https://tools.ietf.org/html/rfc8471#section-3.2">token binding ID</a> of the connection to the
68+
* client, if any.
69+
*
70+
* @see <a href="https://tools.ietf.org/html/rfc8471">The Token Binding Protocol Version 1.0</a>
71+
*/
72+
public Optional<ByteArray> getCallerTokenBindingId() {
73+
return Optional.ofNullable(callerTokenBindingId);
74+
}
6675

6776
public static FinishRegistrationOptionsBuilder.MandatoryStages builder() {
6877
return new FinishRegistrationOptionsBuilder.MandatoryStages();
6978
}
7079

7180
public static class FinishRegistrationOptionsBuilder {
72-
private Optional<ByteArray> callerTokenBindingId = Optional.empty();
81+
private ByteArray callerTokenBindingId = null;
7382

7483
public static class MandatoryStages {
7584
private final FinishRegistrationOptionsBuilder builder = new FinishRegistrationOptionsBuilder();
@@ -93,7 +102,7 @@ public FinishRegistrationOptionsBuilder response(PublicKeyCredential<Authenticat
93102
* @see <a href="https://tools.ietf.org/html/rfc8471">The Token Binding Protocol Version 1.0</a>
94103
*/
95104
public FinishRegistrationOptionsBuilder callerTokenBindingId(@NonNull Optional<ByteArray> callerTokenBindingId) {
96-
this.callerTokenBindingId = callerTokenBindingId;
105+
this.callerTokenBindingId = callerTokenBindingId.orElse(null);
97106
return this;
98107
}
99108

webauthn-server-core/src/main/java/com/yubico/webauthn/RegistrationResult.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ public class RegistrationResult {
112112
* @see <a href="https://www.w3.org/TR/2019/PR-webauthn-20190117/#sctn-attestation">§6.4. Attestation</a>
113113
* @see com.yubico.webauthn.RelyingParty.RelyingPartyBuilder#metadataService(Optional)
114114
*/
115-
@NonNull
116115
@Builder.Default
117-
private final Optional<Attestation> attestationMetadata = Optional.empty();
116+
@Builder.ObtainVia(method = "getAttestationMetadata")
117+
private final Attestation attestationMetadata = null;
118118

119119
@JsonCreator
120120
private RegistrationResult(
@@ -123,7 +123,7 @@ private RegistrationResult(
123123
@NonNull @JsonProperty("attestationType") AttestationType attestationType,
124124
@NonNull @JsonProperty("publicKeyCose") ByteArray publicKeyCose,
125125
@NonNull @JsonProperty("warnings") List<String> warnings,
126-
@NonNull @JsonProperty("attestationMetadata") Optional<Attestation> attestationMetadata
126+
@JsonProperty("attestationMetadata") Attestation attestationMetadata
127127
) {
128128
this.keyId = keyId;
129129
this.attestationTrusted = attestationTrusted;
@@ -133,6 +133,10 @@ private RegistrationResult(
133133
this.attestationMetadata = attestationMetadata;
134134
}
135135

136+
public Optional<Attestation> getAttestationMetadata() {
137+
return Optional.ofNullable(attestationMetadata);
138+
}
139+
136140
static RegistrationResultBuilder.MandatoryStages builder() {
137141
return new RegistrationResultBuilder.MandatoryStages();
138142
}
@@ -166,6 +170,11 @@ public RegistrationResultBuilder publicKeyCose(ByteArray publicKeyCose) {
166170
}
167171
}
168172
}
173+
174+
public RegistrationResultBuilder attestationMetadata(@NonNull Optional<Attestation> attestationMetadata) {
175+
this.attestationMetadata = attestationMetadata.orElse(null);
176+
return this;
177+
}
169178
}
170179

171180
}

webauthn-server-core/src/main/java/com/yubico/webauthn/StartAssertionOptions.java

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public class StartAssertionOptions {
5353
* @see <a href="https://www.w3.org/TR/2019/PR-webauthn-20190117/#client-side-resident-public-key-credential-source">Client-side-resident
5454
* credential</a>
5555
*/
56-
@NonNull
57-
private final Optional<String> username;
56+
private final String username;
5857

5958
/**
6059
* Extension inputs for this authentication operation.
@@ -77,8 +76,7 @@ public class StartAssertionOptions {
7776
* The default is {@link UserVerificationRequirement#PREFERRED}.
7877
* </p>
7978
*/
80-
@NonNull
81-
private final Optional<UserVerificationRequirement> userVerification;
79+
private final UserVerificationRequirement userVerification;
8280

8381
/**
8482
* The value for {@link PublicKeyCredentialRequestOptions#getTimeout()} for this authentication operation.
@@ -91,13 +89,55 @@ public class StartAssertionOptions {
9189
* The default is empty.
9290
* </p>
9391
*/
94-
@NonNull
95-
private final Optional<Long> timeout;
92+
private final Long timeout;
93+
94+
/**
95+
* The username of the user to authenticate, if the user has already been identified.
96+
* <p>
97+
* If this is absent, that implies a first-factor authentication operation - meaning identification of the user is
98+
* deferred until after receiving the response from the client.
99+
* </p>
100+
*
101+
* <p>
102+
* The default is empty (absent).
103+
* </p>
104+
*
105+
* @see <a href="https://www.w3.org/TR/2019/PR-webauthn-20190117/#client-side-resident-public-key-credential-source">Client-side-resident
106+
* credential</a>
107+
*/
108+
public Optional<String> getUsername() {
109+
return Optional.ofNullable(username);
110+
}
111+
112+
/**
113+
* The value for {@link PublicKeyCredentialRequestOptions#getUserVerification()} for this authentication operation.
114+
* <p>
115+
* The default is {@link UserVerificationRequirement#PREFERRED}.
116+
* </p>
117+
*/
118+
public Optional<UserVerificationRequirement> getUserVerification() {
119+
return Optional.ofNullable(userVerification);
120+
}
121+
122+
/**
123+
* The value for {@link PublicKeyCredentialRequestOptions#getTimeout()} for this authentication operation.
124+
* <p>
125+
* This library does not take the timeout into account in any way, other than passing it through to the {@link
126+
* PublicKeyCredentialRequestOptions} so it can be used as an argument to
127+
* <code>navigator.credentials.get()</code> on the client side.
128+
* </p>
129+
* <p>
130+
* The default is empty.
131+
* </p>
132+
*/
133+
public Optional<Long> getTimeout() {
134+
return Optional.ofNullable(timeout);
135+
}
96136

97137
public static class StartAssertionOptionsBuilder {
98-
private @NonNull Optional<String> username = Optional.empty();
99-
private @NonNull Optional<UserVerificationRequirement> userVerification = Optional.empty();
100-
private @NonNull Optional<Long> timeout = Optional.empty();
138+
private String username = null;
139+
private UserVerificationRequirement userVerification = null;
140+
private Long timeout = null;
101141

102142
/**
103143
* The username of the user to authenticate, if the user has already been identified.
@@ -114,7 +154,7 @@ public static class StartAssertionOptionsBuilder {
114154
* credential</a>
115155
*/
116156
public StartAssertionOptionsBuilder username(@NonNull Optional<String> username) {
117-
this.username = username;
157+
this.username = username.orElse(null);
118158
return this;
119159
}
120160

@@ -143,7 +183,7 @@ public StartAssertionOptionsBuilder username(@NonNull String username) {
143183
* </p>
144184
*/
145185
public StartAssertionOptionsBuilder userVerification(@NonNull Optional<UserVerificationRequirement> userVerification) {
146-
this.userVerification = userVerification;
186+
this.userVerification = userVerification.orElse(null);
147187
return this;
148188
}
149189

@@ -172,7 +212,7 @@ public StartAssertionOptionsBuilder timeout(@NonNull Optional<Long> timeout) {
172212
if (timeout.isPresent() && timeout.get() <= 0) {
173213
throw new IllegalArgumentException("timeout must be positive, was: " + timeout.get());
174214
}
175-
this.timeout = timeout;
215+
this.timeout = timeout.orElse(null);
176216
return this;
177217
}
178218

webauthn-server-core/src/main/java/com/yubico/webauthn/StartRegistrationOptions.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public class StartRegistrationOptions {
4949
/**
5050
* Constraints on what kind of authenticator the user is allowed to use to create the credential.
5151
*/
52-
@NonNull
53-
private final Optional<AuthenticatorSelectionCriteria> authenticatorSelection;
52+
private final AuthenticatorSelectionCriteria authenticatorSelection;
5453

5554
/**
5655
* Extension inputs for this registration operation.
@@ -70,16 +69,37 @@ public class StartRegistrationOptions {
7069
* The default is empty.
7170
* </p>
7271
*/
73-
@NonNull
74-
private final Optional<Long> timeout;
72+
private final Long timeout;
73+
74+
/**
75+
* Constraints on what kind of authenticator the user is allowed to use to create the credential.
76+
*/
77+
public Optional<AuthenticatorSelectionCriteria> getAuthenticatorSelection() {
78+
return Optional.ofNullable(authenticatorSelection);
79+
}
80+
81+
/**
82+
* The value for {@link PublicKeyCredentialCreationOptions#getTimeout()} for this registration operation.
83+
* <p>
84+
* This library does not take the timeout into account in any way, other than passing it through to the {@link
85+
* PublicKeyCredentialCreationOptions} so it can be used as an argument to
86+
* <code>navigator.credentials.create()</code> on the client side.
87+
* </p>
88+
* <p>
89+
* The default is empty.
90+
* </p>
91+
*/
92+
public Optional<Long> getTimeout() {
93+
return Optional.ofNullable(timeout);
94+
}
7595

7696
public static StartRegistrationOptionsBuilder.MandatoryStages builder() {
7797
return new StartRegistrationOptionsBuilder.MandatoryStages();
7898
}
7999

80100
public static class StartRegistrationOptionsBuilder {
81-
private @NonNull Optional<AuthenticatorSelectionCriteria> authenticatorSelection = Optional.empty();
82-
private @NonNull Optional<Long> timeout = Optional.empty();
101+
private AuthenticatorSelectionCriteria authenticatorSelection = null;
102+
private Long timeout = null;
83103

84104
public static class MandatoryStages {
85105
private final StartRegistrationOptionsBuilder builder = new StartRegistrationOptionsBuilder();
@@ -93,15 +113,15 @@ public StartRegistrationOptionsBuilder user(UserIdentity user) {
93113
* Constraints on what kind of authenticator the user is allowed to use to create the credential.
94114
*/
95115
public StartRegistrationOptionsBuilder authenticatorSelection(@NonNull Optional<AuthenticatorSelectionCriteria> authenticatorSelection) {
96-
this.authenticatorSelection = authenticatorSelection;
97-
return this;
116+
return this.authenticatorSelection(authenticatorSelection.orElse(null));
98117
}
99118

100119
/**
101120
* Constraints on what kind of authenticator the user is allowed to use to create the credential.
102121
*/
103-
public StartRegistrationOptionsBuilder authenticatorSelection(@NonNull AuthenticatorSelectionCriteria authenticatorSelection) {
104-
return this.authenticatorSelection(Optional.of(authenticatorSelection));
122+
public StartRegistrationOptionsBuilder authenticatorSelection(AuthenticatorSelectionCriteria authenticatorSelection) {
123+
this.authenticatorSelection = authenticatorSelection;
124+
return this;
105125
}
106126

107127
/**
@@ -119,7 +139,7 @@ public StartRegistrationOptionsBuilder timeout(@NonNull Optional<Long> timeout)
119139
if (timeout.isPresent() && timeout.get() <= 0) {
120140
throw new IllegalArgumentException("timeout must be positive, was: " + timeout.get());
121141
}
122-
this.timeout = timeout;
142+
this.timeout = timeout.orElse(null);
123143
return this;
124144
}
125145

0 commit comments

Comments
 (0)