@vatio-ai/cli 0.61.0 → 0.62.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.
package/README.md CHANGED
@@ -70,6 +70,7 @@ kb write|cat|rm-entry Entries, from a file or stdin
70
70
  secrets list|set|rm Credentials your tools read
71
71
  tokens list|create|origins|revoke Publishable tokens and the origins each accepts
72
72
  auth --new-key Keypair that signs the visitor's JWT
73
+ auth --supabase [URL] Your users sign in with Supabase: no keys, no code
73
74
 
74
75
  whatsapp | instagram Set up in the console; prints the link
75
76
 
@@ -65,6 +65,11 @@ export class ApiClient extends HttpClient {
65
65
  return this.delete(`/env/${encode(name)}`);
66
66
  }
67
67
 
68
+ // The project "Connect Supabase" picked in the console, if any.
69
+ supabase() {
70
+ return this.get("/supabase");
71
+ }
72
+
68
73
  // --- secrets --------------------------------------------------------
69
74
 
70
75
  // Without an environment, the value everywhere; with one, the value only
@@ -11,13 +11,18 @@ import { execFileSync } from "node:child_process";
11
11
  import { existsSync, writeFileSync, readFileSync, unlinkSync } from "node:fs";
12
12
  import { join } from "node:path";
13
13
  import { fail } from "../support.mjs";
14
+ import { supabaseAuth } from "./supabase-auth.mjs";
14
15
 
15
16
  const PUBLIC_FILE = "identity.pub";
16
17
  const PRIVATE_FILE = "identity.pem";
17
18
 
18
19
  export async function auth(config, args) {
20
+ if (args.includes("--supabase")) return supabaseAuth(config, args);
19
21
  if (!args.includes("--new-key")) {
20
- fail("Usage: vatio auth --new-key (generates the keypair that signs your JWT)");
22
+ fail(
23
+ "Usage: vatio auth --new-key (generates the keypair that signs your JWT)\n" +
24
+ " vatio auth --supabase (your users sign in with Supabase: no keys, no code)"
25
+ );
21
26
  }
22
27
 
23
28
  const root = config.workspaceRootRequired();
@@ -0,0 +1,226 @@
1
+ // `vatio auth --supabase` — signed-in users for an app whose users already sign
2
+ // in with Supabase Auth, with no keypair and no signing code.
3
+ //
4
+ // The project is found where the app already keeps it (a .env, or the client
5
+ // Lovable generates), checked for the asymmetric signing keys Vatio verifies
6
+ // with, and written into vatio.yml. Its URL and public anon key go into the
7
+ // workspace secrets so a tool can call the project's REST API as the visitor,
8
+ // with their own Row Level Security, in two headers.
9
+
10
+ import { existsSync, readFileSync, writeFileSync } from "node:fs";
11
+ import { dirname, join, resolve } from "node:path";
12
+ import { parseDocument } from "yaml";
13
+ import { deployClient } from "./deploy.mjs";
14
+ import { fail, paint, takeValue } from "../support.mjs";
15
+
16
+ const PROJECT_URL = /^https:\/\/[a-z0-9]+\.supabase\.co$/;
17
+ const URL_IN_SOURCE = /https:\/\/[a-z0-9]+\.supabase\.co/;
18
+ const URL_VARIABLE = /^\s*(?:export\s+)?[A-Z_]*SUPABASE_URL\s*=\s*["']?(https:\/\/[a-z0-9]+\.supabase\.co)/m;
19
+ // Anon or publishable only: a service-role or secret key would let whoever
20
+ // holds it read past every policy, and it never belongs to a widget's tools.
21
+ const KEY_VARIABLE = /^\s*(?:export\s+|const\s+)?[A-Z_]*SUPABASE_(?:ANON|PUBLISHABLE)[A-Z_]*KEY\s*[=:]\s*["']?([A-Za-z0-9._-]{20,})/m;
22
+ const PLACES = [
23
+ ".env",
24
+ ".env.local",
25
+ ".env.development",
26
+ ".env.development.local",
27
+ ".env.production",
28
+ "src/integrations/supabase/client.ts",
29
+ "src/integrations/supabase/client.js"
30
+ ];
31
+
32
+ export async function supabaseAuth(config, args) {
33
+ const root = config.workspaceRootRequired();
34
+ const workspace = config.resolveWorkspaceRequired();
35
+ const signInUrl = takeValue(args, "--sign-in-url");
36
+ const force = args.includes("--force");
37
+ const skipSecrets = args.includes("--no-secrets");
38
+ const index = args.indexOf("--supabase");
39
+ const given = args[index + 1]?.startsWith("https://") ? args[index + 1] : null;
40
+
41
+ if (signInUrl && !/^https:\/\/[^/\s?#]+/.test(signInUrl)) fail("--sign-in-url must be an https url on your own site");
42
+
43
+ // The console's "Connect Supabase" is the one explicit answer short of a URL
44
+ // on the command line; a .env is a guess about which project the app uses.
45
+ const connected = given ? null : await connectedProject(config, workspace);
46
+ const found = connected ? {} : detect([process.cwd(), root]);
47
+ const url = normalize(given ?? connected?.url ?? found.url);
48
+ if (!url) {
49
+ fail(
50
+ "Could not find your Supabase project URL in a .env or src/integrations/supabase/client.ts.\n" +
51
+ "Pass it: vatio auth --supabase https://<project-ref>.supabase.co\n" +
52
+ "or connect Supabase in the console (Channels)."
53
+ );
54
+ }
55
+ if (!PROJECT_URL.test(url)) {
56
+ fail(`${url} is not a Supabase project URL (https://<project-ref>.supabase.co). Custom domains are not supported yet.`);
57
+ }
58
+
59
+ await checkSigningKeys(url);
60
+ writeManifest(config.manifestPath(), url, signInUrl, force);
61
+
62
+ console.log(`${paint("green", "✓")} vatio.yml now verifies visitors with ${url}`);
63
+ if (connected) console.log(` (the project connected in the console: ${connected.name})`);
64
+ else if (found.from && !given) console.log(` (found in ${found.from})`);
65
+
66
+ if (connected) console.log(" SUPABASE_URL and SUPABASE_ANON_KEY were set when it was connected.");
67
+ else if (!skipSecrets) await storeSecrets(config, workspace, url, found.key);
68
+
69
+ console.log("");
70
+ console.log("What happens after `vatio push` and `vatio publish`:");
71
+ console.log(" • Web: the widget reads the Supabase session already on your page. Nothing to add —");
72
+ console.log(" signed in, the visitor is known; signed out, they are anonymous.");
73
+ if (signInUrl || existingSignInUrl(config.manifestPath())) {
74
+ console.log(" • WhatsApp and Instagram: the Sign in button opens your sign_in_url. Keep the widget");
75
+ console.log(" script on that page: it waits for the person to sign in and hands the session over.");
76
+ } else {
77
+ console.log(" • WhatsApp and Instagram: add your login page, with the widget script on it —");
78
+ console.log(" vatio auth --supabase --sign-in-url https://your-app.com/login");
79
+ }
80
+ console.log("");
81
+ console.log("A private tool reads the visitor's own rows, under your Row Level Security:");
82
+ console.log("");
83
+ console.log(" # tools/my_orders.yml");
84
+ console.log(" access: private");
85
+ console.log(" request:");
86
+ console.log(" method: GET");
87
+ console.log(" base_url: $env.SUPABASE_URL");
88
+ console.log(" path: /rest/v1/orders");
89
+ console.log(" query: { select: \"*\" }");
90
+ console.log(" headers:");
91
+ console.log(" apikey: $env.SUPABASE_ANON_KEY");
92
+ console.log(" Authorization: Bearer $auth.token");
93
+ console.log(" respond:");
94
+ console.log(" data: { orders: \"$\" }");
95
+ console.log(" message: { when_empty: No orders yet., default: \"Found {{count}} orders.\" }");
96
+ console.log("");
97
+ console.log("Docs: https://docs.vatio.ai/authentication/supabase");
98
+ }
99
+
100
+ async function connectedProject(config, workspace) {
101
+ if (!config.resolveBaseUrl() || !config.resolveToken()) return null;
102
+ try {
103
+ return (await deployClient(config, workspace).supabase())?.project ?? null;
104
+ } catch {
105
+ // An older platform, or no network: the .env is still there to read.
106
+ return null;
107
+ }
108
+ }
109
+
110
+ // Where the app already says which project it uses: the nearest directory, from
111
+ // here and from the workspace root upwards, that names one.
112
+ function detect(starts) {
113
+ const seen = new Set();
114
+ for (const start of starts) {
115
+ let dir = resolve(start);
116
+ for (let depth = 0; depth < 4; depth += 1) {
117
+ if (!seen.has(dir)) {
118
+ seen.add(dir);
119
+ const found = detectIn(dir);
120
+ if (found.url) return found;
121
+ }
122
+ const parent = dirname(dir);
123
+ if (parent === dir) break;
124
+ dir = parent;
125
+ }
126
+ }
127
+ return {};
128
+ }
129
+
130
+ function detectIn(dir) {
131
+ let url = null;
132
+ let key = null;
133
+ let from = null;
134
+ for (const place of PLACES) {
135
+ const path = join(dir, place);
136
+ if (!existsSync(path)) continue;
137
+ const text = readFileSync(path, "utf8");
138
+ const foundUrl = text.match(URL_VARIABLE)?.[1] ?? (place.startsWith("src/") ? text.match(URL_IN_SOURCE)?.[0] : null);
139
+ if (!url && foundUrl) {
140
+ url = foundUrl;
141
+ from = path;
142
+ }
143
+ const foundKey = text.match(KEY_VARIABLE)?.[1];
144
+ if (!key && foundKey && publicKey(foundKey)) key = foundKey;
145
+ }
146
+ return { url, key, from };
147
+ }
148
+
149
+ // The anon JWT (role "anon") or a publishable key; never a secret or
150
+ // service-role one, whatever the variable was called.
151
+ function publicKey(value) {
152
+ if (value.startsWith("sb_publishable_")) return true;
153
+ if (value.startsWith("sb_secret_")) return false;
154
+ try {
155
+ const payload = JSON.parse(Buffer.from(value.split(".")[1], "base64url").toString("utf8"));
156
+ return payload.role === "anon";
157
+ } catch {
158
+ return false;
159
+ }
160
+ }
161
+
162
+ function normalize(value) {
163
+ return value ? value.trim().toLowerCase().replace(/\/+$/, "") : null;
164
+ }
165
+
166
+ async function checkSigningKeys(url) {
167
+ let keys;
168
+ try {
169
+ const response = await fetch(`${url}/auth/v1/.well-known/jwks.json`, { signal: AbortSignal.timeout(8000) });
170
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
171
+ keys = (await response.json()).keys ?? [];
172
+ } catch (error) {
173
+ console.warn(`${paint("yellow", "!")} Could not reach ${url} to check its signing keys (${error.message}); the deploy checks again.`);
174
+ return;
175
+ }
176
+ if (keys.length > 0) return;
177
+
178
+ fail(
179
+ `${url} still signs with the legacy JWT secret (HS256), which Vatio cannot verify without being able\n` +
180
+ "to mint tokens too. In the Supabase dashboard: Project Settings → JWT Keys → migrate, then rotate\n" +
181
+ "to an ES256 key. Your app keeps working through the switch. Then run this again."
182
+ );
183
+ }
184
+
185
+ function writeManifest(path, url, signInUrl, force) {
186
+ const doc = parseDocument(readFileSync(path, "utf8"));
187
+ if (doc.hasIn(["auth", "public_key"]) && !force) {
188
+ fail(
189
+ "vatio.yml already verifies a JWT your backend signs (auth.public_key). Switching to Supabase signs out\n" +
190
+ "every visitor holding one of those — re-run with --force if that is what you want."
191
+ );
192
+ }
193
+ for (const key of [ "public_key", "algorithm" ]) {
194
+ if (doc.hasIn(["auth", key])) doc.deleteIn(["auth", key]);
195
+ }
196
+ doc.setIn(["auth", "supabase"], url);
197
+ if (signInUrl) doc.setIn(["auth", "sign_in_url"], signInUrl);
198
+ writeFileSync(path, String(doc));
199
+ }
200
+
201
+ function existingSignInUrl(path) {
202
+ return parseDocument(readFileSync(path, "utf8")).hasIn(["auth", "sign_in_url"]);
203
+ }
204
+
205
+ async function storeSecrets(config, workspace, url, key) {
206
+ const commands = [ `vatio secrets set SUPABASE_URL ${url}` ];
207
+ if (key) commands.push(`vatio secrets set SUPABASE_ANON_KEY ${key}`);
208
+ else commands.push("vatio secrets set SUPABASE_ANON_KEY <your anon or publishable key>");
209
+
210
+ if (!config.resolveBaseUrl() || !config.resolveToken()) {
211
+ console.log("Not logged in, so the secrets your tools use are not set yet. After `vatio login`:");
212
+ for (const command of commands) console.log(` ${command}`);
213
+ return;
214
+ }
215
+
216
+ try {
217
+ const client = deployClient(config, workspace);
218
+ await client.upsertSecret({ key: "SUPABASE_URL", value: url, environment: null });
219
+ if (key) await client.upsertSecret({ key: "SUPABASE_ANON_KEY", value: key, environment: null });
220
+ console.log(`${paint("green", "✓")} Set secret SUPABASE_URL${key ? " and SUPABASE_ANON_KEY" : ""} on workspace ${workspace}`);
221
+ if (!key) console.log(` Add the anon key yourself: ${commands[1]}`);
222
+ } catch (error) {
223
+ console.warn(`${paint("yellow", "!")} Could not set the secrets (${error.message}). Run:`);
224
+ for (const command of commands) console.warn(` ${command}`);
225
+ }
226
+ }
package/lib/help.mjs CHANGED
@@ -95,6 +95,9 @@ and the one its pull request deploys to. Elsewhere commands default as listed.
95
95
  keys list|create [--env NAME] [--label NAME]|revoke PREFIX
96
96
  Server keys, for your backend to call the server API
97
97
  auth --new-key Keypair that signs the JWT saying who a visitor is
98
+ auth --supabase [URL] [--sign-in-url URL]
99
+ Your users sign in with Supabase: verify their
100
+ session, no keys or signing code
98
101
 
99
102
  Sending from your backend (WhatsApp, with a server key):
100
103
  templates list [--env live] [--json]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vatio-ai/cli",
3
- "version": "0.61.0",
3
+ "version": "0.62.0",
4
4
  "description": "Vatio CLI — deploy and manage Vatio agent workspaces",
5
5
  "keywords": [
6
6
  "vatio",