|
26 | 26 |
|
27 | 27 | import com.fasterxml.jackson.annotation.JsonCreator;
|
28 | 28 | import com.fasterxml.jackson.annotation.JsonProperty;
|
| 29 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 30 | +import com.yubico.internal.util.WebAuthnCodecs; |
| 31 | +import java.io.IOException; |
29 | 32 | import lombok.AllArgsConstructor;
|
30 | 33 | import lombok.Builder;
|
31 | 34 | import lombok.NonNull;
|
@@ -126,4 +129,80 @@ public PublicKeyCredentialBuilder<A, B> clientExtensionResults(B clientExtension
|
126 | 129 |
|
127 | 130 | }
|
128 | 131 |
|
| 132 | + /** |
| 133 | + * Parse a {@link PublicKeyCredential} object from JSON. |
| 134 | + * |
| 135 | + * <p>The <code>json</code> should be of the following format:</p> |
| 136 | + * |
| 137 | + * <pre> |
| 138 | + * { |
| 139 | + * "id": "(resp.id)", |
| 140 | + * "response": { |
| 141 | + * "attestationObject": "(Base64Url encoded resp.attestationObject)", |
| 142 | + * "clientDataJSON": "(Base64Url encoded resp.clientDataJSON)" |
| 143 | + * }, |
| 144 | + * "clientExtensionResults": { (resp.getClientExtensionResults()) }, |
| 145 | + * "type": "public-key" |
| 146 | + * } |
| 147 | + * </pre> |
| 148 | + * |
| 149 | + * <dl> |
| 150 | + * <dt>resp:</dt><dd>The <a href="https://www.w3.org/TR/webauthn-1/#iface-pkcredential">PublicKeyCredential</a> object returned from a registration ceremony.</dd> |
| 151 | + * <dt>id:</dt><dd>The string value of <code>resp.id</code></dd> |
| 152 | + * <dt>response.attestationObject:</dt><dd>The value of <code>resp.attestationObject</code>, Base64Url encoded as a string</dd> |
| 153 | + * <dt>response.clientDataJSON:</dt><dd>The value of <code>resp.clientDataJSON</code>, Base64Url encoded as a string</dd> |
| 154 | + * <dt>clientExtensionResults:</dt><dd>The return value of <code>resp.getClientExtensionResults()</code></dd> |
| 155 | + * <dt>type:</dt><dd>The literal string value <code>"public-key"</code></dd> |
| 156 | + * </dl> |
| 157 | + * |
| 158 | + * @param json a JSON string of the above format |
| 159 | + * @throws IOException if the <code>json</code> is invalid or cannot be decoded as a {@link PublicKeyCredential} |
| 160 | + */ |
| 161 | + public static PublicKeyCredential<AuthenticatorAttestationResponse, ClientRegistrationExtensionOutputs> parseRegistrationResponseJson(String json) throws IOException { |
| 162 | + return WebAuthnCodecs.json().readValue( |
| 163 | + json, |
| 164 | + new TypeReference<PublicKeyCredential<AuthenticatorAttestationResponse, ClientRegistrationExtensionOutputs>>(){} |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Parse a {@link PublicKeyCredential} object from JSON. |
| 170 | + * |
| 171 | + * <p>The <code>json</code> should be of the following format:</p> |
| 172 | + * |
| 173 | + * <pre> |
| 174 | + * { |
| 175 | + * "id": "(resp.id)", |
| 176 | + * "response": { |
| 177 | + * "authenticatorData": "(Base64Url encoded resp.authenticatorData)", |
| 178 | + * "signature": "(Base64Url encoded resp.signature)", |
| 179 | + * "clientDataJSON": "(Base64Url encoded resp.clientDataJSON)", |
| 180 | + * "userHandle": "(null, undefined or Base64Url encoded resp.userHandle)" |
| 181 | + * }, |
| 182 | + * "clientExtensionResults": { (resp.getClientExtensionResults()) }, |
| 183 | + * "type": "public-key" |
| 184 | + * } |
| 185 | + * </pre> |
| 186 | + * |
| 187 | + * <dl> |
| 188 | + * <dt>resp:</dt><dd>The <a href="https://www.w3.org/TR/webauthn-1/#iface-pkcredential">PublicKeyCredential</a> object returned from an authentication ceremony.</dd> |
| 189 | + * <dt>id:</dt><dd>The string value of <code>resp.id</code></dd> |
| 190 | + * <dt>response.authenticatorData:</dt><dd>The value of <code>resp.authenticatorData</code>, Base64Url encoded as a string</dd> |
| 191 | + * <dt>response.signature:</dt><dd>The value of <code>resp.signature</code>, Base64Url encoded as a string</dd> |
| 192 | + * <dt>response.clientDataJSON:</dt><dd>The value of <code>resp.clientDataJSON</code>, Base64Url encoded as a string</dd> |
| 193 | + * <dt>response.userHandle:</dt><dd>The value of <code>resp.userHandle</code> Base64Url encoded as a string if present, otherwise <code>null</code> or <code>undefined</code></dd> |
| 194 | + * <dt>clientExtensionResults:</dt><dd>The return value of <code>resp.getClientExtensionResults()</code></dd> |
| 195 | + * <dt>type:</dt><dd>The literal string value <code>"public-key"</code></dd> |
| 196 | + * </dl> |
| 197 | + * |
| 198 | + * @param json a JSON string of the above format |
| 199 | + * @throws IOException if the <code>json</code> is invalid or cannot be decoded as a {@link PublicKeyCredential} |
| 200 | + */ |
| 201 | + public static PublicKeyCredential<AuthenticatorAssertionResponse, ClientAssertionExtensionOutputs> parseAssertionResponseJson(String json) throws IOException { |
| 202 | + return WebAuthnCodecs.json().readValue( |
| 203 | + json, |
| 204 | + new TypeReference<PublicKeyCredential<AuthenticatorAssertionResponse, ClientAssertionExtensionOutputs>>(){} |
| 205 | + ); |
| 206 | + } |
| 207 | + |
129 | 208 | }
|
0 commit comments