ddo / oauth-1.0a

OAuth 1.0a Request Authorization for Node and Browser
MIT License
325 stars 116 forks source link

Add authorize async function for asynchronous signature functions #107

Open macintoshhelper opened 3 years ago

macintoshhelper commented 3 years ago

Hi, I’m proposing adding a new authorizeAsync function, which helps for people who want to use the WebCrypto APIs, which are synchronous, i.e.

const hmacSha256Base64Digest = async (body, k) => {
  const secret = k;
  const enc = new TextEncoder('utf-8');
  const algorithm = { name: 'HMAC', hash: 'SHA-256' };

  const key = await crypto.subtle.importKey('raw', enc.encode(secret), algorithm, false, ['sign', 'verify']);
  const signature = await crypto.subtle.sign(algorithm.name, key, enc.encode(body));
  const digest = btoa(String.fromCharCode(...new Uint8Array(signature)));

  return digest;
}
 const oauthAuthorized = await oauth.authorizeAsync(request_data, { key: 'asdf', secret: 'qwerty' })

Ref #108

macintoshhelper commented 3 years ago

Apologies, just noticed that a PR exists for this already at #83 ; I feel that having both a sync, and async version of the authorize function may be a better approach though and minimise on the lines of code. I can't seem to see the error stack on the CI.