Skip to content

Commit 0763da9

Browse files
committed
Implement to/fromJson methods in PublicKeyCredentialCreationOptions and AssertionRequest
1 parent 84a80f8 commit 0763da9

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ New features:
3939
`PublicKeyCredentialRequestOptions.toCredentialsGetJson()` and
4040
`AssertionRequest.toCredentialsGetJson()` for serializing to JSON without
4141
having to use Jackson directly.
42+
* Added methods `PublicKeyCredentialCreationOptions.toJson()` and
43+
`.fromJson(String)` suitable for encoding to and decoding from JSON.
44+
* Added methods `AssertionRequest.toJson()` and `.fromJson(String)` suitable for
45+
encoding to and decoding from JSON.
4246

4347
Fixes:
4448

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.fasterxml.jackson.annotation.JsonCreator;
2828
import com.fasterxml.jackson.annotation.JsonProperty;
2929
import com.fasterxml.jackson.core.JsonProcessingException;
30+
import com.yubico.internal.util.JacksonCodecs;
3031
import com.yubico.webauthn.data.ByteArray;
3132
import com.yubico.webauthn.data.PublicKeyCredentialRequestOptions;
3233
import java.util.Optional;
@@ -101,12 +102,36 @@ public String toCredentialsGetJson() throws JsonProcessingException {
101102
return publicKeyCredentialRequestOptions.toCredentialsGetJson();
102103
}
103104

105+
/**
106+
* Encode this {@link AssertionRequest} to JSON. The inverse of {@link #fromJson(String)}.
107+
*
108+
* <p>This method is suitable for encoding the {@link AssertionRequest} for temporary storage so
109+
* that it can later be passed as an argument to {@link
110+
* RelyingParty#finishAssertion(FinishAssertionOptions)}. The {@link #fromJson(String)} factory
111+
* function is guaranteed to restore an identical {@link AssertionRequest} instance.
112+
*
113+
* <p>Note that encoding might not be needed if you can simply keep the {@link AssertionRequest}
114+
* instance in server memory.
115+
*
116+
* @return this {@link AssertionRequest} encoded to JSON.
117+
* @throws JsonProcessingException
118+
*/
104119
public String toJson() throws JsonProcessingException {
105-
return "";
120+
return JacksonCodecs.json().writeValueAsString(this);
106121
}
107122

123+
/**
124+
* Decode an {@link AssertionRequest} from JSON. The inverse of {@link #toJson()}.
125+
*
126+
* <p>If the JSON was generated by the {@link #toJson()} method, then {@link #fromJson(String)} in
127+
* the same library version guarantees to restore an identical {@link AssertionRequest} instance.
128+
* This is not guaranteed between different library versions.
129+
*
130+
* @return a {@link AssertionRequest} decoded from the input JSON.
131+
* @throws JsonProcessingException
132+
*/
108133
public static AssertionRequest fromJson(String json) throws JsonProcessingException {
109-
return null;
134+
return JacksonCodecs.json().readValue(json, AssertionRequest.class);
110135
}
111136

112137
public static AssertionRequestBuilder.MandatoryStages builder() {

webauthn-server-core/src/main/java/com/yubico/webauthn/data/PublicKeyCredentialCreationOptions.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.fasterxml.jackson.databind.node.ObjectNode;
3232
import com.yubico.internal.util.CollectionUtil;
3333
import com.yubico.internal.util.JacksonCodecs;
34+
import com.yubico.webauthn.FinishRegistrationOptions;
35+
import com.yubico.webauthn.RelyingParty;
3436
import java.util.List;
3537
import java.util.Optional;
3638
import java.util.Set;
@@ -169,12 +171,41 @@ public String toCredentialsCreateJson() throws JsonProcessingException {
169171
return json.writeValueAsString(result);
170172
}
171173

174+
/**
175+
* Encode this {@link PublicKeyCredentialCreationOptions} to JSON. The inverse of {@link
176+
* #fromJson(String)}.
177+
*
178+
* <p>This method is suitable for encoding the {@link PublicKeyCredentialCreationOptions} for
179+
* temporary storage so that it can later be passed as an argument to {@link
180+
* RelyingParty#finishRegistration(FinishRegistrationOptions)}. The {@link #fromJson(String)}
181+
* factory function is guaranteed to restore an identical {@link
182+
* PublicKeyCredentialCreationOptions} instance.
183+
*
184+
* <p>Note that encoding might not be needed if you can simply keep the {@link
185+
* PublicKeyCredentialCreationOptions} instance in server memory.
186+
*
187+
* @return this {@link PublicKeyCredentialCreationOptions} encoded to JSON.
188+
* @throws JsonProcessingException
189+
*/
172190
public String toJson() throws JsonProcessingException {
173-
return "";
191+
return JacksonCodecs.json().writeValueAsString(this);
174192
}
175193

176-
public static PublicKeyCredentialCreationOptions fromJson(String json) throws JsonProcessingException {
177-
return null;
194+
/**
195+
* Decode an {@link PublicKeyCredentialCreationOptions} from JSON. The inverse of {@link
196+
* #toJson()}.
197+
*
198+
* <p>If the JSON was generated by the {@link #toJson()} method, then {@link #fromJson(String)} in
199+
* the same library version guarantees to restore an identical {@link
200+
* PublicKeyCredentialCreationOptions} instance. This is not guaranteed between different library
201+
* versions.
202+
*
203+
* @return a {@link PublicKeyCredentialCreationOptions} decoded from the input JSON.
204+
* @throws JsonProcessingException
205+
*/
206+
public static PublicKeyCredentialCreationOptions fromJson(String json)
207+
throws JsonProcessingException {
208+
return JacksonCodecs.json().readValue(json, PublicKeyCredentialCreationOptions.class);
178209
}
179210

180211
public Optional<Long> getTimeout() {

0 commit comments

Comments
 (0)