|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -const encoded = process.env.GITHUB_AUTH_STATE; |
| 3 | +/** |
| 4 | + * Validates GitHub session auth from GITHUB_AUTH_STATE environment variable. |
| 5 | + * Can be run as CLI or imported as a module. |
| 6 | + */ |
4 | 7 |
|
5 | | -if (!encoded) { |
6 | | - console.error('GITHUB_AUTH_STATE environment variable is not set'); |
7 | | - process.exit(1); |
| 8 | +/** |
| 9 | + * Decodes and validates GITHUB_AUTH_STATE, returns the storage state or null. |
| 10 | + * @param {string} [encoded] - Base64-encoded auth state (defaults to env var) |
| 11 | + * @returns {object|null} - Playwright storageState object, or null if invalid |
| 12 | + */ |
| 13 | +export function getGitHubAuth(encoded = process.env.GITHUB_AUTH_STATE) { |
| 14 | + if (!encoded) return null; |
| 15 | + try { |
| 16 | + const decoded = JSON.parse(Buffer.from(encoded, 'base64').toString('utf-8')); |
| 17 | + const githubCookies = decoded.cookies?.filter(c => c.domain?.includes('github')) || []; |
| 18 | + if (githubCookies.length === 0) return null; |
| 19 | + |
| 20 | + // Check for expired cookies (expires > 0 means it has expiry, not a session cookie) |
| 21 | + const expiredCount = githubCookies.filter(c => c.expires > 0 && c.expires * 1000 < Date.now()).length; |
| 22 | + if (expiredCount > 0) return null; |
| 23 | + |
| 24 | + return decoded; |
| 25 | + } catch { |
| 26 | + return null; |
| 27 | + } |
8 | 28 | } |
9 | 29 |
|
10 | | -try { |
11 | | - const decoded = JSON.parse(Buffer.from(encoded, 'base64').toString('utf-8')); |
| 30 | +/** |
| 31 | + * Returns a descriptive reason why session auth is unavailable, or null if valid. |
| 32 | + * @param {string} [encoded] - Base64-encoded auth state (defaults to env var) |
| 33 | + * @returns {string|null} - Error message, or null if auth is valid |
| 34 | + */ |
| 35 | +export function getSessionSkipReason(encoded = process.env.GITHUB_AUTH_STATE) { |
| 36 | + if (!encoded) { |
| 37 | + return 'GITHUB_AUTH_STATE not set. Run: node scripts/obtain-github-authorization.js'; |
| 38 | + } |
| 39 | + try { |
| 40 | + const decoded = JSON.parse(Buffer.from(encoded, 'base64').toString('utf-8')); |
| 41 | + const githubCookies = decoded.cookies?.filter(c => c.domain?.includes('github')) || []; |
| 42 | + if (githubCookies.length === 0) { |
| 43 | + return 'GITHUB_AUTH_STATE has no GitHub cookies'; |
| 44 | + } |
| 45 | + const expiredCount = githubCookies.filter(c => c.expires > 0 && c.expires * 1000 < Date.now()).length; |
| 46 | + if (expiredCount > 0) { |
| 47 | + return `GITHUB_AUTH_STATE has ${expiredCount} expired cookie(s). Run: node scripts/obtain-github-authorization.js`; |
| 48 | + } |
| 49 | + return null; // Auth is valid |
| 50 | + } catch (e) { |
| 51 | + return `GITHUB_AUTH_STATE decode failed: ${e.message}`; |
| 52 | + } |
| 53 | +} |
12 | 54 |
|
13 | | - console.log('Cookies:', decoded.cookies?.length || 0); |
14 | | - console.log('Origins:', decoded.origins?.length || 0); |
15 | | - console.log(); |
| 55 | +/** |
| 56 | + * Validates GitHub PAT by making an API call. Returns skip reason or null if valid. |
| 57 | + * @param {string} [pat] - GitHub PAT (defaults to env var) |
| 58 | + * @returns {Promise<string|null>} - Error message, or null if PAT is valid |
| 59 | + */ |
| 60 | +export async function getPatSkipReason(pat = process.env.API_CONTRACT_TEST_PAT) { |
| 61 | + if (!pat) { |
| 62 | + return 'API_CONTRACT_TEST_PAT not set'; |
| 63 | + } |
| 64 | + try { |
| 65 | + const response = await fetch('https://api.github.com/rate_limit', { |
| 66 | + headers: { Authorization: `Bearer ${pat}` }, |
| 67 | + }); |
| 68 | + if (response.status === 401) { |
| 69 | + return 'API_CONTRACT_TEST_PAT is expired or invalid (HTTP 401)'; |
| 70 | + } |
| 71 | + if (!response.ok) { |
| 72 | + return `API_CONTRACT_TEST_PAT validation failed (HTTP ${response.status})`; |
| 73 | + } |
| 74 | + return null; // PAT is valid |
| 75 | + } catch (e) { |
| 76 | + return `API_CONTRACT_TEST_PAT validation error: ${e.message}`; |
| 77 | + } |
| 78 | +} |
16 | 79 |
|
17 | | - const githubCookies = decoded.cookies?.filter(c => c.domain?.includes('github')) || []; |
| 80 | +// CLI behavior when run directly |
| 81 | +const isMain = process.argv[1]?.endsWith('validate-github-authorization.js'); |
| 82 | +if (isMain) { |
| 83 | + const encoded = process.env.GITHUB_AUTH_STATE; |
18 | 84 |
|
19 | | - if (githubCookies.length === 0) { |
20 | | - console.error('No GitHub cookies found - auth state may be invalid'); |
| 85 | + if (!encoded) { |
| 86 | + console.error('GITHUB_AUTH_STATE environment variable is not set'); |
21 | 87 | process.exit(1); |
22 | 88 | } |
23 | 89 |
|
24 | | - console.log('GitHub cookies:'); |
25 | | - githubCookies.forEach(c => { |
26 | | - // expires <= 0 means session cookie, not expired |
27 | | - const isSession = !c.expires || c.expires <= 0; |
28 | | - const expires = isSession ? 'session' : new Date(c.expires * 1000).toISOString(); |
29 | | - const expired = !isSession && c.expires * 1000 < Date.now() ? ' (EXPIRED)' : ''; |
30 | | - console.log(` ${c.name} ${c.domain} ${expires}${expired}`); |
31 | | - }); |
32 | | - |
33 | | - const expiredCount = githubCookies.filter(c => c.expires > 0 && c.expires * 1000 < Date.now()).length; |
34 | | - if (expiredCount > 0) { |
| 90 | + try { |
| 91 | + const decoded = JSON.parse(Buffer.from(encoded, 'base64').toString('utf-8')); |
| 92 | + |
| 93 | + console.log('Cookies:', decoded.cookies?.length || 0); |
| 94 | + console.log('Origins:', decoded.origins?.length || 0); |
| 95 | + console.log(); |
| 96 | + |
| 97 | + const githubCookies = decoded.cookies?.filter(c => c.domain?.includes('github')) || []; |
| 98 | + |
| 99 | + if (githubCookies.length === 0) { |
| 100 | + console.error('No GitHub cookies found - auth state may be invalid'); |
| 101 | + process.exit(1); |
| 102 | + } |
| 103 | + |
| 104 | + console.log('GitHub cookies:'); |
| 105 | + githubCookies.forEach(c => { |
| 106 | + // expires <= 0 means session cookie, not expired |
| 107 | + const isSession = !c.expires || c.expires <= 0; |
| 108 | + const expires = isSession ? 'session' : new Date(c.expires * 1000).toISOString(); |
| 109 | + const expired = !isSession && c.expires * 1000 < Date.now() ? ' (EXPIRED)' : ''; |
| 110 | + console.log(` ${c.name} ${c.domain} ${expires}${expired}`); |
| 111 | + }); |
| 112 | + |
| 113 | + const expiredCount = githubCookies.filter(c => c.expires > 0 && c.expires * 1000 < Date.now()).length; |
| 114 | + if (expiredCount > 0) { |
| 115 | + console.log(); |
| 116 | + console.error(`${expiredCount} cookie(s) expired - regenerate with: node scripts/obtain-github-authorization.js`); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | + |
35 | 120 | console.log(); |
36 | | - console.error(`${expiredCount} cookie(s) expired - regenerate with: node scripts/obtain-github-authorization.js`); |
| 121 | + console.log('Auth state appears valid'); |
| 122 | + } catch (e) { |
| 123 | + console.error('Failed to decode GITHUB_AUTH_STATE:', e.message); |
37 | 124 | process.exit(1); |
38 | 125 | } |
39 | | - |
40 | | - console.log(); |
41 | | - console.log('Auth state appears valid'); |
42 | | -} catch (e) { |
43 | | - console.error('Failed to decode GITHUB_AUTH_STATE:', e.message); |
44 | | - process.exit(1); |
45 | 126 | } |
0 commit comments