use of com.yubico.webauthn.data.exception.Base64UrlException in project java-webauthn-server by Yubico.
the class FidoMetadataDownloader method retrieveBlob.
/**
* @throws Base64UrlException if the metadata BLOB is not a well-formed JWT in compact
* serialization.
* @throws CertPathValidatorException if the downloaded or explicitly configured BLOB fails
* certificate path validation.
* @throws CertificateException if the BLOB signing certificate chain fails to parse.
* @throws IOException if any of the following fails: downloading the BLOB, reading or writing the
* cache file (if any), or parsing the BLOB contents.
* @throws InvalidAlgorithmParameterException if certificate path validation fails.
* @throws InvalidKeyException if signature verification fails.
* @throws UnexpectedLegalHeader if the downloaded BLOB (if any) contains a <code>"legalHeader"
* </code> value not configured in {@link
* FidoMetadataDownloaderBuilder.Step1#expectLegalHeader(String...)
* expectLegalHeader(String...)} but is otherwise valid. The downloaded BLOB will not be
* written to cache in this case.
* @throws NoSuchAlgorithmException if signature verification fails.
* @throws SignatureException if signature verification fails.
* @throws FidoMetadataDownloaderException if the explicitly configured BLOB (if any) has a bad
* signature.
*/
private MetadataBLOB retrieveBlob(X509Certificate trustRootCertificate) throws Base64UrlException, CertPathValidatorException, CertificateException, IOException, InvalidAlgorithmParameterException, InvalidKeyException, UnexpectedLegalHeader, NoSuchAlgorithmException, SignatureException, FidoMetadataDownloaderException {
if (blobJwt != null) {
return parseAndVerifyBlob(new ByteArray(blobJwt.getBytes(StandardCharsets.UTF_8)), trustRootCertificate);
} else {
final Optional<ByteArray> cachedContents;
if (blobCacheFile != null) {
cachedContents = readCacheFile(blobCacheFile);
} else {
cachedContents = blobCacheSupplier.get();
}
final MetadataBLOB cachedBlob = cachedContents.map(cached -> {
try {
return parseAndVerifyBlob(cached, trustRootCertificate);
} catch (Exception e) {
return null;
}
}).orElse(null);
if (cachedBlob != null && cachedBlob.getPayload().getNextUpdate().atStartOfDay().atZone(clock.getZone()).isAfter(clock.instant().atZone(clock.getZone()))) {
return cachedBlob;
} else {
final ByteArray downloaded = download(blobUrl);
try {
final MetadataBLOB downloadedBlob = parseAndVerifyBlob(downloaded, trustRootCertificate);
if (cachedBlob == null || downloadedBlob.getPayload().getNo() > cachedBlob.getPayload().getNo()) {
if (expectedLegalHeaders.contains(downloadedBlob.getPayload().getLegalHeader())) {
if (blobCacheFile != null) {
new FileOutputStream(blobCacheFile).write(downloaded.getBytes());
}
if (blobCacheConsumer != null) {
blobCacheConsumer.accept(downloaded);
}
return downloadedBlob;
} else {
throw new UnexpectedLegalHeader(cachedBlob, downloadedBlob);
}
} else {
return cachedBlob;
}
} catch (FidoMetadataDownloaderException e) {
if (e.getReason() == FidoMetadataDownloaderException.Reason.BAD_SIGNATURE && cachedBlob != null) {
return cachedBlob;
} else {
throw e;
}
}
}
}
}
use of com.yubico.webauthn.data.exception.Base64UrlException in project java-webauthn-server by Yubico.
the class WebAuthnRestResource method deregisterCredential.
@Path("action/deregister")
@POST
public Response deregisterCredential(@NonNull @FormParam("sessionToken") String sessionTokenBase64, @NonNull @FormParam("credentialId") String credentialIdBase64) throws MalformedURLException, Base64UrlException {
logger.trace("deregisterCredential sesion: {}, credentialId: {}", sessionTokenBase64, credentialIdBase64);
final ByteArray credentialId;
try {
credentialId = ByteArray.fromBase64Url(credentialIdBase64);
} catch (Base64UrlException e) {
return messagesJson(Response.status(Status.BAD_REQUEST), "Credential ID is not valid Base64Url data: " + credentialIdBase64);
}
Either<List<String>, DeregisterCredentialResult> result = server.deregisterCredential(ByteArray.fromBase64Url(sessionTokenBase64), credentialId);
if (result.isRight()) {
return finishResponse(result, "Failed to deregister credential; further error message(s) were unfortunately lost to an internal server error.", "deregisterCredential", "");
} else {
return messagesJson(Response.status(Status.BAD_REQUEST), result.left().get());
}
}
Aggregations