clawnify 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,9 +29,10 @@ import http from "http";
29
29
  import readline from "readline";
30
30
  import open from "open";
31
31
  var CALLBACK_PORT = 19823;
32
- var REDIRECT_URI = "https://app.clawnify.com/oauth/cli";
32
+ var LOCAL_REDIRECT_URI = `http://localhost:${CALLBACK_PORT}/callback`;
33
+ var HOSTED_REDIRECT_URI = "https://app.clawnify.com/oauth/cli";
33
34
  var WEB_ORIGIN = "https://app.clawnify.com";
34
- function waitForCode(state) {
35
+ function waitForCode(stateLocal, stateRemote) {
35
36
  return new Promise((resolve, reject) => {
36
37
  let done = false;
37
38
  let rl = null;
@@ -54,26 +55,43 @@ function waitForCode(state) {
54
55
  }
55
56
  const code = url.searchParams.get("code");
56
57
  const returnedState = url.searchParams.get("state");
57
- if (!code || returnedState !== state) {
58
- res.writeHead(400, { ...corsHeaders, "Content-Type": "application/json" });
59
- res.end(JSON.stringify({ ok: false }));
58
+ const error = url.searchParams.get("error");
59
+ if (returnedState === stateLocal) {
60
+ if (error || !code) {
61
+ res.writeHead(200, { "Content-Type": "text/html" });
62
+ res.end(
63
+ "<html><body><h2>Authorization denied.</h2><p>You can close this window.</p></body></html>"
64
+ );
65
+ finish(new Error(`Authorization denied: ${error ?? "no code returned"}`));
66
+ return;
67
+ }
68
+ res.writeHead(200, { "Content-Type": "text/html" });
69
+ res.end(
70
+ "<html><body><h2>Logged in!</h2><p>You can close this window and return to the terminal.</p></body></html>"
71
+ );
72
+ finish(null, { code, redirectUri: LOCAL_REDIRECT_URI });
60
73
  return;
61
74
  }
62
- res.writeHead(200, { ...corsHeaders, "Content-Type": "application/json" });
63
- res.end(JSON.stringify({ ok: true }));
64
- finish(null, code);
75
+ if (returnedState === stateRemote && code) {
76
+ res.writeHead(200, { ...corsHeaders, "Content-Type": "application/json" });
77
+ res.end(JSON.stringify({ ok: true }));
78
+ finish(null, { code, redirectUri: HOSTED_REDIRECT_URI });
79
+ return;
80
+ }
81
+ res.writeHead(400, { ...corsHeaders, "Content-Type": "application/json" });
82
+ res.end(JSON.stringify({ ok: false }));
65
83
  });
66
84
  const timeout = setTimeout(() => {
67
85
  finish(new Error("Timed out waiting for authorization."));
68
86
  }, 5 * 60 * 1e3);
69
- function finish(err, code) {
87
+ function finish(err, result) {
70
88
  if (done) return;
71
89
  done = true;
72
90
  clearTimeout(timeout);
73
91
  server.close();
74
92
  rl?.close();
75
93
  if (err) reject(err);
76
- else resolve(code);
94
+ else resolve(result);
77
95
  }
78
96
  server.on("error", () => {
79
97
  });
@@ -89,32 +107,38 @@ function waitForCode(state) {
89
107
  return;
90
108
  }
91
109
  const [code, pastedState] = input.split("#");
92
- if (!code || pastedState !== state) {
110
+ if (!code || pastedState !== stateRemote) {
93
111
  finish(new Error("Invalid code. Run `clawnify login` to try again."));
94
112
  return;
95
113
  }
96
- finish(null, code);
114
+ finish(null, { code, redirectUri: HOSTED_REDIRECT_URI });
97
115
  });
98
116
  });
99
117
  }
100
118
  async function loginCommand() {
101
119
  const { codeVerifier: verifier, codeChallenge: challenge } = generatePkce();
102
- const state = crypto.randomBytes(16).toString("hex");
103
- const authUrl = new URL(OAUTH_AUTHORIZE_URL);
104
- authUrl.searchParams.set("response_type", "code");
105
- authUrl.searchParams.set("client_id", OAUTH_CLIENT_ID);
106
- authUrl.searchParams.set("redirect_uri", REDIRECT_URI);
107
- authUrl.searchParams.set("code_challenge", challenge);
108
- authUrl.searchParams.set("code_challenge_method", "S256");
109
- authUrl.searchParams.set("scope", "openid email");
110
- authUrl.searchParams.set("state", state);
120
+ const stateLocal = crypto.randomBytes(16).toString("hex");
121
+ const stateRemote = crypto.randomBytes(16).toString("hex");
122
+ const buildAuthUrl = (redirectUri2, state) => {
123
+ const url = new URL(OAUTH_AUTHORIZE_URL);
124
+ url.searchParams.set("response_type", "code");
125
+ url.searchParams.set("client_id", OAUTH_CLIENT_ID);
126
+ url.searchParams.set("redirect_uri", redirectUri2);
127
+ url.searchParams.set("code_challenge", challenge);
128
+ url.searchParams.set("code_challenge_method", "S256");
129
+ url.searchParams.set("scope", "openid email");
130
+ url.searchParams.set("state", state);
131
+ return url.toString();
132
+ };
133
+ const localAuthUrl = buildAuthUrl(LOCAL_REDIRECT_URI, stateLocal);
134
+ const hostedAuthUrl = buildAuthUrl(HOSTED_REDIRECT_URI, stateRemote);
111
135
  console.log("Opening browser to authorize...");
112
136
  console.log("\nBrowser didn't open? Use the url below to sign in:\n");
113
- console.log(` ${authUrl.toString()}
137
+ console.log(` ${hostedAuthUrl}
114
138
  `);
115
- open(authUrl.toString()).catch(() => {
139
+ open(localAuthUrl).catch(() => {
116
140
  });
117
- const code = await waitForCode(state);
141
+ const { code, redirectUri } = await waitForCode(stateLocal, stateRemote);
118
142
  const tokenRes = await fetch(OAUTH_TOKEN_URL, {
119
143
  method: "POST",
120
144
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
@@ -122,7 +146,7 @@ async function loginCommand() {
122
146
  grant_type: "authorization_code",
123
147
  code,
124
148
  client_id: OAUTH_CLIENT_ID,
125
- redirect_uri: REDIRECT_URI,
149
+ redirect_uri: redirectUri,
126
150
  code_verifier: verifier
127
151
  })
128
152
  });
@@ -846,7 +870,7 @@ function agentByName(name) {
846
870
  var ALL_AGENTS = DETECTORS.map((d) => d.agent);
847
871
 
848
872
  // src/lib/skills/content.gen.ts
849
- var CLI_VERSION = "0.9.0";
873
+ var CLI_VERSION = "0.9.1";
850
874
  var BUNDLED_SKILLS = [
851
875
  {
852
876
  name: "build-a-clawnify-app",
@@ -2905,4 +2929,4 @@ async function runCli() {
2905
2929
  export {
2906
2930
  runCli
2907
2931
  };
2908
- //# sourceMappingURL=cli-LGYRXYUU.js.map
2932
+ //# sourceMappingURL=cli-K7BINKS7.js.map