All files / app/util jwt.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173x 7x       7x 7x 7x 7x 7x 7x       110x    
export async function signJwt(payload: any, secret: BufferSource) {
  const header = {
    alg: 'HS256',
    typ: 'JWT'
  };
  const body = btoa(JSON.stringify(header)) + '.' + btoa(JSON.stringify(payload));
  const algorithm = { name: 'HMAC', hash: 'SHA-256' };
  const key = await crypto.subtle.importKey('raw', secret, algorithm, false, ['sign']);
  const signature = await crypto.subtle.sign(algorithm.name, key, new TextEncoder().encode(body));
  const digest = btoa(String.fromCharCode(...new Uint8Array(signature)));
  return (body + '.' + digest).replace(/\+/g, '-').replace(/\//g, '_');
}
 
export function base64Bytes(base64: string) {
  return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
}