Skip to content

Commit d03bdea

Browse files
jrusso1020claude
andauthored
fix(cli): prefer OAuth session over HEYGEN_API_KEY (#7051)
* fix(cli): prefer OAuth session over HEYGEN_API_KEY A signed-in `hyperframes auth login` session now wins over an API key in the environment, so a paid plan on the login is used even when the shell also exports HEYGEN_API_KEY. New order: HEYGEN_ACCESS_TOKEN (host app), the stored login (refreshed when expired), HEYGEN_API_KEY, HYPERFRAMES_API_KEY, the saved API key. The env key still comes first when HEYGEN_API_URL or HEYGEN_API_BASE names a host app's gateway, and takes over for the rest of the process when the login cannot be refreshed. The media-use audio helper follows the same order. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * fix(cli): fall back to the env key only when HeyGen refuses the refresh A 503, a network error or an overlapping refresh no longer switches the process to HEYGEN_API_KEY. The fallback now needs a refused grant or an expired login with no refresh token. A login another process stored since is re-read first, and refreshes that overlap in one process share one request. The media-use helper no longer treats HeyGen's own API base as a gateway. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * fix(cli): keep the login when the refresh reply can't be read Only a 400/401 from the token endpoint marks the grant as refused and switches to the env key. A 200 whose body can't be parsed now throws and leaves the login in place, so the next call refreshes again. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 92943d9 commit d03bdea

24 files changed

Lines changed: 632 additions & 103 deletions

File tree

‎docs/guides/authentication.mdx‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Signing in is the same OAuth step as creating an account — new users land on t
3737
</Step>
3838
</Steps>
3939

40-
HyperFrames uses its own public OAuth client and stores credentials in `~/.hyperframes/credentials` (mode `0600`). Sign in again after upgrading from the shared HeyGen login. The separate [`heygen` CLI](https://github.com/heygen-com/heygen-cli) keeps its own login in `~/.heygen`; HyperFrames does not move or delete it. An expired HyperFrames OAuth session requires refresh or sign-in and never selects a saved API key instead. An explicit API-key login or environment key selects API-key billing.
40+
HyperFrames uses its own public OAuth client and stores credentials in `~/.hyperframes/credentials` (mode `0600`). Sign in again after upgrading from the shared HeyGen login. The separate [`heygen` CLI](https://github.com/heygen-com/heygen-cli) keeps its own login in `~/.heygen`; HyperFrames does not move or delete it. A signed-in HyperFrames session wins over `HEYGEN_API_KEY` in the environment. The environment key is used only when no session is signed in, when HeyGen refuses to refresh the session, or when a host app routes calls through its own gateway. An expired session never selects a saved API key instead. An API key, from the environment or `auth login --api-key`, selects API-key billing.
4141

4242
`auth status` verifies identity. For a dedicated HyperFrames OAuth login it reports the billing product, but the HyperFrames balance is unavailable here: `/v3/users/me` returns generic HeyGen account billing fields. Those raw fields remain in JSON and are not presented as the HyperFrames balance. Host-injected or custom-client OAuth is reported as an unverified billing product.
4343

@@ -54,13 +54,14 @@ sound retrieval. It resolves first-match-wins:
5454

5555
1. `HEYGEN_API_BASE` with `HEYGEN_API_KEY` — a host app's own gateway, set by the app that started the workflow
5656
2. `HEYGEN_ACCESS_TOKEN` — an OAuth token a host app injects
57-
3. `HEYGEN_API_KEY` — environment variable
58-
4. `HYPERFRAMES_API_KEY` — alias, for parity with other tools
59-
5. `~/.hyperframes/credentials` — written by `hyperframes auth login`
57+
3. The session `hyperframes auth login` saved in `~/.hyperframes/credentials`, while it is unexpired
58+
4. `HEYGEN_API_KEY` — environment variable
59+
5. `HYPERFRAMES_API_KEY` — alias, for parity with other tools
60+
6. An API key saved in `~/.hyperframes/credentials` by `hyperframes auth login --api-key`
6061

6162
A nearby project `.env` can supply the keys, but never `HEYGEN_API_BASE`. Point at a different config directory
62-
with `HYPERFRAMES_CONFIG_DIR`. The `hyperframes` CLI's own commands check the API keys before `HEYGEN_ACCESS_TOKEN`; see
63-
the [CLI reference](/packages/cli). When `media-use` invokes the separate `heygen` executable, it supplies an isolated temporary credential containing the selected access token. That helper cannot refresh a HyperFrames grant or pick up another HeyGen login.
63+
with `HYPERFRAMES_CONFIG_DIR`. The scripts can't refresh a session; the `hyperframes` CLI's own commands use the same
64+
order and refresh an expired session first. See the [CLI reference](/packages/cli). When `media-use` invokes the separate `heygen` executable, it supplies an isolated temporary credential containing the selected access token. That helper cannot refresh a HyperFrames grant or pick up another HeyGen login.
6465

6566
## Providers used by agent workflows
6667

@@ -71,7 +72,7 @@ vision key is available.
7172

7273
| Capability | Provider order | Key(s) — first match wins | Local dependency |
7374
|------------|----------------|---------------------------|------------------|
74-
| **Voice (TTS)** | HeyGen → ElevenLabs → Kokoro | `HEYGEN_API_KEY` → `HYPERFRAMES_API_KEY` → `~/.hyperframes` · then `ELEVENLABS_API_KEY` | Kokoro: `pip install kokoro-onnx soundfile` |
75+
| **Voice (TTS)** | HeyGen → ElevenLabs → Kokoro | HeyGen credential (above) · then `ELEVENLABS_API_KEY` | Kokoro: `pip install kokoro-onnx soundfile` |
7576
| **Music (BGM)** | HeyGen library → Lyria → MusicGen | HeyGen credential (above) · then `GEMINI_API_KEY` → `GOOGLE_API_KEY` | MusicGen: `pip install transformers torch soundfile numpy` |
7677
| **Sound effects** | HeyGen library → bundled library | HeyGen credential (above) | bundled — no deps |
7778
| **Capture descriptions** | OpenRouter → Gemini | `OPENROUTER_API_KEY` → `GEMINI_API_KEY` | None; optional for [website capture](/guides/product-launch-video) |

‎docs/packages/cli.mdx‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,8 @@ FFmpeg.
936936

937937
Only at `--quality best` is audio uploaded. Stems and the `api` provider then
938938
call HeyGen, which may bill your account, with the same credential as the other
939-
HeyGen commands: `HEYGEN_API_KEY`, `HYPERFRAMES_API_KEY`, `HEYGEN_ACCESS_TOKEN`,
940-
or a credential saved by `hyperframes auth login`, on the API at
939+
HeyGen commands (see [`hyperframes auth`](#hyperframes-auth) for the order),
940+
on the API at
941941
`HEYGEN_API_URL` when set. API keys are sent as `x-api-key`; OAuth credentials
942942
are sent as bearer tokens. If the service refuses a request, analysis falls
943943
back with a warning. Without a credential, stems are skipped and `api` is
@@ -2294,10 +2294,17 @@ of the `heygen` CLI. A previous shared login requires a fresh HyperFrames sign-i
22942294

22952295
First match wins:
22962296

2297-
1. `HEYGEN_API_KEY`
2298-
2. `HYPERFRAMES_API_KEY` (a HyperFrames alias for the same thing)
2299-
3. `HEYGEN_ACCESS_TOKEN` (an OAuth token a host app injects; never refreshed or saved)
2300-
4. `~/.hyperframes/credentials`
2297+
1. `HEYGEN_ACCESS_TOKEN` (an OAuth token a host app injects; never refreshed or saved)
2298+
2. Your `hyperframes auth login` session in `~/.hyperframes/credentials`. An expired session is refreshed first.
2299+
3. `HEYGEN_API_KEY`
2300+
4. `HYPERFRAMES_API_KEY` (a HyperFrames alias for the same thing)
2301+
5. An API key saved with `hyperframes auth login --api-key`
2302+
2303+
A signed-in session wins over an API key in the environment. The environment key
2304+
is used instead when HeyGen refuses to refresh the session, or when `HEYGEN_API_URL` or
2305+
`HEYGEN_API_BASE` points at a host app's own gateway. To use the environment key
2306+
while signed in, run `hyperframes auth logout`. `auth status` shows which
2307+
credential is in use.
23012308

23022309
### `auth login`
23032310

@@ -2358,9 +2365,9 @@ hyperframes auth logout --yes # no prompt
23582365

23592366
| Variable | Description |
23602367
| --------------------- | ------------------------------------------------ |
2361-
| `HEYGEN_API_KEY` | Override the stored credential. |
2368+
| `HEYGEN_API_KEY` | API key, used when no session is signed in. Wins over a key saved with `--api-key`. |
23622369
| `HYPERFRAMES_API_KEY` | Alias for `HEYGEN_API_KEY`. |
2363-
| `HEYGEN_ACCESS_TOKEN` | OAuth token from a host app; never refreshed. |
2370+
| `HEYGEN_ACCESS_TOKEN` | OAuth token from a host app; used first, never refreshed. |
23642371
| `HEYGEN_API_URL` | API base URL (default `https://api.heygen.com`). |
23652372
| `HYPERFRAMES_CONFIG_DIR` | Credentials root (default `~/.hyperframes`). |
23662373
| `HYPERFRAMES_ENV` | `prod` (default) or `dev`; `dev` uses a separate credential directory. Hosts and the OAuth client id come from `HEYGEN_API_URL` and `HYPERFRAMES_OAUTH_*`. |

‎packages/cli/src/auth/_test-utils.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { promises as fs } from "node:fs";
1010
import { tmpdir } from "node:os";
1111
import { join } from "node:path";
12+
import { _resetLoginRefreshFailureForTests } from "./resolver.js";
1213

1314
const ENV_KEYS = [
1415
"HEYGEN_API_KEY",
@@ -48,8 +49,10 @@ export async function setupTempAuthEnv(prefix = "hf-auth-test-"): Promise<EnvFix
4849
delete process.env[k];
4950
}
5051
process.env["HYPERFRAMES_CONFIG_DIR"] = dir;
52+
_resetLoginRefreshFailureForTests();
5153

5254
const restore = async (): Promise<void> => {
55+
_resetLoginRefreshFailureForTests();
5356
for (const k of ENV_KEYS) {
5457
const v = saved[k];
5558
if (v === undefined) delete process.env[k];

‎packages/cli/src/auth/errors.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ export type AuthErrorCode =
1717
export class AuthError extends Error {
1818
readonly code: AuthErrorCode;
1919
readonly hint?: string;
20+
/** HeyGen refused a token refresh (400/401), so the grant is gone; a reply the CLI couldn't read is not this. */
21+
readonly refused: boolean;
2022

21-
constructor(code: AuthErrorCode, message: string, hint?: string) {
23+
constructor(
24+
code: AuthErrorCode,
25+
message: string,
26+
hint?: string,
27+
opts: { refused?: boolean } = {},
28+
) {
2229
super(message);
2330
this.name = "AuthError";
2431
this.code = code;
2532
this.hint = hint;
33+
this.refused = opts.refused ?? false;
2634
}
2735
}
2836

@@ -57,11 +65,12 @@ export const ErrOAuthNotConfigured = () =>
5765
"Set HYPERFRAMES_OAUTH_CLIENT_ID, or run `hyperframes auth login --api-key`.",
5866
);
5967

60-
export const ErrRefreshFailed = (detail?: string) =>
68+
export const ErrRefreshFailed = (detail?: string, opts: { refused?: boolean } = {}) =>
6169
new AuthError(
6270
"REFRESH_FAILED",
6371
detail ? `Failed to refresh OAuth tokens: ${detail}` : "Failed to refresh OAuth tokens",
6472
"Run `hyperframes auth login` to re-authenticate.",
73+
opts,
6574
);
6675

6776
export const ErrLoginExpired = () =>

‎packages/cli/src/auth/media-environment.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,60 @@ describe("media-use authentication", () => {
6161
expect(await fs.readFile(join(fixture.dir, "credentials"), "utf8")).toContain("rotated");
6262
});
6363

64+
it("passes the login, not HEYGEN_API_KEY, when both are present", async () => {
65+
await persistFreshOAuth({ access_token: "hf-access", refresh_token: "hf-refresh" });
66+
process.env["HEYGEN_API_KEY"] = "env-key";
67+
await withMediaAuthEnvironment(async (env) => {
68+
expect(env.HEYGEN_ACCESS_TOKEN).toBe("hf-access");
69+
expect(env.HEYGEN_API_KEY).toBeUndefined();
70+
});
71+
});
72+
73+
it("falls back to HEYGEN_API_KEY when the login cannot be refreshed", async () => {
74+
await persistFreshOAuth({
75+
access_token: "expired",
76+
refresh_token: "hf-refresh",
77+
expires_at: new Date(Date.now() - 60 * 1000).toISOString(),
78+
});
79+
process.env["HEYGEN_API_KEY"] = "env-key";
80+
const fetchMock = vi.fn(async () => new Response("invalid_grant", { status: 400 }));
81+
vi.stubGlobal("fetch", fetchMock);
82+
await withMediaAuthEnvironment(async (env) => {
83+
expect(fetchMock).toHaveBeenCalledOnce();
84+
expect(env.HEYGEN_API_KEY).toBe("env-key");
85+
expect(env.HEYGEN_ACCESS_TOKEN).toBeUndefined();
86+
});
87+
});
88+
89+
it("still fails when the login cannot be refreshed and no env key is set", async () => {
90+
await persistFreshOAuth({
91+
access_token: "expired",
92+
refresh_token: "hf-refresh",
93+
expires_at: new Date(Date.now() - 60 * 1000).toISOString(),
94+
});
95+
vi.stubGlobal(
96+
"fetch",
97+
vi.fn(async () => new Response("invalid_grant", { status: 400 })),
98+
);
99+
await expect(withMediaAuthEnvironment(() => undefined)).rejects.toMatchObject({
100+
code: "REFRESH_FAILED",
101+
});
102+
});
103+
104+
it("gives a host gateway its own key even when a login is stored", async () => {
105+
await persistFreshOAuth({ access_token: "hf-access", refresh_token: "hf-refresh" });
106+
process.env["HEYGEN_API_BASE"] = "https://gateway.example";
107+
process.env["HEYGEN_API_KEY"] = "gateway-key";
108+
try {
109+
await withMediaAuthEnvironment(async (env) => {
110+
expect(env.HEYGEN_API_KEY).toBe("gateway-key");
111+
expect(env.HEYGEN_ACCESS_TOKEN).toBeUndefined();
112+
});
113+
} finally {
114+
delete process.env["HEYGEN_API_BASE"];
115+
}
116+
});
117+
64118
it("refuses to send OAuth to a non-HeyGen media gateway", async () => {
65119
await persistFreshOAuth({ access_token: "hf-access", refresh_token: "hf-refresh" });
66120
process.env["HEYGEN_API_BASE"] = "https://gateway.example";

‎packages/cli/src/auth/media-environment.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from "node:path";
44
import { apiBaseUrl } from "./client.js";
55
import { refreshTokens } from "./oauth.js";
66
import { AuthError, isAuthError } from "./errors.js";
7-
import { resolveCredential, tryResolveCredential } from "./resolver.js";
7+
import { afterRefreshFailure, resolveCredential, tryResolveCredential } from "./resolver.js";
88
import { writeStore } from "./store.js";
99

1010
// Avatar helpers can poll for five minutes after submitting paid work.
@@ -28,8 +28,11 @@ export async function withMediaAuthEnvironment<T>(run: (env: NodeJS.ProcessEnv)
2828
credential.refresh_token &&
2929
(credential.refreshable || expiresDuringHelper)
3030
) {
31-
await refreshTokens(credential.refresh_token);
32-
credential = await resolveCredential();
31+
const refreshToken = credential.refresh_token;
32+
credential = await refreshTokens(refreshToken).then(
33+
() => resolveCredential(),
34+
(error: unknown) => afterRefreshFailure(error, refreshToken),
35+
);
3336
}
3437

3538
const dir = await fs.mkdtemp(join(tmpdir(), "hyperframes-media-auth-"));

‎packages/cli/src/auth/oauth.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,21 @@ function retryAfterSeconds(response: Response, nowMs: number): number | undefine
355355
return Math.min(Math.max(Math.ceil((retryAt - nowMs) / 1000), 0), MAX_DEVICE_POLL_SECONDS);
356356
}
357357

358-
export async function refreshTokens(
358+
const refreshing = new Map<string, Promise<OAuthTokens>>();
359+
360+
/** Refresh the stored login. Calls that overlap in this process share one request, since the server rotates the token. */
361+
export function refreshTokens(
359362
refresh_token: string,
360363
opts: RefreshOptions = {},
361364
): Promise<OAuthTokens> {
365+
const pending = refreshing.get(refresh_token);
366+
if (pending) return pending;
367+
const request = refreshOnce(refresh_token, opts).finally(() => refreshing.delete(refresh_token));
368+
refreshing.set(refresh_token, request);
369+
return request;
370+
}
371+
372+
async function refreshOnce(refresh_token: string, opts: RefreshOptions): Promise<OAuthTokens> {
362373
const { credentials } = await readStore();
363374
if (credentials.oauth?.refresh_token !== refresh_token) throw ErrLoginChanged();
364375
assertOAuthIdentity(credentials.oauth);
@@ -380,7 +391,7 @@ export async function refreshTokens(
380391
});
381392

382393
if (res.status === 400 || res.status === 401) {
383-
throw ErrRefreshFailed(await safeText(res));
394+
throw ErrRefreshFailed(await safeText(res), { refused: true });
384395
}
385396
if (!res.ok) {
386397
throw ErrApi(res.status, (await safeText(res)) || res.statusText);

‎packages/cli/src/auth/resolver.test.ts‎

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { promises as fs } from "node:fs";
33
import { join } from "node:path";
44
import { afterEach, beforeEach, describe, expect, it } from "vitest";
55
import { setupTempAuthEnv } from "./_test-utils.js";
6-
import { isAuthError } from "./errors.js";
7-
import { resolveCredential, tryResolveCredential } from "./resolver.js";
6+
import { ErrApi, ErrLoginChanged, ErrRefreshFailed, isAuthError } from "./errors.js";
7+
import { afterRefreshFailure, resolveCredential, tryResolveCredential } from "./resolver.js";
88
import { writeStore as writeUnboundStore, type Credentials } from "./store.js";
99

1010
async function writeStore(credentials: Credentials): Promise<void> {
@@ -27,8 +27,103 @@ describe("auth/resolver", () => {
2727
await fixture.restore();
2828
});
2929

30-
it("prefers HEYGEN_API_KEY over everything else", async () => {
30+
const inOneHour = () => new Date(Date.now() + 60 * 60 * 1000).toISOString();
31+
const anHourAgo = () => new Date(Date.now() - 60 * 60 * 1000).toISOString();
32+
33+
it("prefers a signed-in login over HEYGEN_API_KEY", async () => {
34+
process.env["HEYGEN_API_KEY"] = "env-key";
35+
process.env["HYPERFRAMES_API_KEY"] = "alias-key";
36+
await writeStore({
37+
api_key: "file-key",
38+
oauth: { access_token: "fresh-at", refresh_token: "rt", expires_at: inOneHour() },
39+
});
40+
const r = await resolveCredential();
41+
expect(r).toMatchObject({ type: "oauth", access_token: "fresh-at", source: "file_json" });
42+
});
43+
44+
it("prefers an expired login that can refresh over HEYGEN_API_KEY", async () => {
45+
process.env["HEYGEN_API_KEY"] = "env-key";
46+
await writeStore({
47+
oauth: { access_token: "stale-at", refresh_token: "rt", expires_at: anHourAgo() },
48+
});
49+
const r = await resolveCredential();
50+
expect(r).toMatchObject({ type: "oauth", access_token: "stale-at", refreshable: true });
51+
});
52+
53+
it("uses HEYGEN_API_KEY when the login expired and cannot refresh", async () => {
54+
process.env["HEYGEN_API_KEY"] = "env-key";
55+
await writeStore({ oauth: { access_token: "stale-at", expires_at: anHourAgo() } });
56+
expect(await resolveCredential()).toEqual({ type: "api_key", key: "env-key", source: "env" });
57+
});
58+
59+
it("uses HEYGEN_API_KEY over a login from another OAuth client", async () => {
60+
process.env["HEYGEN_API_KEY"] = "env-key";
61+
await writeStore({ oauth: { access_token: "foreign", client_id: "shared-heygen-cli" } });
62+
expect(await resolveCredential()).toEqual({ type: "api_key", key: "env-key", source: "env" });
63+
});
64+
65+
it("uses HEYGEN_API_KEY when the credentials file is broken", async () => {
66+
process.env["HEYGEN_API_KEY"] = "env-key";
67+
await fs.writeFile(join(dir, "credentials"), "{not valid", { mode: 0o600 });
68+
expect(await resolveCredential()).toEqual({ type: "api_key", key: "env-key", source: "env" });
69+
});
70+
71+
it("keeps the env key first when HEYGEN_API_URL names a host app's gateway", async () => {
72+
process.env["HEYGEN_API_KEY"] = "gateway-token";
73+
process.env["HEYGEN_API_URL"] = "http://127.0.0.1:4100";
74+
await writeStore({ oauth: { access_token: "fresh-at", expires_at: inOneHour() } });
75+
expect(await resolveCredential()).toEqual({
76+
type: "api_key",
77+
key: "gateway-token",
78+
source: "env",
79+
});
80+
process.env["HEYGEN_API_URL"] = "https://api.dev.heygen.com";
81+
expect(await resolveCredential()).toMatchObject({ type: "oauth", access_token: "fresh-at" });
82+
process.env["HEYGEN_API_BASE"] = "https://gateway.example";
83+
expect(await resolveCredential()).toMatchObject({ type: "api_key", key: "gateway-token" });
84+
});
85+
86+
it("uses the env key for the rest of the process once HeyGen refuses to refresh the login", async () => {
87+
await writeStore({
88+
oauth: { access_token: "at", refresh_token: "rt", expires_at: anHourAgo() },
89+
});
90+
const refused = ErrRefreshFailed("invalid_grant", { refused: true });
91+
await expect(afterRefreshFailure(refused, "rt")).rejects.toMatchObject({
92+
code: "REFRESH_FAILED",
93+
});
94+
expect(await resolveCredential()).toMatchObject({ type: "oauth", refreshable: true });
95+
96+
process.env["HEYGEN_API_KEY"] = "env-key";
97+
// A server error or a login another process is changing says nothing about the grant.
98+
await expect(afterRefreshFailure(ErrApi(503, "down"), "rt")).rejects.toMatchObject({
99+
code: "API_ERROR",
100+
});
101+
await expect(afterRefreshFailure(ErrLoginChanged(), "rt")).rejects.toMatchObject({
102+
code: "LOGIN_CHANGED",
103+
});
104+
// Nor does a reply the CLI couldn't read.
105+
await expect(
106+
afterRefreshFailure(ErrRefreshFailed("no access_token"), "rt"),
107+
).rejects.toMatchObject({ code: "REFRESH_FAILED" });
108+
expect(await resolveCredential()).toMatchObject({ type: "oauth", refreshable: true });
109+
110+
const fallback = { type: "api_key", key: "env-key", source: "env" };
111+
expect(await afterRefreshFailure(refused, "rt")).toEqual(fallback);
112+
expect(await resolveCredential()).toEqual(fallback);
113+
});
114+
115+
it("prefers a host app's HEYGEN_ACCESS_TOKEN over the login and env keys", async () => {
31116
process.env["HEYGEN_ACCESS_TOKEN"] = "host-token";
117+
process.env["HEYGEN_API_KEY"] = "env-key";
118+
await writeStore({ oauth: { access_token: "fresh-at", expires_at: inOneHour() } });
119+
expect(await resolveCredential()).toMatchObject({
120+
type: "oauth",
121+
access_token: "host-token",
122+
source: "env_oauth",
123+
});
124+
});
125+
126+
it("prefers HEYGEN_API_KEY over HYPERFRAMES_API_KEY and a saved API key", async () => {
32127
process.env["HEYGEN_API_KEY"] = "env-key";
33128
process.env["HYPERFRAMES_API_KEY"] = "alias-key";
34129
await writeStore({ api_key: "file-key" });
@@ -37,7 +132,6 @@ describe("auth/resolver", () => {
37132
});
38133

39134
it("falls through to HYPERFRAMES_API_KEY", async () => {
40-
process.env["HEYGEN_ACCESS_TOKEN"] = "host-token";
41135
process.env["HYPERFRAMES_API_KEY"] = "alias-key";
42136
await writeStore({ api_key: "file-key" });
43137
const r = await resolveCredential();

0 commit comments

Comments
 (0)