Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

fix(oauth): return sign-in through the hosted callback page (#349)

Sharky Wang66a35294ada7
diff --git a/packages/cli/src/oauth.ts b/packages/cli/src/oauth.ts
index 5a19c959..f0c735e4 100644
--- a/packages/cli/src/oauth.ts
+++ b/packages/cli/src/oauth.ts
@@ -87,7 +87,8 @@ export async function acquireOAuthCredential(
const verifier = base64url(randomBytes(32));
// S256 is part of OAuth PKCE. It authenticates this browser exchange; it is not content identity.
const challenge = base64url(createHash("sha256").update(verifier).digest());
- const state = base64url(randomBytes(24));
+ const nonce = base64url(randomBytes(24));
+ let state = nonce;
const server = createServer();
const callback = new Promise<string>((resolveCode, reject) => {
let settled = false;
@@ -140,7 +141,10 @@ export async function acquireOAuthCredential(
});
const address = server.address();
if (address === null || typeof address === "string") throw new Error("could not open a local OAuth callback");
- const redirectUri = `http://127.0.0.1:${address.port}/callback`;
+ // The hosted callback page forwards the code to this port when the browser shares this host;
+ // otherwise the page shows `code#state` for the user to deliver to this port themselves.
+ state = `${nonce}.${address.port}`;
+ const redirectUri = acquisition.redirectUri;
const authorize = new URL(acquisition.authorizationEndpoint);
authorize.searchParams.set("response_type", "code");
authorize.searchParams.set("client_id", acquisition.clientId);
diff --git a/packages/cli/test/oauth.test.ts b/packages/cli/test/oauth.test.ts
index 4a85b13c..540448f6 100644
--- a/packages/cli/test/oauth.test.ts
+++ b/packages/cli/test/oauth.test.ts
@@ -14,15 +14,22 @@ import { acquireOAuthCredential, authorizeBrowserLaunch } from "../src/oauth.js"
const acquisition = {
kind: "oauth2-pkce" as const,
authorizationEndpoint: "https://identity.example.test/authorize",
+ redirectUri: "https://identity.example.test/callback",
tokenEndpoint: "https://identity.example.test/token",
clientId: "client",
scopes: ["work"],
requestTimeoutMs: 1_000,
};
+/** Where the hosted callback page forwards the code: the port packed after the last `.` of state. */
+function loopbackCallback(authorization: URL): URL {
+ const state = authorization.searchParams.get("state")!;
+ return new URL(`http://127.0.0.1:${state.slice(state.lastIndexOf(".") + 1)}/callback`);
+}
+
function returnAuthorization(url: string, page: (html: string) => void): void {
const authorization = new URL(url);
- const redirect = new URL(authorization.searchParams.get("redirect_uri")!);
+ const redirect = loopbackCallback(authorization);
redirect.searchParams.set("state", authorization.searchParams.get("state")!);
redirect.searchParams.set("code", "authorization-code");
void globalThis.fetch(redirect).then(async (response) => {
@@ -60,8 +67,7 @@ test("OAuth callback releases another browser connection after sending its page"
let browserConnection: ReturnType<typeof createConnection> | undefined;
const raw = await acquireOAuthCredential(acquisition, {
open: (url) => {
- const authorization = new URL(url);
- const redirect = new URL(authorization.searchParams.get("redirect_uri")!);
+ const redirect = loopbackCallback(new URL(url));
browserConnection = createConnection({ host: redirect.hostname, port: Number(redirect.port) });
void once(browserConnection, "connect").then(() => {
browserConnection!.write(`GET /favicon.ico HTTP/1.1\r\nHost: ${redirect.host}\r\n`);
@@ -150,8 +156,9 @@ const value = await acquireOAuthCredential(${JSON.stringify(acquisition)}, {
onProgress(message) {
if (!message.startsWith("Opening sign-in: ")) return;
const authorize = new URL(message.slice("Opening sign-in: ".length));
- const callback = new URL(authorize.searchParams.get("redirect_uri"));
- callback.searchParams.set("state", authorize.searchParams.get("state"));
+ const state = authorize.searchParams.get("state");
+ const callback = new URL("http://127.0.0.1:" + state.slice(state.lastIndexOf(".") + 1) + "/callback");
+ callback.searchParams.set("state", state);
callback.searchParams.set("code", "manual-code");
setTimeout(() => { void fetch(callback); }, 50);
},
diff --git a/packages/provider-hypihub/src/provider.ts b/packages/provider-hypihub/src/provider.ts
index d151121c..298da480 100644
--- a/packages/provider-hypihub/src/provider.ts
+++ b/packages/provider-hypihub/src/provider.ts
@@ -607,6 +607,7 @@ export function createHypiHubProvider(options: CreateHypiHubProviderOptions = {}
acquisition: {
kind: "oauth2-pkce",
authorizationEndpoint: new URL("/oauth/consent", oauthOrigin).toString(),
+ redirectUri: new URL("/oauth/callback", oauthOrigin).toString(),
tokenEndpoint: new URL("/oauth/token", oauthOrigin).toString(),
clientId: "hyc_d5d5e8e7131b0c877756e66c",
scopes: ["user:profile", "user:inference"],
diff --git a/packages/runtime/src/credentials.ts b/packages/runtime/src/credentials.ts
index 81eeff5e..68f9c6cc 100644
--- a/packages/runtime/src/credentials.ts
+++ b/packages/runtime/src/credentials.ts
@@ -43,6 +43,8 @@ export function decodeOAuth2Credential(secret: string): OAuth2Credential | undef
export type CredentialAcquisition = {
readonly kind: "oauth2-pkce";
readonly authorizationEndpoint: string;
+ /** Service-hosted page that shows the code and forwards it to the port packed into `state`. */
+ readonly redirectUri: string;
readonly tokenEndpoint: string;
readonly clientId: string;
readonly scopes: readonly string[];
diff --git a/skills/hypit/references/environment/model-and-provider.md b/skills/hypit/references/environment/model-and-provider.md
index 9f4dac30..c4ae335c 100644
--- a/skills/hypit/references/environment/model-and-provider.md
+++ b/skills/hypit/references/environment/model-and-provider.md
@@ -108,9 +108,16 @@ For the selected account:
hypit auth login hypihub.default
```
-The Endpoint opens its browser authorization flow and the CLI reports completion after the credential
-is stored. That connection can serve the supported models; login is not repeated per Model. This is
-the integrated account setup, not a promise to install local tools or make every model free.
+Run it as a background task and send the user the URL after `Opening sign-in:` in its output. Ask
+them to authorize and, if the login does not finish on its own, to send back the `<code>#<state>`
+shown on the callback page. The login is done when the CLI reports the credential stored. If the
+user sends `<code>#<state>` while the login is still waiting, deliver it with
+`curl "http://127.0.0.1:<port>/callback?code=<code>&state=<state>"`, where `<port>` is the digits
+after the last `.` in `<state>`. The code works once and only for the waiting login process, so
+passing it through the conversation does not expose the credential.
+
+That connection can serve the supported models; login is not repeated per Model. This is the
+integrated account setup, not a promise to install local tools or make every model free.
A callback page alone does not prove the CLI finished storing the credential.
To use a HypiHub static API key instead, explicitly import a private file supplied through a secure