Add SubtleCryptoProvider and update Webhooks to allow async crypto. #1288
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Notify
r? @richardm-stripe
Summary
Introduce a new
CryptoProvider
which uses theSubtleCrypto
API. This is the main implementation for the Web Crypto API.This requires adding support for async HMAC computations in
CryptoProvider
and adding new async Webhook creation/verification functions which can use these.As a result, we can now construct and verify a webhook asynchronously by doing:
I opted to go down the route of having sync and async methods rather than parameterizing the CryptoProvider. We could have done something like
CryptoProvider<T>
and thenconstructEvent():T
. However our internal code would still need to check whether theT
was a promise or not. Instead this forces one to be explicit about whether you want to operate synchronously or asynchronously, and gives us an opportunity to error if trying to mix contexts.Motivation
We need this for Deno support (#997). Deno's
crypto
package doesn't implement HMAC and instead relies on the Web Crypto API, which is all async.Test Plan
Added tests throughout. Note that unfortunately the
SubtleCryptoProvider
can only be tested on Node 16, as Node only added aSubtleCrypto
shim in Node 15. All tests are shared between sync and async versinos.I've also set up a Deno template which processes webhooks asynchronously using this