clawnify 0.6.1 → 0.7.0

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.
@@ -28,95 +28,109 @@ import crypto from "crypto";
28
28
  import http from "http";
29
29
  import readline from "readline";
30
30
  import open from "open";
31
- async function loginCommand() {
32
- const { codeVerifier: verifier, codeChallenge: challenge } = generatePkce();
33
- const state = crypto.randomBytes(16).toString("hex");
34
- const port = 19823;
35
- const redirectUri = `http://localhost:${port}/callback`;
36
- const authUrl = new URL(OAUTH_AUTHORIZE_URL);
37
- authUrl.searchParams.set("response_type", "code");
38
- authUrl.searchParams.set("client_id", OAUTH_CLIENT_ID);
39
- authUrl.searchParams.set("redirect_uri", redirectUri);
40
- authUrl.searchParams.set("code_challenge", challenge);
41
- authUrl.searchParams.set("code_challenge_method", "S256");
42
- authUrl.searchParams.set("scope", "openid email");
43
- authUrl.searchParams.set("state", state);
44
- const result = await new Promise((resolve, reject) => {
45
- const timeout = setTimeout(() => {
46
- server.close();
47
- reject(new Error("Timed out waiting for authorization."));
48
- }, 5 * 60 * 1e3);
49
- const server = http.createServer(async (req, res) => {
50
- const url = new URL(req.url, `http://localhost:${port}`);
31
+ var CALLBACK_PORT = 19823;
32
+ var REDIRECT_URI = "https://app.clawnify.com/oauth/cli";
33
+ var WEB_ORIGIN = "https://app.clawnify.com";
34
+ function waitForCode(state) {
35
+ return new Promise((resolve, reject) => {
36
+ let done = false;
37
+ let rl = null;
38
+ const server = http.createServer((req, res) => {
39
+ const url = new URL(req.url, `http://127.0.0.1:${CALLBACK_PORT}`);
40
+ const corsHeaders = {
41
+ "Access-Control-Allow-Origin": WEB_ORIGIN,
42
+ "Access-Control-Allow-Methods": "GET, OPTIONS",
43
+ "Access-Control-Allow-Private-Network": "true"
44
+ };
45
+ if (req.method === "OPTIONS") {
46
+ res.writeHead(204, corsHeaders);
47
+ res.end();
48
+ return;
49
+ }
51
50
  if (url.pathname !== "/callback") {
52
- res.writeHead(404);
51
+ res.writeHead(404, corsHeaders);
53
52
  res.end("Not found");
54
53
  return;
55
54
  }
56
55
  const code = url.searchParams.get("code");
57
56
  const returnedState = url.searchParams.get("state");
58
- const error = url.searchParams.get("error");
59
- if (error) {
60
- res.writeHead(200, { "Content-Type": "text/html" });
61
- res.end(
62
- "<html><body><h2>Authorization denied.</h2><p>You can close this window.</p></body></html>"
63
- );
64
- clearTimeout(timeout);
65
- server.close();
66
- reject(new Error(`Authorization denied: ${error}`));
57
+ if (!code || returnedState !== state) {
58
+ res.writeHead(400, { ...corsHeaders, "Content-Type": "application/json" });
59
+ res.end(JSON.stringify({ ok: false }));
67
60
  return;
68
61
  }
69
- if (!code || returnedState !== state) {
70
- res.writeHead(400, { "Content-Type": "text/html" });
71
- res.end(
72
- "<html><body><h2>Invalid callback.</h2></body></html>"
73
- );
62
+ res.writeHead(200, { ...corsHeaders, "Content-Type": "application/json" });
63
+ res.end(JSON.stringify({ ok: true }));
64
+ finish(null, code);
65
+ });
66
+ const timeout = setTimeout(() => {
67
+ finish(new Error("Timed out waiting for authorization."));
68
+ }, 5 * 60 * 1e3);
69
+ function finish(err, code) {
70
+ if (done) return;
71
+ done = true;
72
+ clearTimeout(timeout);
73
+ server.close();
74
+ rl?.close();
75
+ if (err) reject(err);
76
+ else resolve(code);
77
+ }
78
+ server.on("error", () => {
79
+ });
80
+ server.listen(CALLBACK_PORT);
81
+ rl = readline.createInterface({
82
+ input: process.stdin,
83
+ output: process.stdout
84
+ });
85
+ rl.question("Paste code here if prompted > ", (answer) => {
86
+ const input = answer.trim();
87
+ if (!input) {
88
+ finish(new Error("No code provided."));
74
89
  return;
75
90
  }
76
- try {
77
- const tokenRes = await fetch(OAUTH_TOKEN_URL, {
78
- method: "POST",
79
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
80
- body: new URLSearchParams({
81
- grant_type: "authorization_code",
82
- code,
83
- client_id: OAUTH_CLIENT_ID,
84
- redirect_uri: redirectUri,
85
- code_verifier: verifier
86
- })
87
- });
88
- if (!tokenRes.ok) {
89
- const err = await tokenRes.text();
90
- throw new Error(`Token exchange failed: ${err}`);
91
- }
92
- const tokens = await tokenRes.json();
93
- res.writeHead(200, { "Content-Type": "text/html" });
94
- res.end(
95
- "<html><body><h2>Logged in!</h2><p>You can close this window and return to the terminal.</p></body></html>"
96
- );
97
- clearTimeout(timeout);
98
- server.close();
99
- resolve(tokens);
100
- } catch (err) {
101
- res.writeHead(500, { "Content-Type": "text/html" });
102
- res.end(
103
- "<html><body><h2>Login failed.</h2></body></html>"
104
- );
105
- clearTimeout(timeout);
106
- server.close();
107
- reject(err);
91
+ const [code, pastedState] = input.split("#");
92
+ if (!code || pastedState !== state) {
93
+ finish(new Error("Invalid code. Run `clawnify login` to try again."));
94
+ return;
108
95
  }
96
+ finish(null, code);
109
97
  });
110
- server.listen(port, () => {
111
- console.log("Opening browser to authorize...");
112
- console.log("\nBrowser didn't open? Use the url below to sign in:\n");
113
- console.log(` ${authUrl.toString()}
98
+ });
99
+ }
100
+ async function loginCommand() {
101
+ 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);
111
+ console.log("Opening browser to authorize...");
112
+ console.log("\nBrowser didn't open? Use the url below to sign in:\n");
113
+ console.log(` ${authUrl.toString()}
114
114
  `);
115
- open(authUrl.toString()).catch(() => {
116
- });
117
- console.log("Waiting for authorization...");
118
- });
115
+ open(authUrl.toString()).catch(() => {
116
+ });
117
+ const code = await waitForCode(state);
118
+ const tokenRes = await fetch(OAUTH_TOKEN_URL, {
119
+ method: "POST",
120
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
121
+ body: new URLSearchParams({
122
+ grant_type: "authorization_code",
123
+ code,
124
+ client_id: OAUTH_CLIENT_ID,
125
+ redirect_uri: REDIRECT_URI,
126
+ code_verifier: verifier
127
+ })
119
128
  });
129
+ if (!tokenRes.ok) {
130
+ const err = await tokenRes.text();
131
+ throw new Error(`Token exchange failed: ${err}`);
132
+ }
133
+ const result = await tokenRes.json();
120
134
  let email = "unknown";
121
135
  try {
122
136
  const payload = JSON.parse(
@@ -874,7 +888,7 @@ function agentByName(name) {
874
888
  var ALL_AGENTS = DETECTORS.map((d) => d.agent);
875
889
 
876
890
  // src/lib/skills/content.gen.ts
877
- var CLI_VERSION = "0.6.1";
891
+ var CLI_VERSION = "0.7.0";
878
892
  var BUNDLED_SKILLS = [
879
893
  {
880
894
  name: "build-a-clawnify-app",
@@ -2883,4 +2897,4 @@ async function runCli() {
2883
2897
  export {
2884
2898
  runCli
2885
2899
  };
2886
- //# sourceMappingURL=cli-OQPC6TYM.js.map
2900
+ //# sourceMappingURL=cli-KCTCHVPG.js.map