Skip to content

Commit bb59411

Browse files
committed
fix(cli): address code-review findings on auth PR
1 parent 9a7b3ef commit bb59411

19 files changed

Lines changed: 1903 additions & 0 deletions

‎docs/packages/cli.mdx‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,63 @@ This is suppressed in CI environments, non-TTY shells, and when `HYPERFRAMES_NO_
860860
</Tab>
861861
</Tabs>
862862

863+
## hyperframes auth
864+
865+
Sign in to HeyGen and manage credentials. Credentials are stored in
866+
`~/.heygen/credentials` (mode `0600`) and are **shared with the
867+
`heygen` CLI** — sign in with one and the other picks up the session.
868+
869+
Resolution order (first match wins):
870+
871+
1. `HEYGEN_API_KEY` environment variable
872+
2. `HYPERFRAMES_API_KEY` environment variable (hyperframes alias)
873+
3. `~/.heygen/credentials`
874+
875+
### Subcommands
876+
877+
#### `auth login --api-key`
878+
879+
Save a HeyGen API key. The key is verified against `GET /v3/users/me`
880+
before the command reports success; a rejected key is not left on disk.
881+
882+
```bash
883+
# Interactive hidden-input prompt
884+
hyperframes auth login --api-key
885+
886+
# Pipe a key from stdin (CI-friendly)
887+
echo "$HEYGEN_API_KEY" | hyperframes auth login --api-key
888+
```
889+
890+
#### `auth status`
891+
892+
Show the active credential's source, type, and verified identity
893+
(account + billing snapshot). Exits non-zero when nothing is configured
894+
or the API rejects the credential, so scripts can check sign-in state.
895+
896+
```bash
897+
hyperframes auth status
898+
hyperframes auth status --json # machine-readable
899+
```
900+
901+
#### `auth logout`
902+
903+
Remove the stored credential. Prompts for confirmation on a TTY.
904+
905+
```bash
906+
hyperframes auth logout
907+
hyperframes auth logout --keep-api-key # clear only an OAuth session
908+
hyperframes auth logout --yes # skip the confirmation prompt
909+
```
910+
911+
### Environment variables
912+
913+
| Variable | Description |
914+
|----------|-------------|
915+
| `HEYGEN_API_KEY` | Override the stored credential. |
916+
| `HYPERFRAMES_API_KEY` | Alias for `HEYGEN_API_KEY`. |
917+
| `HEYGEN_API_URL` | API base URL (default `https://api.heygen.com`). |
918+
| `HEYGEN_CONFIG_DIR` | Credentials directory (default `~/.heygen`). |
919+
863920
## hyperframes lambda
864921

865922
Deploy HyperFrames distributed rendering to AWS Lambda and drive renders from your laptop or CI.
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
2+
import { AuthClient, apiBaseUrl, buildAuthHeaders } from "./client.js";
3+
import { isAuthError } from "./errors.js";
4+
import type { ResolvedCredential } from "./resolver.js";
5+
6+
function jsonFetch(body: unknown, status = 200): typeof fetch {
7+
return (async () =>
8+
new Response(JSON.stringify(body), {
9+
status,
10+
headers: { "content-type": "application/json" },
11+
})) as unknown as typeof fetch;
12+
}
13+
14+
function textFetch(body: string, status: number): typeof fetch {
15+
return (async () => new Response(body, { status })) as unknown as typeof fetch;
16+
}
17+
18+
function apiKeyCred(): ResolvedCredential {
19+
return { type: "api_key", key: "hg_x", source: "env" };
20+
}
21+
22+
function makeClient(fetchImpl: typeof fetch): AuthClient {
23+
return new AuthClient({ baseUrl: "https://api.test.example", fetchImpl });
24+
}
25+
26+
describe("auth/client", () => {
27+
const original = process.env["HEYGEN_API_URL"];
28+
29+
beforeEach(() => {
30+
delete process.env["HEYGEN_API_URL"];
31+
});
32+
33+
afterEach(() => {
34+
if (original !== undefined) process.env["HEYGEN_API_URL"] = original;
35+
else delete process.env["HEYGEN_API_URL"];
36+
});
37+
38+
it("apiBaseUrl defaults to https://api.heygen.com", () => {
39+
expect(apiBaseUrl()).toBe("https://api.heygen.com");
40+
});
41+
42+
it("apiBaseUrl honors HEYGEN_API_URL and strips trailing slash", () => {
43+
process.env["HEYGEN_API_URL"] = "https://api.dev.heygen.com/";
44+
expect(apiBaseUrl()).toBe("https://api.dev.heygen.com");
45+
});
46+
47+
it("buildAuthHeaders uses Bearer for oauth", () => {
48+
const cred: ResolvedCredential = {
49+
type: "oauth",
50+
access_token: "at_123",
51+
source: "file_json",
52+
refreshable: false,
53+
};
54+
expect(buildAuthHeaders(cred)).toEqual({ authorization: "Bearer at_123" });
55+
});
56+
57+
it("buildAuthHeaders uses x-api-key for api_key", () => {
58+
expect(buildAuthHeaders(apiKeyCred())).toEqual({ "x-api-key": "hg_x" });
59+
});
60+
61+
it("getCurrentUser parses a wrapped {data: {...}} payload", async () => {
62+
const client = makeClient(
63+
jsonFetch({
64+
code: 100,
65+
message: "ok",
66+
data: {
67+
username: "alice",
68+
69+
billing_type: "subscription",
70+
subscription: { plan: "team", credits: { premium_credits: 4200 } },
71+
},
72+
}),
73+
);
74+
const user = await client.getCurrentUser(apiKeyCred());
75+
expect(user.username).toBe("alice");
76+
expect(user.email).toBe("[email protected]");
77+
expect(user.subscription?.plan).toBe("team");
78+
expect(user.subscription?.credits?.premium_credits).toBe(4200);
79+
});
80+
81+
it("getCurrentUser parses an unwrapped payload", async () => {
82+
const client = makeClient(jsonFetch({ email: "[email protected]" }));
83+
const user = await client.getCurrentUser(apiKeyCred());
84+
expect(user.email).toBe("[email protected]");
85+
});
86+
87+
it("getCurrentUser throws ErrUnauthenticated on 401", async () => {
88+
const client = makeClient(textFetch("invalid token", 401));
89+
await expect(client.getCurrentUser(apiKeyCred())).rejects.toSatisfy((err) => {
90+
return isAuthError(err) && (err as { code: string }).code === "UNAUTHENTICATED";
91+
});
92+
});
93+
94+
it("getCurrentUser throws ErrApi on 5xx", async () => {
95+
const client = makeClient(textFetch("upstream", 503));
96+
await expect(client.getCurrentUser(apiKeyCred())).rejects.toSatisfy((err) => {
97+
return isAuthError(err) && (err as { code: string }).code === "API_ERROR";
98+
});
99+
});
100+
101+
it("getCurrentUser throws ErrApi when 2xx body is not valid JSON", async () => {
102+
const fetchImpl = (async () =>
103+
new Response("<html>not json</html>", {
104+
status: 200,
105+
headers: { "content-type": "text/html" },
106+
})) as unknown as typeof fetch;
107+
const client = makeClient(fetchImpl);
108+
await expect(client.getCurrentUser(apiKeyCred())).rejects.toSatisfy((err) => {
109+
return isAuthError(err) && (err as { code: string }).code === "API_ERROR";
110+
});
111+
});
112+
113+
it("getCurrentUser returns empty UserInfo when payload.data is an array", async () => {
114+
const client = makeClient(jsonFetch({ code: 0, data: [{ email: "x@y" }] }));
115+
const user = await client.getCurrentUser(apiKeyCred());
116+
expect(user).toEqual({});
117+
});
118+
119+
it("getCurrentUser scrubs hg_ keys and JWTs from 401 detail", async () => {
120+
const fetchImpl = textFetch(
121+
'invalid request — got header "x-api-key: hg_supersecret_abc123"',
122+
401,
123+
);
124+
const client = makeClient(fetchImpl);
125+
try {
126+
await client.getCurrentUser(apiKeyCred());
127+
} catch (err) {
128+
const msg = (err as Error).message;
129+
expect(msg).not.toContain("hg_supersecret_abc123");
130+
expect(msg).toContain("<redacted>");
131+
return;
132+
}
133+
throw new Error("expected rejection");
134+
});
135+
136+
it("getCurrentUser redacts the full Authorization: Bearer value (not just the scheme)", async () => {
137+
const fetchImpl = textFetch(
138+
"rejected — echoed Authorization: Bearer at_opaque_secret_999",
139+
401,
140+
);
141+
const client = makeClient(fetchImpl);
142+
try {
143+
await client.getCurrentUser(apiKeyCred());
144+
} catch (err) {
145+
const msg = (err as Error).message;
146+
expect(msg).not.toContain("at_opaque_secret_999");
147+
expect(msg).not.toContain("Bearer at_opaque_secret_999");
148+
expect(msg).toContain("<redacted>");
149+
return;
150+
}
151+
throw new Error("expected rejection");
152+
});
153+
154+
it("getCurrentUser sends the right header for oauth credentials", async () => {
155+
let captured: Record<string, string> = {};
156+
const fetchImpl = (async (_url: string, init?: RequestInit) => {
157+
captured = (init?.headers as Record<string, string>) ?? {};
158+
return new Response(JSON.stringify({ email: "[email protected]" }), {
159+
status: 200,
160+
headers: { "content-type": "application/json" },
161+
});
162+
}) as unknown as typeof fetch;
163+
164+
const client = makeClient(fetchImpl);
165+
await client.getCurrentUser({
166+
type: "oauth",
167+
access_token: "at_xyz",
168+
source: "file_json",
169+
refreshable: false,
170+
});
171+
expect(captured["authorization"]).toBe("Bearer at_xyz");
172+
});
173+
});

0 commit comments

Comments
 (0)