Skip to content

Commit

Permalink
Use globally available web crypto API instead of nodeJS crypto module
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO committed Dec 8, 2023
1 parent 1c2e183 commit 6bfa241
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/WebhookReceiver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import crypto from 'crypto';
import { TokenVerifier } from './AccessToken.js';
import { WebhookEvent } from './proto/livekit_webhook_pb.js';

Expand Down Expand Up @@ -30,10 +29,15 @@ export class WebhookReceiver {
}
const claims = await this.verifier.verify(authHeader);
// confirm sha
const hash = crypto.createHash('sha256');
hash.update(body);
const encoder = new TextEncoder();
const hash = await crypto.subtle.digest('SHA-256', encoder.encode(body));
const hashDecoded = btoa(
Array.from(new Uint8Array(hash))
.map((v) => String.fromCharCode(v))
.join(''),
);

if (claims.sha256 !== hash.digest('base64')) {
if (claims.sha256 !== hashDecoded) {
throw new Error('sha256 checksum of body does not match');
}
}
Expand Down

0 comments on commit 6bfa241

Please sign in to comment.