Skip to content

Commit

Permalink
Implement to/fromJson methods in PublicKeyCredentialCreationOptions a…
Browse files Browse the repository at this point in the history
…nd AssertionRequest
  • Loading branch information
emlun committed Sep 28, 2021
1 parent 84a80f8 commit 0763da9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ New features:
`PublicKeyCredentialRequestOptions.toCredentialsGetJson()` and
`AssertionRequest.toCredentialsGetJson()` for serializing to JSON without
having to use Jackson directly.
* Added methods `PublicKeyCredentialCreationOptions.toJson()` and
`.fromJson(String)` suitable for encoding to and decoding from JSON.
* Added methods `AssertionRequest.toJson()` and `.fromJson(String)` suitable for
encoding to and decoding from JSON.

Fixes:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.yubico.internal.util.JacksonCodecs;
import com.yubico.webauthn.data.ByteArray;
import com.yubico.webauthn.data.PublicKeyCredentialRequestOptions;
import java.util.Optional;
Expand Down Expand Up @@ -101,12 +102,36 @@ public String toCredentialsGetJson() throws JsonProcessingException {
return publicKeyCredentialRequestOptions.toCredentialsGetJson();
}

/**
* Encode this {@link AssertionRequest} to JSON. The inverse of {@link #fromJson(String)}.
*
* <p>This method is suitable for encoding the {@link AssertionRequest} for temporary storage so
* that it can later be passed as an argument to {@link
* RelyingParty#finishAssertion(FinishAssertionOptions)}. The {@link #fromJson(String)} factory
* function is guaranteed to restore an identical {@link AssertionRequest} instance.
*
* <p>Note that encoding might not be needed if you can simply keep the {@link AssertionRequest}
* instance in server memory.
*
* @return this {@link AssertionRequest} encoded to JSON.
* @throws JsonProcessingException
*/
public String toJson() throws JsonProcessingException {
return "";
return JacksonCodecs.json().writeValueAsString(this);
}

/**
* Decode an {@link AssertionRequest} from JSON. The inverse of {@link #toJson()}.
*
* <p>If the JSON was generated by the {@link #toJson()} method, then {@link #fromJson(String)} in
* the same library version guarantees to restore an identical {@link AssertionRequest} instance.
* This is not guaranteed between different library versions.
*
* @return a {@link AssertionRequest} decoded from the input JSON.
* @throws JsonProcessingException
*/
public static AssertionRequest fromJson(String json) throws JsonProcessingException {
return null;
return JacksonCodecs.json().readValue(json, AssertionRequest.class);
}

public static AssertionRequestBuilder.MandatoryStages builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yubico.internal.util.CollectionUtil;
import com.yubico.internal.util.JacksonCodecs;
import com.yubico.webauthn.FinishRegistrationOptions;
import com.yubico.webauthn.RelyingParty;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -169,12 +171,41 @@ public String toCredentialsCreateJson() throws JsonProcessingException {
return json.writeValueAsString(result);
}

/**
* Encode this {@link PublicKeyCredentialCreationOptions} to JSON. The inverse of {@link
* #fromJson(String)}.
*
* <p>This method is suitable for encoding the {@link PublicKeyCredentialCreationOptions} for
* temporary storage so that it can later be passed as an argument to {@link
* RelyingParty#finishRegistration(FinishRegistrationOptions)}. The {@link #fromJson(String)}
* factory function is guaranteed to restore an identical {@link
* PublicKeyCredentialCreationOptions} instance.
*
* <p>Note that encoding might not be needed if you can simply keep the {@link
* PublicKeyCredentialCreationOptions} instance in server memory.
*
* @return this {@link PublicKeyCredentialCreationOptions} encoded to JSON.
* @throws JsonProcessingException
*/
public String toJson() throws JsonProcessingException {
return "";
return JacksonCodecs.json().writeValueAsString(this);
}

public static PublicKeyCredentialCreationOptions fromJson(String json) throws JsonProcessingException {
return null;
/**
* Decode an {@link PublicKeyCredentialCreationOptions} from JSON. The inverse of {@link
* #toJson()}.
*
* <p>If the JSON was generated by the {@link #toJson()} method, then {@link #fromJson(String)} in
* the same library version guarantees to restore an identical {@link
* PublicKeyCredentialCreationOptions} instance. This is not guaranteed between different library
* versions.
*
* @return a {@link PublicKeyCredentialCreationOptions} decoded from the input JSON.
* @throws JsonProcessingException
*/
public static PublicKeyCredentialCreationOptions fromJson(String json)
throws JsonProcessingException {
return JacksonCodecs.json().readValue(json, PublicKeyCredentialCreationOptions.class);
}

public Optional<Long> getTimeout() {
Expand Down

0 comments on commit 0763da9

Please sign in to comment.