-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement signedConfig and signature support for HMAC auth #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emptyhammond
wants to merge
12
commits into
main
Choose a base branch
from
inf-6536/update-react-web-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4943bf4
feat: implement signedConfig and signature support for HMAC authentic…
emptyhammond 66e92ae
chore: additional comments to clarify skipped tests
kennethkalmer d0d51d5
fix: handle hanging child process in `did-you-mean.test.ts`
kennethkalmer d47b434
fix: add missing pnpm version to `.tool-versions`
kennethkalmer 50727cc
feat(web-cli): add /api/sign endpoint for credential signing
kennethkalmer ff5105f
feat(web-cli): update example app for signed config authentication
kennethkalmer 408c9c4
test(web-cli): add signing infrastructure for E2E tests
kennethkalmer 3255715
test(web-cli): update E2E tests for signed config authentication
kennethkalmer 5d3f7f0
docs: update .env.example for terminal server signing secret
kennethkalmer b059ab2
fix(web-cli): consistent secret priority and domain-scoped credential…
kennethkalmer b0e4822
fix(web-cli): detect auto-connect with signed config in rate limiter
kennethkalmer d454d36
refactor(react-web-cli): remove unused additionalEnvVars parameter
kennethkalmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| nodejs 22.14.0 | ||
| pnpm 10.28.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { VercelRequest, VercelResponse } from "@vercel/node"; | ||
| import { signCredentials, getSigningSecret } from "../server/sign-handler.js"; | ||
|
|
||
| /** | ||
| * Vercel Serverless Function: Sign credentials for terminal authentication | ||
| * | ||
| * This endpoint signs API keys with HMAC-SHA256 to create signed configs | ||
| * that can be validated by the terminal server. | ||
| * | ||
| * Environment Variables Required: | ||
| * - SIGNING_SECRET or TERMINAL_SERVER_SIGNING_SECRET | ||
| * | ||
| * Request Body: | ||
| * - apiKey: string (required) - Ably API key in format "appId.keyId:secret" | ||
| * - bypassRateLimit: boolean (optional) - Set to true for CI/testing | ||
| * | ||
| * Response: | ||
| * - signedConfig: string - JSON-encoded config that was signed | ||
| * - signature: string - HMAC-SHA256 hex signature | ||
| */ | ||
| export default async function handler( | ||
| req: VercelRequest, | ||
| res: VercelResponse, | ||
| ) { | ||
| // Only accept POST requests | ||
| if (req.method !== "POST") { | ||
| return res.status(405).json({ error: "Method not allowed" }); | ||
| } | ||
|
|
||
| // Get signing secret from environment | ||
| const secret = getSigningSecret(); | ||
|
|
||
| if (!secret) { | ||
| console.error("[/api/sign] Signing secret not configured"); | ||
| return res.status(500).json({ error: "Signing secret not configured" }); | ||
| } | ||
|
|
||
| const { apiKey, bypassRateLimit } = req.body; | ||
|
|
||
| if (!apiKey) { | ||
| return res.status(400).json({ error: "apiKey is required" }); | ||
| } | ||
|
|
||
| // Use shared signing logic | ||
| const result = signCredentials({ apiKey, bypassRateLimit }, secret); | ||
|
|
||
| res.status(200).json(result); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import crypto from "crypto"; | ||
|
|
||
| /** | ||
| * Shared signing logic for credential authentication | ||
| * Used by: Vercel function, Vite middleware, and preview server | ||
| */ | ||
|
|
||
| export interface SignRequest { | ||
| apiKey: string; | ||
| bypassRateLimit?: boolean; | ||
| } | ||
|
|
||
| export interface SignResponse { | ||
| signedConfig: string; | ||
| signature: string; | ||
| } | ||
|
|
||
| /** | ||
| * Sign credentials using HMAC-SHA256 | ||
| * @param request - Request containing apiKey and optional flags | ||
| * @param secret - Signing secret from environment | ||
| * @returns Signed config and signature | ||
| */ | ||
| export function signCredentials( | ||
| request: SignRequest, | ||
| secret: string, | ||
| ): SignResponse { | ||
| const { apiKey, bypassRateLimit } = request; | ||
|
|
||
| // Build config object (matches terminal server expectations) | ||
| const config = { | ||
| apiKey, | ||
| timestamp: Date.now(), | ||
| bypassRateLimit: bypassRateLimit || false, | ||
| }; | ||
|
|
||
| // Serialize to JSON - this exact string is what gets signed | ||
| const configString = JSON.stringify(config); | ||
|
|
||
| // Generate HMAC-SHA256 signature | ||
| const hmac = crypto.createHmac("sha256", secret); | ||
| hmac.update(configString); | ||
| const signature = hmac.digest("hex"); | ||
|
|
||
| return { | ||
| signedConfig: configString, | ||
| signature, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get signing secret from environment variables | ||
| * Checks multiple variable names for compatibility | ||
| */ | ||
| export function getSigningSecret(): string | null { | ||
| return ( | ||
| process.env.TERMINAL_SERVER_SIGNING_SECRET || | ||
| process.env.SIGNING_SECRET || | ||
| process.env.CI_BYPASS_SECRET || | ||
| null | ||
| ); | ||
kennethkalmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.