@marktiderman/genesis-supabase 0.7.0 → 0.8.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.
@@ -0,0 +1,270 @@
1
+ -- Genesis web handoff: continue on the web, already signed in
2
+ --
3
+ -- The database half of `@marktiderman/genesis-supabase/web-handoff/server`. A native app asks
4
+ -- the server for a link to a web page; the link opens that page signed in as the same person.
5
+ -- Each link is a row in private.genesis_web_handoffs, which keeps SHA-256 digests only, never a
6
+ -- token, so a copy of the table signs nobody in. A row moves through:
7
+ --
8
+ -- issued the app asked (genesis_web_handoff_issue): person, path, a 60-second expiry, the
9
+ -- app session it came from. At most `max` a person per window (SQLSTATE GS429),
10
+ -- counted under a per-person lock.
11
+ -- claimed the page opened it (genesis_web_handoff_claim), once and unexpired, and got a
12
+ -- continue nonce that lives 30 seconds. Nothing that signs anyone in exists yet.
13
+ -- continued the person chose Continue (genesis_web_handoff_continue), once, within the
14
+ -- nonce's life; only now does the server ask Supabase Auth for a sign-in token.
15
+ -- discarded the person chose Cancel (genesis_web_handoff_discard): the nonce is burnt.
16
+ -- bound the page reported the session it made (genesis_web_handoff_bind_session), so
17
+ -- signing out of the app can end exactly that web session
18
+ -- (genesis_web_handoff_revoke_sessions, which deletes those auth.sessions rows:
19
+ -- Supabase documents a session as signed out when its row is gone; its access
20
+ -- token still works until it expires).
21
+ --
22
+ -- Rows are also the log of handoffs (who, which path, when), kept 30 days, or longer while the
23
+ -- web session they made still exists.
24
+ --
25
+ -- Access: only service_role, through the functions below, granted by name. anon and
26
+ -- authenticated reach nothing: the table is in `private`, has row level security with no
27
+ -- policies, and every function also refuses any role but service_role
28
+ -- (`current_setting('role')`, which is what PostgREST set, not a JWT claim).
29
+ --
30
+ -- Account deletion: a person's handoffs go with their login (on delete cascade).
31
+ --
32
+ -- Use: copy this file into your app's `supabase/migrations/` under a new timestamped name.
33
+ -- Never edit it once applied; change it with a new migration. It stands alone.
34
+ --
35
+ -- Verify: the pgTAP test beside this file (`tests/0015_genesis_web_handoff.test.sql`).
36
+
37
+ create schema if not exists private;
38
+
39
+ create table private.genesis_web_handoffs (
40
+ id uuid primary key default gen_random_uuid(),
41
+ user_id uuid not null references auth.users (id) on delete cascade,
42
+ token_digest text not null unique check (token_digest ~ '^[0-9a-f]{64}$'),
43
+ path text not null check (char_length(path) between 1 and 2048 and left(path, 1) = '/' and left(path, 2) <> '//'),
44
+ created_at timestamptz not null default now(),
45
+ expires_at timestamptz not null,
46
+ used_at timestamptz,
47
+ source_session_id uuid,
48
+ nonce_digest text unique check (nonce_digest ~ '^[0-9a-f]{64}$'),
49
+ nonce_expires_at timestamptz,
50
+ continued_at timestamptz,
51
+ discarded_at timestamptz,
52
+ web_session_id uuid,
53
+ check (expires_at > created_at)
54
+ );
55
+
56
+ comment on table private.genesis_web_handoffs is
57
+ 'Native-to-web sign-in handoffs (genesis-supabase web-handoff): digests, never tokens. Service role only.';
58
+
59
+ create index genesis_web_handoffs_user_recent on private.genesis_web_handoffs (user_id, created_at desc);
60
+ create index genesis_web_handoffs_created on private.genesis_web_handoffs (created_at);
61
+ create index genesis_web_handoffs_source on private.genesis_web_handoffs (user_id, source_session_id) where web_session_id is not null;
62
+
63
+ alter table private.genesis_web_handoffs enable row level security;
64
+ revoke all on table private.genesis_web_handoffs from public, anon, authenticated;
65
+
66
+ -- Issue ------------------------------------------------------------------------------
67
+
68
+ create or replace function public.genesis_web_handoff_issue(
69
+ p_user_id uuid,
70
+ p_digest text,
71
+ p_path text,
72
+ p_ttl_seconds integer default 60,
73
+ p_max integer default 10,
74
+ p_window_seconds integer default 60,
75
+ p_source_session_id uuid default null
76
+ )
77
+ returns timestamptz
78
+ language plpgsql
79
+ volatile
80
+ security definer
81
+ set search_path = ''
82
+ as $$
83
+ declare
84
+ recent integer;
85
+ expires timestamptz;
86
+ begin
87
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
88
+ raise exception 'genesis_web_handoff_issue is for the server only' using errcode = '42501';
89
+ end if;
90
+ if p_ttl_seconds is null or p_ttl_seconds not between 10 and 300 then
91
+ raise exception 'p_ttl_seconds must be from 10 to 300' using errcode = '22023';
92
+ end if;
93
+ if p_max is null or p_max < 1 or p_window_seconds is null or p_window_seconds < 1 then
94
+ raise exception 'p_max and p_window_seconds must be positive' using errcode = '22023';
95
+ end if;
96
+
97
+ -- One person's issues one at a time, so the count below can't be raced.
98
+ perform pg_advisory_xact_lock(hashtextextended('genesis_web_handoff:' || p_user_id::text, 0));
99
+
100
+ select count(*) into recent
101
+ from private.genesis_web_handoffs h
102
+ where h.user_id = p_user_id and h.created_at > now() - make_interval(secs => p_window_seconds);
103
+ if recent >= p_max then
104
+ raise exception 'too many web handoffs; try again in a minute' using errcode = 'GS429';
105
+ end if;
106
+
107
+ expires := now() + make_interval(secs => p_ttl_seconds);
108
+ insert into private.genesis_web_handoffs (user_id, token_digest, path, expires_at, source_session_id)
109
+ values (p_user_id, p_digest, p_path, expires, p_source_session_id);
110
+
111
+ -- The log keeps 30 days, and a row whose web session still exists until that session goes.
112
+ delete from private.genesis_web_handoffs h
113
+ where h.created_at < now() - interval '30 days'
114
+ and (h.web_session_id is null or not exists (select 1 from auth.sessions s where s.id = h.web_session_id));
115
+ return expires;
116
+ end;
117
+ $$;
118
+
119
+ revoke execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) from public, anon, authenticated;
120
+ grant execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) to service_role;
121
+
122
+ -- Claim: the page opened the link. Once, unexpired; hands out a short-lived continue nonce.
123
+
124
+ create or replace function public.genesis_web_handoff_claim(
125
+ p_digest text,
126
+ p_nonce_digest text,
127
+ p_nonce_ttl_seconds integer default 30
128
+ )
129
+ returns table (user_id uuid, path text)
130
+ language plpgsql
131
+ volatile
132
+ security definer
133
+ set search_path = ''
134
+ as $$
135
+ begin
136
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
137
+ raise exception 'genesis_web_handoff_claim is for the server only' using errcode = '42501';
138
+ end if;
139
+ if p_nonce_ttl_seconds is null or p_nonce_ttl_seconds not between 5 and 120 then
140
+ raise exception 'p_nonce_ttl_seconds must be from 5 to 120' using errcode = '22023';
141
+ end if;
142
+ return query
143
+ update private.genesis_web_handoffs h
144
+ set used_at = now(), nonce_digest = p_nonce_digest, nonce_expires_at = now() + make_interval(secs => p_nonce_ttl_seconds)
145
+ where h.token_digest = p_digest and h.used_at is null and h.expires_at > now()
146
+ returning h.user_id, h.path;
147
+ end;
148
+ $$;
149
+
150
+ revoke execute on function public.genesis_web_handoff_claim(text, text, integer) from public, anon, authenticated;
151
+ grant execute on function public.genesis_web_handoff_claim(text, text, integer) to service_role;
152
+
153
+ -- Continue: the person chose Continue. Once, within the nonce's life, not after Cancel.
154
+
155
+ create or replace function public.genesis_web_handoff_continue(p_nonce_digest text)
156
+ returns table (user_id uuid, path text)
157
+ language plpgsql
158
+ volatile
159
+ security definer
160
+ set search_path = ''
161
+ as $$
162
+ begin
163
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
164
+ raise exception 'genesis_web_handoff_continue is for the server only' using errcode = '42501';
165
+ end if;
166
+ return query
167
+ update private.genesis_web_handoffs h
168
+ set continued_at = now()
169
+ where h.nonce_digest = p_nonce_digest and h.continued_at is null and h.discarded_at is null
170
+ and h.nonce_expires_at > now()
171
+ returning h.user_id, h.path;
172
+ end;
173
+ $$;
174
+
175
+ revoke execute on function public.genesis_web_handoff_continue(text) from public, anon, authenticated;
176
+ grant execute on function public.genesis_web_handoff_continue(text) to service_role;
177
+
178
+ -- Discard: the person chose Cancel. Burns the nonce, so Continue is refused afterwards.
179
+
180
+ create or replace function public.genesis_web_handoff_discard(p_nonce_digest text)
181
+ returns boolean
182
+ language plpgsql
183
+ volatile
184
+ security definer
185
+ set search_path = ''
186
+ as $$
187
+ begin
188
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
189
+ raise exception 'genesis_web_handoff_discard is for the server only' using errcode = '42501';
190
+ end if;
191
+ update private.genesis_web_handoffs h
192
+ set discarded_at = now()
193
+ where h.nonce_digest = p_nonce_digest and h.continued_at is null and h.discarded_at is null;
194
+ return found;
195
+ end;
196
+ $$;
197
+
198
+ revoke execute on function public.genesis_web_handoff_discard(text) from public, anon, authenticated;
199
+ grant execute on function public.genesis_web_handoff_discard(text) to service_role;
200
+
201
+ -- Bind: the page reports the session Continue made (its id from the verified access token),
202
+ -- once, within two minutes of Continue, for the handoff's own person.
203
+
204
+ create or replace function public.genesis_web_handoff_bind_session(p_nonce_digest text, p_user_id uuid, p_session_id uuid)
205
+ returns boolean
206
+ language plpgsql
207
+ volatile
208
+ security definer
209
+ set search_path = ''
210
+ as $$
211
+ begin
212
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
213
+ raise exception 'genesis_web_handoff_bind_session is for the server only' using errcode = '42501';
214
+ end if;
215
+ update private.genesis_web_handoffs h
216
+ set web_session_id = p_session_id
217
+ where h.nonce_digest = p_nonce_digest and h.user_id = p_user_id and h.web_session_id is null
218
+ and h.continued_at > now() - interval '2 minutes'
219
+ and h.source_session_id is distinct from p_session_id;
220
+ return found;
221
+ end;
222
+ $$;
223
+
224
+ revoke execute on function public.genesis_web_handoff_bind_session(text, uuid, uuid) from public, anon, authenticated;
225
+ grant execute on function public.genesis_web_handoff_bind_session(text, uuid, uuid) to service_role;
226
+
227
+ -- Revoke: the app signs out. Ends exactly the web sessions handed off from that app session
228
+ -- (never the person's other devices), by deleting their auth.sessions rows.
229
+
230
+ create or replace function public.genesis_web_handoff_revoke_sessions(p_user_id uuid, p_source_session_id uuid)
231
+ returns integer
232
+ language plpgsql
233
+ volatile
234
+ security definer
235
+ set search_path = ''
236
+ as $$
237
+ declare
238
+ ended integer;
239
+ begin
240
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
241
+ raise exception 'genesis_web_handoff_revoke_sessions is for the server only' using errcode = '42501';
242
+ end if;
243
+ if p_user_id is null or p_source_session_id is null then
244
+ return 0;
245
+ end if;
246
+ with targets as (
247
+ select h.id, h.web_session_id
248
+ from private.genesis_web_handoffs h
249
+ where h.user_id = p_user_id and h.source_session_id = p_source_session_id
250
+ and h.web_session_id is not null and h.web_session_id <> p_source_session_id
251
+ for update
252
+ ), gone as (
253
+ delete from auth.sessions s
254
+ using targets t
255
+ where s.id = t.web_session_id and s.user_id = p_user_id
256
+ returning s.id
257
+ ), cleared as (
258
+ update private.genesis_web_handoffs h
259
+ set web_session_id = null
260
+ from targets t
261
+ where h.id = t.id
262
+ returning h.id
263
+ )
264
+ select count(*) into ended from gone;
265
+ return ended;
266
+ end;
267
+ $$;
268
+
269
+ revoke execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) from public, anon, authenticated;
270
+ grant execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) to service_role;
@@ -0,0 +1,203 @@
1
+ -- @feat:web-handoff
2
+ -- pgTAP test for 0015_genesis_web_handoff.sql.
3
+ -- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
4
+ -- It writes only its own users, sessions and handoffs and rolls everything back.
5
+ begin;
6
+ create extension if not exists pgtap with schema extensions;
7
+ select plan(38);
8
+
9
+ insert into auth.users (id, aud, role, email, instance_id)
10
+ select ('00000000-0000-4000-8000-0000000015' || n)::uuid, 'authenticated', 'authenticated', 'genesis-handoff-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
11
+ from unnest(array['a1', 'b2']) n;
12
+
13
+ create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-0000000015' || n)::uuid $$;
14
+ create function pg_temp.session(n int) returns uuid language sql immutable as $$ select ('00000000-0000-4000-9000-0000000150' || lpad(n::text, 2, '0'))::uuid $$;
15
+ create function pg_temp.digest(n int) returns text language sql immutable as $$ select repeat(to_hex(n % 16), 64) $$;
16
+ create function pg_temp.nonce(n int) returns text language sql immutable as $$ select repeat(chr(96 + n), 64) $$;
17
+ grant execute on function pg_temp.person(text), pg_temp.session(int), pg_temp.digest(int), pg_temp.nonce(int) to anon, authenticated, service_role;
18
+
19
+ -- a1's app session (1), the web session a handoff makes (2), and a1's other device (3).
20
+ insert into auth.sessions (id, user_id, created_at, updated_at)
21
+ values (pg_temp.session(1), pg_temp.person('a1'), now(), now()),
22
+ (pg_temp.session(2), pg_temp.person('a1'), now(), now()),
23
+ (pg_temp.session(3), pg_temp.person('a1'), now(), now());
24
+
25
+ -- Shape and access --------------------------------------------------------------------
26
+
27
+ select has_table('private', 'genesis_web_handoffs', 'the handoff table is in private');
28
+ select ok(
29
+ (select relrowsecurity from pg_class where oid = 'private.genesis_web_handoffs'::regclass),
30
+ 'row level security is on'
31
+ );
32
+ select ok(
33
+ not has_table_privilege('authenticated', 'private.genesis_web_handoffs', 'select')
34
+ and not has_table_privilege('anon', 'private.genesis_web_handoffs', 'select'),
35
+ 'anon and authenticated cannot read the table'
36
+ );
37
+ select ok(
38
+ not exists (
39
+ select 1
40
+ from unnest(array[
41
+ 'public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid)',
42
+ 'public.genesis_web_handoff_claim(text, text, integer)',
43
+ 'public.genesis_web_handoff_continue(text)',
44
+ 'public.genesis_web_handoff_discard(text)',
45
+ 'public.genesis_web_handoff_bind_session(text, uuid, uuid)',
46
+ 'public.genesis_web_handoff_revoke_sessions(uuid, uuid)'
47
+ ]) f
48
+ cross join unnest(array['anon', 'authenticated']) r
49
+ where has_function_privilege(r, f, 'execute')
50
+ ),
51
+ 'anon and authenticated can execute none of the functions'
52
+ );
53
+ select ok(
54
+ has_function_privilege('service_role', 'public.genesis_web_handoff_continue(text)', 'execute'),
55
+ 'service_role can'
56
+ );
57
+
58
+ -- Even granted, the functions refuse any role but service_role, whatever the JWT claims.
59
+ grant execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) to authenticated;
60
+ grant execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) to authenticated;
61
+ set local role authenticated;
62
+ select set_config('request.jwt.claims', json_build_object('sub', pg_temp.person('a1'), 'role', 'service_role')::text, true);
63
+ select throws_ok(
64
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(1), '/x') $$,
65
+ '42501', 'genesis_web_handoff_issue is for the server only',
66
+ 'issue: authenticated is refused even with a service_role claim in its JWT'
67
+ );
68
+ select throws_ok(
69
+ $$ select public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)) $$,
70
+ '42501', 'genesis_web_handoff_revoke_sessions is for the server only',
71
+ 'revoke: the same'
72
+ );
73
+ reset role;
74
+ revoke execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) from authenticated;
75
+ revoke execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) from authenticated;
76
+
77
+ -- Issue, claim, continue ---------------------------------------------------------------
78
+
79
+ set local role service_role;
80
+ -- (Through a subquery: BETWEEN would call the function twice.)
81
+ select ok(
82
+ (select e between now() + interval '59 seconds' and now() + interval '61 seconds'
83
+ from (select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(1), '/clients/1/forms', 60, 10, 60, pg_temp.session(1)) as e) issued),
84
+ 'a handoff expires in 60 seconds by default'
85
+ );
86
+ select results_eq(
87
+ $$ select user_id, path from public.genesis_web_handoff_claim(pg_temp.digest(1), pg_temp.nonce(1)) $$,
88
+ $$ values (pg_temp.person('a1'), '/clients/1/forms') $$,
89
+ 'claiming returns the person and path'
90
+ );
91
+ select is_empty(
92
+ $$ select * from public.genesis_web_handoff_claim(pg_temp.digest(1), pg_temp.nonce(2)) $$,
93
+ 'a link is claimed once'
94
+ );
95
+ select is_empty(
96
+ $$ select * from public.genesis_web_handoff_claim(pg_temp.digest(9), pg_temp.nonce(3)) $$,
97
+ 'an unknown link claims nothing'
98
+ );
99
+ select results_eq(
100
+ $$ select user_id, path from public.genesis_web_handoff_continue(pg_temp.nonce(1)) $$,
101
+ $$ values (pg_temp.person('a1'), '/clients/1/forms') $$,
102
+ 'Continue with the nonce returns the person and path'
103
+ );
104
+ select is_empty(
105
+ $$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(1)) $$,
106
+ 'a nonce continues once'
107
+ );
108
+ select ok(
109
+ not public.genesis_web_handoff_discard(pg_temp.nonce(1)),
110
+ 'a continued handoff cannot be discarded'
111
+ );
112
+
113
+ -- Bind and revoke.
114
+ select ok(
115
+ not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('b2'), pg_temp.session(2)),
116
+ 'bind refuses another person'
117
+ );
118
+ select ok(
119
+ not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(1)),
120
+ 'bind refuses the app session itself'
121
+ );
122
+ select ok(
123
+ public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(2)),
124
+ 'bind keeps the web session'
125
+ );
126
+ select ok(
127
+ not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(3)),
128
+ 'bind happens once'
129
+ );
130
+ select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('b2'), pg_temp.session(1)), 0, 'another person ends nothing');
131
+ select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(3)), 0, 'another app session ends nothing');
132
+ select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)), 1, 'the app session that handed off ends one web session');
133
+ reset role;
134
+ select is(
135
+ (select array_agg(id order by id) from auth.sessions where user_id = pg_temp.person('a1')),
136
+ array[pg_temp.session(1), pg_temp.session(3)],
137
+ 'only the handed-off web session is gone; the app and the other device stay'
138
+ );
139
+ set local role service_role;
140
+ select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)), 0, 'ending again ends nothing');
141
+
142
+ -- Cancel, and the nonce's life.
143
+ select lives_ok(
144
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(2), '/a') $$,
145
+ 'a second handoff'
146
+ );
147
+ select lives_ok($$ select * from public.genesis_web_handoff_claim(pg_temp.digest(2), pg_temp.nonce(4)) $$, 'claimed');
148
+ select ok(public.genesis_web_handoff_discard(pg_temp.nonce(4)), 'Cancel discards it');
149
+ select is_empty($$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(4)) $$, 'Continue after Cancel is refused');
150
+
151
+ select lives_ok(
152
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(3), '/a') $$,
153
+ 'a third handoff'
154
+ );
155
+ select lives_ok($$ select * from public.genesis_web_handoff_claim(pg_temp.digest(3), pg_temp.nonce(5), 5) $$, 'claimed with a 5-second nonce');
156
+ reset role;
157
+ update private.genesis_web_handoffs set nonce_expires_at = now() - interval '1 second' where nonce_digest = pg_temp.nonce(5);
158
+ set local role service_role;
159
+ select is_empty($$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(5)) $$, 'Continue after the nonce expired is refused');
160
+
161
+ -- Expired links, refusals and the rate limit (b2).
162
+ select lives_ok(
163
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(4), '/a', 10) $$,
164
+ 'a 10-second handoff is issued'
165
+ );
166
+ reset role;
167
+ update private.genesis_web_handoffs set created_at = now() - interval '1 minute', expires_at = now() - interval '1 second' where token_digest = pg_temp.digest(4);
168
+ set local role service_role;
169
+ select is_empty(
170
+ $$ select * from public.genesis_web_handoff_claim(pg_temp.digest(4), pg_temp.nonce(6)) $$,
171
+ 'an expired link claims nothing'
172
+ );
173
+ select throws_ok(
174
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(5), '/a', 301) $$,
175
+ '22023', null, 'a lifetime over 300 seconds is refused'
176
+ );
177
+ select throws_ok(
178
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(5), '//evil.example') $$,
179
+ '23514', null, 'a protocol-relative path is refused'
180
+ );
181
+ select lives_ok(
182
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(6), '/a', 60, 2, 60) $$,
183
+ 'first in the window'
184
+ );
185
+ select lives_ok(
186
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(7), '/a', 60, 2, 60) $$,
187
+ 'second in the window, at the limit'
188
+ );
189
+ select throws_ok(
190
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(8), '/a', 60, 2, 60) $$,
191
+ 'GS429', null, 'the third in the window is refused'
192
+ );
193
+ reset role;
194
+
195
+ -- Deleting the login deletes its handoffs.
196
+ delete from auth.users where id = pg_temp.person('b2');
197
+ select is(
198
+ (select count(*)::int from private.genesis_web_handoffs where user_id = pg_temp.person('b2')),
199
+ 0, 'a deleted login leaves no handoffs'
200
+ );
201
+
202
+ select * from finish();
203
+ rollback;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@marktiderman/genesis-supabase",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
- "description": "Supabase for Genesis apps on web and React Native: a client with PKCE and safe defaults, Keychain session storage that survives interrupted writes, refresh only while the app is in front, native Sign in with Apple and Google, OAuth redirects, in-app account deletion, per-tenant feature switches (tenant_features), a background-job queue with schedules (genesis_jobs), the feature registry in the database for the feature gate (features, entitlements, flag values, feature_on), and a result helper. Part of the Genesis framework.",
7
+ "description": "Supabase for Genesis apps on web and React Native: a client with PKCE and safe defaults, Keychain session storage that survives interrupted writes, refresh only while the app is in front, native Sign in with Apple and Google, OAuth redirects, in-app account deletion, per-tenant feature switches (tenant_features), a background-job queue with schedules (genesis_jobs) on one outbox engine (/runner, shared with notifications and forms), the feature registry in the database for the feature gate (features, entitlements, flag values, feature_on), a native-to-web sign-in handoff (continue on the web, already signed in: web-handoff), and a result helper. Part of the Genesis framework.",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/marktiderman/genesis.git",
@@ -35,6 +35,18 @@
35
35
  "./jobs": {
36
36
  "types": "./dist/jobs.d.ts",
37
37
  "import": "./dist/jobs.js"
38
+ },
39
+ "./runner": {
40
+ "types": "./dist/runner.d.ts",
41
+ "import": "./dist/runner.js"
42
+ },
43
+ "./web-handoff": {
44
+ "types": "./dist/web-handoff.d.ts",
45
+ "import": "./dist/web-handoff.js"
46
+ },
47
+ "./web-handoff/server": {
48
+ "types": "./dist/web-handoff-server.d.ts",
49
+ "import": "./dist/web-handoff-server.js"
38
50
  }
39
51
  },
40
52
  "files": [
@@ -224,6 +236,40 @@
224
236
  "steps": [
225
237
  "Define private.genesis_owner_can(tenant, viewer, person_id, team_id, organization_id, action) and private.genesis_readable_owners(viewer) in your own migration, as the header of 0009 shows. Without them the wrappers answer no."
226
238
  ]
239
+ },
240
+ "web-handoff": {
241
+ "feature": {
242
+ "name": "Continue on the web",
243
+ "summary": "A native app opens a web page signed in as the same person: a single-use, 60-second link minted by the server for the person's own account, confirmed on the page unless that browser is already signed in as them.",
244
+ "keywords": [
245
+ "handoff",
246
+ "open on web",
247
+ "magic link",
248
+ "single sign-on",
249
+ "continue on web"
250
+ ],
251
+ "audience": [
252
+ "person"
253
+ ]
254
+ },
255
+ "platforms": [
256
+ "web",
257
+ "native"
258
+ ],
259
+ "migrations": [
260
+ {
261
+ "file": "migrations/0015_genesis_web_handoff.sql",
262
+ "test": "migrations/tests/0015_genesis_web_handoff.test.sql"
263
+ }
264
+ ],
265
+ "install": [
266
+ "@supabase/supabase-js@^2.118.0"
267
+ ],
268
+ "steps": [
269
+ "Add two POST routes with createWebHandoff from @marktiderman/genesis-supabase/web-handoff/server (mint at /api/auth/handoff, redeem at /api/auth/redeem-handoff), using the service-role client on the server.",
270
+ "Add the /auth/handoff page: call takeWebHandoffFragment before analytics or error reporting load, then completeWebHandoff; send Referrer-Policy: no-referrer and frame-ancestors 'none' for it.",
271
+ "In the native app, call openOnWeb from @marktiderman/genesis-native/web-handoff with the mint route, the session's access token and expo-web-browser."
272
+ ]
227
273
  }
228
274
  }
229
275
  },
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/blocks.ts"],"names":[],"mappings":"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","file":"chunk-FWZIWKI4.js","sourcesContent":["/**\n * The blocks this package carries the server half of, as `defineBlock`\n * declarations: what each needs in the database, and what it means for the\n * stores' privacy forms. The functions themselves (`deleteMyAccount`,\n * `tenantFeatures`, …) don't change.\n */\nimport { defineBlock } from \"@marktiderman/genesis-features/blocks\";\n\nconst PACKAGE = \"@marktiderman/genesis-supabase\";\n\n/**\n * The account block: sign-in, profile and in-app account deletion. Its\n * screens are the app's (or genesis-ui-native's `AuthScreen`); this names its\n * migrations and its data.\n *\n * @stability Beta\n */\nexport const accountBlock = defineBlock({\n id: \"account\",\n platforms: [\"web\", \"native\"],\n contributes: {\n migrations: [\n {\n package: PACKAGE,\n file: \"migrations/0001_genesis_account_deletion.sql\",\n test: \"migrations/tests/0001_genesis_account_deletion.test.sql\",\n },\n // With Sign in with Apple, with the genesis-apple-revoke function beside it.\n {\n package: PACKAGE,\n file: \"migrations/0003_genesis_apple_token_revocation.sql\",\n test: \"migrations/tests/0003_genesis_apple_token_revocation.test.sql\",\n },\n ],\n privacy: {\n collects: [\n {\n type: \"email address\",\n purposes: [\"app functionality\"],\n linked: true,\n tracking: false,\n },\n {\n type: \"user id\",\n purposes: [\"app functionality\"],\n linked: true,\n tracking: false,\n },\n {\n type: \"name\",\n purposes: [\"app functionality\"],\n linked: true,\n tracking: false,\n },\n ],\n notes: [\n \"People delete their account from inside the app; their login and the rows that cascade from it go with it.\",\n \"With Sign in with Apple, the person's Apple tokens are revoked when the account is deleted.\",\n ],\n },\n },\n});\n\n/**\n * The tenant features block: per-tenant on/off switches for shipped\n * features, set by the tenant's admins with a reason.\n *\n * @stability Beta\n */\nexport const tenantFeaturesBlock = defineBlock({\n id: \"tenant-features\",\n contributes: {\n migrations: [\n {\n package: PACKAGE,\n file: \"migrations/0004_genesis_tenant_features.sql\",\n test: \"migrations/tests/0004_genesis_tenant_features.test.sql\",\n },\n ],\n privacy: {\n notes: [\n \"Keeps who changed a tenant's switches; a deleted person's changes stay, without the person.\",\n ],\n },\n },\n});\n\n/**\n * The feature registry block: the registry mirrored into the app's database\n * on deploy, entitlements, per-tenant and platform flag values and SQL `feature_on()`,\n * for the feature gate. Needs the tenant features block.\n *\n * @stability Beta\n */\nexport const featureMirrorBlock = defineBlock({\n id: \"feature-mirror\",\n requires: [\"tenant-features\"],\n contributes: {\n migrations: [\n {\n package: PACKAGE,\n file: \"migrations/0008_genesis_feature_registry.sql\",\n test: \"migrations/tests/0008_genesis_feature_registry.test.sql\",\n },\n {\n package: PACKAGE,\n file: \"migrations/0014_genesis_feature_registry_hardening.sql\",\n test: \"migrations/tests/0014_genesis_feature_registry_hardening.test.sql\",\n },\n ],\n privacy: {\n notes: [\n \"Stores which tenants hold which paid features and flag values; no personal data.\",\n ],\n },\n },\n});\n\n/**\n * The jobs block: a background-job queue and named schedules that call the\n * app's job route. Server only; the route is `createJobsHandler` from `/jobs`.\n *\n * @stability Beta\n */\nexport const jobsBlock = defineBlock({\n id: \"jobs\",\n platforms: [\"web\"],\n contributes: {\n migrations: [{ package: PACKAGE, file: \"migrations/0007_genesis_jobs.sql\", test: \"migrations/tests/0007_genesis_jobs.test.sql\" }],\n privacy: { notes: [\"Job payloads hold ids, never content or secrets; finished jobs are deleted after 14 days.\"] },\n },\n});\n\n/**\n * The owner-access block: the contract every Genesis block with owned rows\n * calls in its policies. Two fail-closed wrappers over the app's hooks\n * (`genesis_owner_can`, `genesis_readable_owners`): a missing or broken hook\n * means no.\n *\n * @stability Experimental\n */\nexport const ownerAccessBlock = defineBlock({\n id: \"owner-access\",\n requires: [\"tenant-features\"],\n platforms: [\"web\", \"native\"],\n contributes: {\n migrations: [\n {\n package: PACKAGE,\n file: \"migrations/0009_genesis_owner_access.sql\",\n test: \"migrations/tests/0009_genesis_owner_access.test.sql\",\n },\n ],\n privacy: { notes: [\"Stores nothing; it only asks the app's hooks who may read and write.\"] },\n },\n});\n"]}