MCPcopy Create free account
hub / github.com/Treeki/BirdBridge / makeSignature

Function makeSignature

utils/oauth.ts:14–40  ·  view source on GitHub ↗
(hmacKey: CryptoKey, oauthParams: Record<string, string>, method: string, url: string|URL, params: URLSearchParams|null)

Source from the content-addressed store, hash-verified

12}
13
14async 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
42export class OAuth {
43 consumerKey: string;

Callers 1

requestMethod · 0.85

Calls 1

percentEncodeFunction · 0.85

Tested by

no test coverage detected