|
| 1 | +/* |
| 2 | + * Minimal script to acquire an IAP OIDC ID token and call the API. |
| 3 | + * Reads configuration from environment variables (see .env.sample). |
| 4 | + */ |
| 5 | + |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | +const { GoogleAuth } = require('google-auth-library'); |
| 9 | +require('dotenv').config(); |
| 10 | + |
| 11 | +function base64UrlDecode(input) { |
| 12 | + // Pad input to multiple of 4 and replace URL-safe chars |
| 13 | + input = input.replace(/-/g, '+').replace(/_/g, '/'); |
| 14 | + const pad = input.length % 4; |
| 15 | + if (pad) input += '='.repeat(4 - pad); |
| 16 | + return Buffer.from(input, 'base64').toString('utf8'); |
| 17 | +} |
| 18 | + |
| 19 | +function decodeJwt(jwt) { |
| 20 | + try { |
| 21 | + const [header, payload] = jwt.split('.'); |
| 22 | + const h = JSON.parse(base64UrlDecode(header)); |
| 23 | + const p = JSON.parse(base64UrlDecode(payload)); |
| 24 | + return { header: h, payload: p }; |
| 25 | + } catch (e) { |
| 26 | + return null; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +async function main() { |
| 31 | + const audience = process.env.IAP_AUDIENCE; |
| 32 | + const apiUrl = process.env.API_URL; |
| 33 | + const saJsonPath = process.env.GOOGLE_SA_JSON_PATH; |
| 34 | + const saJsonInline = process.env.GOOGLE_SA_JSON; |
| 35 | + const logClaims = String(process.env.LOG_JWT_CLAIMS || 'false').toLowerCase() === 'true'; |
| 36 | + |
| 37 | + if (!audience) { |
| 38 | + console.error('Missing IAP_AUDIENCE'); |
| 39 | + process.exit(2); |
| 40 | + } |
| 41 | + if (!apiUrl) { |
| 42 | + console.error('Missing API_URL'); |
| 43 | + process.exit(2); |
| 44 | + } |
| 45 | + |
| 46 | + let credentials; |
| 47 | + if (saJsonPath && fs.existsSync(path.resolve(saJsonPath))) { |
| 48 | + credentials = JSON.parse(fs.readFileSync(path.resolve(saJsonPath), 'utf8')); |
| 49 | + } else if (saJsonInline) { |
| 50 | + credentials = JSON.parse(saJsonInline); |
| 51 | + } else { |
| 52 | + console.error('Provide GOOGLE_SA_JSON_PATH or GOOGLE_SA_JSON'); |
| 53 | + process.exit(2); |
| 54 | + } |
| 55 | + |
| 56 | + const auth = new GoogleAuth({ credentials }); |
| 57 | + const client = await auth.getIdTokenClient(audience); |
| 58 | + |
| 59 | + // Intercept to print the ID token (and optionally decode claims) |
| 60 | + const origRequest = client.request.bind(client); |
| 61 | + client.request = async (opts) => { |
| 62 | + const res = await origRequest(opts); |
| 63 | + return res; |
| 64 | + }; |
| 65 | + |
| 66 | + // The getRequestHeaders() includes Authorization header; we can decode claims for sanity |
| 67 | + const headers = await client.getRequestHeaders(); |
| 68 | + const token = (headers.Authorization || headers.authorization || '').replace(/^Bearer\s+/i, ''); |
| 69 | + if (!token) { |
| 70 | + console.error('Failed to obtain ID token'); |
| 71 | + process.exit(1); |
| 72 | + } |
| 73 | + if (logClaims) { |
| 74 | + const decoded = decodeJwt(token); |
| 75 | + if (decoded) { |
| 76 | + console.log('JWT header:', JSON.stringify(decoded.header, null, 2)); |
| 77 | + console.log('JWT payload:', JSON.stringify(decoded.payload, null, 2)); |
| 78 | + if (decoded.payload && decoded.payload.iss) { |
| 79 | + console.log('JWT issuer:', decoded.payload.iss); |
| 80 | + } |
| 81 | + if (decoded.payload && decoded.payload.aud !== audience) { |
| 82 | + console.warn('Warning: token aud does not match IAP_AUDIENCE'); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + console.log(`Calling ${apiUrl} with IAP audience ${audience}...`); |
| 88 | + try { |
| 89 | + const res = await client.request({ url: apiUrl }); |
| 90 | + console.log('Status:', res.status); |
| 91 | + if (res.status >= 400) { |
| 92 | + console.error('Error body:', res.data); |
| 93 | + process.exit(1); |
| 94 | + } |
| 95 | + console.log(typeof res.data === 'string' ? res.data : JSON.stringify(res.data)); |
| 96 | + } catch (err) { |
| 97 | + if (err.response) { |
| 98 | + console.error('Request failed:', err.response.status, err.response.data); |
| 99 | + const body = typeof err.response.data === 'string' ? err.response.data : JSON.stringify(err.response.data); |
| 100 | + if (err.response.status === 401 && /Invalid GCIP ID token/i.test(body)) { |
| 101 | + console.error('Hint: IAP is configured with Identity Platform (GCIP). Use fetch-gcip.js instead of fetch-iap.js.'); |
| 102 | + } |
| 103 | + } else { |
| 104 | + console.error('Error:', err.message); |
| 105 | + } |
| 106 | + process.exit(1); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +main(); |
0 commit comments