mirror of
https://github.com/versia-pub/api.git
synced 2025-12-06 08:28:19 +01:00
fix(federation): ♻️ Don't use Node-specific API Buffer
This commit is contained in:
parent
2dd8615db3
commit
62e5724a1c
|
|
@ -10,6 +10,12 @@ type HttpVerb =
|
||||||
| "OPTIONS"
|
| "OPTIONS"
|
||||||
| "HEAD";
|
| "HEAD";
|
||||||
|
|
||||||
|
const base64ToArrayBuffer = (base64: string) =>
|
||||||
|
Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
||||||
|
|
||||||
|
const arrayBufferToBase64 = (arrayBuffer: ArrayBuffer) =>
|
||||||
|
btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
||||||
|
|
||||||
const checkEvironmentSupport = () => {
|
const checkEvironmentSupport = () => {
|
||||||
// Check if WebCrypto is supported
|
// Check if WebCrypto is supported
|
||||||
if (!globalThis.crypto?.subtle) {
|
if (!globalThis.crypto?.subtle) {
|
||||||
|
|
@ -47,7 +53,7 @@ export class SignatureValidator {
|
||||||
return new SignatureValidator(
|
return new SignatureValidator(
|
||||||
await crypto.subtle.importKey(
|
await crypto.subtle.importKey(
|
||||||
"spki",
|
"spki",
|
||||||
Buffer.from(base64PublicKey, "base64"),
|
base64ToArrayBuffer(base64PublicKey),
|
||||||
"Ed25519",
|
"Ed25519",
|
||||||
false,
|
false,
|
||||||
["verify"],
|
["verify"],
|
||||||
|
|
@ -165,15 +171,13 @@ export class SignatureValidator {
|
||||||
`(request-target): ${method.toLowerCase()} ${url.pathname}\n` +
|
`(request-target): ${method.toLowerCase()} ${url.pathname}\n` +
|
||||||
`host: ${url.host}\n` +
|
`host: ${url.host}\n` +
|
||||||
`date: ${date.toISOString()}\n` +
|
`date: ${date.toISOString()}\n` +
|
||||||
`digest: SHA-256=${Buffer.from(new Uint8Array(digest)).toString(
|
`digest: SHA-256=${arrayBufferToBase64(digest)}\n`;
|
||||||
"base64",
|
|
||||||
)}\n`;
|
|
||||||
|
|
||||||
// Check if signed string is valid
|
// Check if signed string is valid
|
||||||
const isValid = await crypto.subtle.verify(
|
const isValid = await crypto.subtle.verify(
|
||||||
"Ed25519",
|
"Ed25519",
|
||||||
this.publicKey,
|
this.publicKey,
|
||||||
Buffer.from(signature, "base64"),
|
base64ToArrayBuffer(signature),
|
||||||
new TextEncoder().encode(expectedSignedString),
|
new TextEncoder().encode(expectedSignedString),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -219,7 +223,7 @@ export class SignatureConstructor {
|
||||||
return new SignatureConstructor(
|
return new SignatureConstructor(
|
||||||
await crypto.subtle.importKey(
|
await crypto.subtle.importKey(
|
||||||
"pkcs8",
|
"pkcs8",
|
||||||
Buffer.from(base64PrivateKey, "base64"),
|
base64ToArrayBuffer(base64PrivateKey),
|
||||||
"Ed25519",
|
"Ed25519",
|
||||||
false,
|
false,
|
||||||
["sign"],
|
["sign"],
|
||||||
|
|
@ -321,7 +325,7 @@ export class SignatureConstructor {
|
||||||
}\n` +
|
}\n` +
|
||||||
`host: ${url.host}\n` +
|
`host: ${url.host}\n` +
|
||||||
`date: ${finalDate}\n` +
|
`date: ${finalDate}\n` +
|
||||||
`digest: SHA-256=${Buffer.from(digest).toString("base64")}\n`;
|
`digest: SHA-256=${arrayBufferToBase64(digest)}\n`;
|
||||||
|
|
||||||
const signature = await crypto.subtle.sign(
|
const signature = await crypto.subtle.sign(
|
||||||
"Ed25519",
|
"Ed25519",
|
||||||
|
|
@ -329,9 +333,7 @@ export class SignatureConstructor {
|
||||||
new TextEncoder().encode(signedString),
|
new TextEncoder().encode(signedString),
|
||||||
);
|
);
|
||||||
|
|
||||||
const signatureBase64 = Buffer.from(new Uint8Array(signature)).toString(
|
const signatureBase64 = arrayBufferToBase64(signature);
|
||||||
"base64",
|
|
||||||
);
|
|
||||||
|
|
||||||
headers.set("Date", finalDate);
|
headers.set("Date", finalDate);
|
||||||
headers.set(
|
headers.set(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue