ltonetwork / http-message-signatures

Implementation of the IETF HTTP Message Signatures draft standard
ISC License
3 stars 1 forks source link

Update docs: Verifying in the Browser #1

Open timkelty opened 11 months ago

timkelty commented 11 months ago

I'm trying to use this lib to verify a signature.

Following the "Verifying in the Browser" docs, I've found some issues:

I've tried to work through some of these things, and have TS happy, but still can't seem to get things verifying.

export async function verifyHmac(data: string, signature: Uint8Array, params: Parameters) {
    const keyData = new TextEncoder().encode('123456789');
    const algorithm = { name: 'HMAC', hash: 'SHA-256' };
    const key = await crypto.subtle.importKey('raw', keyData, algorithm, false, ['verify']);
    const encodedData = new TextEncoder().encode(data);
    const valid = await crypto.subtle.verify('HMAC', key, signature, encodedData);

    if (!valid) {
        throw new Error('Invalid signature');
    }

    return true;
}

export default {
    async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
                try {
                    verify(request, verifyHmac);
                } catch(e: any) {
                    return new Response(e.message, {
                        status: 403,
                    });
                }
        }
}
jasny commented 2 months ago

Updated docs. All code examples need to be tested with the latest version.