( cookieHeader: string | string[] | undefined, cookieName: string )
| 62 | } |
| 63 | |
| 64 | export function extractCookieValues( |
| 65 | cookieHeader: string | string[] | undefined, |
| 66 | cookieName: string |
| 67 | ): string[] { |
| 68 | const rawCookieHeaders = Array.isArray(cookieHeader) |
| 69 | ? cookieHeader.filter((value): value is string => typeof value === "string") |
| 70 | : typeof cookieHeader === "string" |
| 71 | ? [cookieHeader] |
| 72 | : []; |
| 73 | |
| 74 | if (rawCookieHeaders.length === 0) { |
| 75 | return []; |
| 76 | } |
| 77 | |
| 78 | const tokens: string[] = []; |
| 79 | |
| 80 | for (const rawCookieHeader of rawCookieHeaders) { |
| 81 | if (rawCookieHeader.trim().length === 0) { |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | const cookiePairs = rawCookieHeader.split(";"); |
| 86 | for (const pair of cookiePairs) { |
| 87 | const separatorIndex = pair.indexOf("="); |
| 88 | if (separatorIndex <= 0) { |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | const key = pair.slice(0, separatorIndex).trim(); |
| 93 | if (key !== cookieName) { |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | const value = pair.slice(separatorIndex + 1).trim(); |
| 98 | if (value.length === 0) { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | try { |
| 103 | tokens.push(decodeURIComponent(value)); |
| 104 | } catch { |
| 105 | tokens.push(value); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return tokens; |
| 111 | } |
| 112 | |
| 113 | /** Create auth middleware that validates Authorization header or session cookie from context */ |
| 114 | export function createAuthMiddleware(authToken?: string) { |
no test coverage detected