(hmacKey: CryptoKey, oauthParams: Record<string, string>, method: string, url: string|URL, params: URLSearchParams|null)
| 12 | } |
| 13 | |
| 14 | async function makeSignature(hmacKey: CryptoKey, oauthParams: Record<string, string>, method: string, url: string|URL, params: URLSearchParams|null): Promise<string> { |
| 15 | const paramBits = []; |
| 16 | |
| 17 | for (const [key, value] of Object.entries(oauthParams)) { |
| 18 | paramBits.push(`${percentEncode(key)}=${percentEncode(value)}`); |
| 19 | } |
| 20 | |
| 21 | if (typeof url === 'string') { |
| 22 | url = new URL(url); |
| 23 | } |
| 24 | url.searchParams.forEach((value, key) => { |
| 25 | paramBits.push(`${percentEncode(key)}=${percentEncode(value)}`); |
| 26 | }); |
| 27 | |
| 28 | if (params !== null) { |
| 29 | for (const [key, value] of Object.entries(params)) { |
| 30 | paramBits.push(`${percentEncode(key)}=${percentEncode(value)}`); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | const parameterString = percentEncode(paramBits.sort().join('&')); |
| 35 | const urlBase = percentEncode(url.origin + url.pathname); |
| 36 | const baseString = `${method.toUpperCase()}&${urlBase}&${parameterString}`; |
| 37 | const baseArray = new TextEncoder().encode(baseString); |
| 38 | const hash = await crypto.subtle.sign('HMAC', hmacKey, baseArray.buffer); |
| 39 | return toHashString(hash, 'base64'); |
| 40 | } |
| 41 | |
| 42 | export class OAuth { |
| 43 | consumerKey: string; |
no test coverage detected