|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Script to generate a test JWT token for evault-core authentication |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * REGISTRY_ENTROPY_KEY_JWK='{"kty":"EC",...}' node generate-test-token.js |
| 8 | + * |
| 9 | + * Or set the environment variable in a .env file |
| 10 | + * |
| 11 | + * You can also use tsx to run it: |
| 12 | + * REGISTRY_ENTROPY_KEY_JWK='...' npx tsx generate-test-token.js |
| 13 | + */ |
| 14 | + |
| 15 | +const { importJWK, SignJWT } = require("jose"); |
| 16 | +const dotenv = require("dotenv"); |
| 17 | +const path = require("path"); |
| 18 | + |
| 19 | +// Load .env file if it exists (try both root and current directory) |
| 20 | +dotenv.config({ path: path.resolve(__dirname, "../../../.env") }); |
| 21 | +dotenv.config({ path: path.resolve(__dirname, ".env") }); |
| 22 | + |
| 23 | +async function generateTestToken() { |
| 24 | + const jwkString = '{"kty":"EC","use":"sig","alg":"ES256","kid":"entropy-key-1","crv":"P-256","x":"POWUVJwOulAW0gheTVUHF4nXUenMCg0jxhGKI8M1LLU","y":"Cb4GC8Tt0gW7zMr-DhsDJisGVNgWttwjnyQl1HyU7hg","d":"FWqvWBzoiZcD0hh4JAMdJ7foxFRGqyV2Ei_eWvr1Si4"}'; |
| 25 | + |
| 26 | + if (!jwkString) { |
| 27 | + console.error("Error: REGISTRY_ENTROPY_KEY_JWK environment variable is required"); |
| 28 | + console.error("\nUsage:"); |
| 29 | + console.error(" REGISTRY_ENTROPY_KEY_JWK='<your-jwk-json>' node generate-test-token.js"); |
| 30 | + console.error("\nOr set it in your .env file"); |
| 31 | + process.exit(1); |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + const jwk = JSON.parse(jwkString); |
| 36 | + const privateKey = await importJWK(jwk, "ES256"); |
| 37 | + |
| 38 | + // Generate token valid for 1 year |
| 39 | + const token = await new SignJWT({ |
| 40 | + // Add any custom claims you want |
| 41 | + platform: "eid-wallet", |
| 42 | + purpose: "public-key-sync", |
| 43 | + }) |
| 44 | + .setProtectedHeader({ |
| 45 | + alg: "ES256", |
| 46 | + kid: jwk.kid || "entropy-key-1", |
| 47 | + }) |
| 48 | + .setIssuedAt() |
| 49 | + .setExpirationTime("1y") // Valid for 1 year |
| 50 | + .sign(privateKey); |
| 51 | + |
| 52 | + console.log("\n✅ Generated JWT Token (valid for 1 year):\n"); |
| 53 | + console.log(token); |
| 54 | + console.log("\n📋 Use this token in the Authorization header:"); |
| 55 | + console.log(` Authorization: Bearer ${token}\n`); |
| 56 | + |
| 57 | + return token; |
| 58 | + } catch (error) { |
| 59 | + console.error("Error generating token:", error.message); |
| 60 | + if (error.message.includes("JSON")) { |
| 61 | + console.error("\nMake sure REGISTRY_ENTROPY_KEY_JWK is valid JSON"); |
| 62 | + } |
| 63 | + process.exit(1); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +generateTestToken(); |
| 68 | + |
0 commit comments