Skip to content

Commit 88a0ce9

Browse files
committed
Give the site sign-in door its schema, scopes and tokens
First step of Plan 008: the tables, the scope rules, the token families and the repository. No endpoints yet — those come next, and keeping them out means this step can be read and reverted on its own. Deliberately not built on the MCP tables. An mcp_authorization_grants row cannot exist without an agent_id, and a site grant has nothing to do with agents — it is valid for an account that never creates one. Sharing the table would have meant asking "which kind of row is this" in every query. Three things here were learned the hard way elsewhere in this repo. The scope list lives in code rather than a CHECK, because 0022 nailed its scope down that way and every widening after it had to rebuild the table. The code is single-use through the WHERE clause of one UPDATE rather than a read followed by a write, since two requests holding the same code would both see it unconsumed. And tokens are written after the code is burned, not before: if the step between fails the user simply signs in again, where the other order could leave a live token behind an open code. resolveToken carries the account and client status in the same read as the token. A second query would leave a window in which the account is suspended but the refresh still looks legitimate — the 15 minute access token is the whole reason suspension propagates at all. Tests are in test:d1:core rather than only test:d1, because the deploy workflow runs the split lists and not the full one. Two of them were verified by reverting the protection they cover: removing the consumed_at guard fails the single-use test, and dropping the code burn from revokeGrant fails the revocation test.
1 parent ac74c9d commit 88a0ce9

8 files changed

Lines changed: 2102 additions & 5 deletions
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
-- Orbit başka sitelerin giriş kapısı oluyor.
2+
--
3+
-- Bugüne kadar Orbit bir kimlik TÜKETİCİSİYDİ: Google'dan kim olduğunu öğrenip
4+
-- kendi oturumunu kuruyordu. Bu göç onu bir kimlik SAĞLAYICISINA çeviriyor —
5+
-- Anime sitesi gibi diğer Equinox siteleri kendi hesap sistemini kurmayacak,
6+
-- kullanıcı orada "Orbit ile devam et"e basacak.
7+
--
8+
-- Tasarım kaydı: docs/FUTURE_PLANS.md Plan 008.
9+
--
10+
-- Neden MCP tablolarının üstüne kurulmuyor: `mcp_authorization_grants` bir
11+
-- AJANA yetki veriyor ve satırı `agent_id` olmadan var olamıyor. Buradaki izin
12+
-- bir SİTEYE kim olduğunu söylüyor ve ajanla hiç ilgisi yok — hesabın ajanı
13+
-- olmasa da geçerli. İkisini tek tabloya sıkıştırmak, her sorguya "bu satır
14+
-- hangi cinsten" sorusunu eklemek ve iki özelliğin tetikleyicilerini
15+
-- birbirine dolaştırmak olurdu.
16+
17+
PRAGMA foreign_keys = ON;
18+
19+
-- 1. Bağlanacak siteler.
20+
--
21+
-- Dinamik kayıt yok: site sayısı beş ve istemciler operatör eliyle giriliyor.
22+
-- Kendi kendine kaydolabilen bir istemci yüzeyi, onay ekranında rastgele bir
23+
-- ismin görünmesine izin vermek demekti.
24+
CREATE TABLE oauth_clients (
25+
id TEXT PRIMARY KEY,
26+
client_id TEXT NOT NULL UNIQUE CHECK (length(trim(client_id)) BETWEEN 8 AND 255),
27+
secret_digest TEXT NOT NULL,
28+
hash_version INTEGER NOT NULL CHECK (hash_version > 0),
29+
-- Onay ekranında kullanıcının göreceği ad. "Anime sitesi" gibi.
30+
label TEXT NOT NULL CHECK (length(trim(label)) BETWEEN 1 AND 120),
31+
site_url TEXT NOT NULL CHECK (site_url LIKE 'https://%' OR site_url LIKE 'http://localhost%'),
32+
-- İzin verilen kapsamların ÜST SINIRI, boşlukla ayrılmış.
33+
--
34+
-- Neden bir CHECK listesi değil: 0022 kapsamı `CHECK (scopes = 'feed:read')`
35+
-- diye çivilemişti ve kapsam ikinci kez genişlediğinde tabloyu yeniden kurmak
36+
-- gerekti (0024, 0025). Yayımlanmış bir migration değiştirilemediği için o
37+
-- bedel her genişlemede tekrar ödeniyor. Kapsam listesinin doğrulaması bu
38+
-- yüzden kodda: `site-authorization-scopes.ts`.
39+
allowed_scopes TEXT NOT NULL CHECK (length(trim(allowed_scopes)) BETWEEN 6 AND 500),
40+
environment TEXT NOT NULL CHECK (environment IN ('production', 'development')),
41+
status TEXT NOT NULL CHECK (status IN ('active', 'revoked')),
42+
created_at INTEGER NOT NULL,
43+
revoked_at INTEGER,
44+
CHECK (revoked_at IS NULL OR revoked_at >= created_at),
45+
CHECK ((status = 'revoked') = (revoked_at IS NOT NULL))
46+
);
47+
48+
CREATE INDEX oauth_clients_status_idx ON oauth_clients (status, created_at DESC);
49+
50+
-- 2. Yönlendirme adresleri.
51+
--
52+
-- Bu tablonun tek işi açık yönlendiriciyi engellemek. Adres tam eşleşiyor;
53+
-- joker, önek eşleşmesi veya "aynı alan adı olsun yeter" yok. Gevşek bir
54+
-- eşleşme, kullanıcının Orbit'te verdiği onayın karşılığındaki anahtarın
55+
-- saldırganın seçtiği adrese gitmesi demek.
56+
CREATE TABLE oauth_client_redirect_uris (
57+
id TEXT PRIMARY KEY,
58+
client_id TEXT NOT NULL REFERENCES oauth_clients(id),
59+
redirect_uri TEXT NOT NULL CHECK (
60+
length(redirect_uri) BETWEEN 8 AND 500
61+
-- Parça (#fragment) taşıyan adres yok: yönlendirme parametrelerini
62+
-- fragment'a yazmak onları sunucuya hiç ulaşmayan bir yere koyar.
63+
AND instr(redirect_uri, '#') = 0
64+
),
65+
created_at INTEGER NOT NULL,
66+
UNIQUE (client_id, redirect_uri)
67+
);
68+
69+
-- `http://localhost` yalnız development istemcisinde. İstemcinin ortamı başka
70+
-- bir tabloda olduğu için bu bir CHECK ile yazılamıyor; tetikleyici gerekiyor.
71+
CREATE TRIGGER oauth_client_redirect_uris_scheme_insert
72+
BEFORE INSERT ON oauth_client_redirect_uris
73+
BEGIN
74+
SELECT RAISE(ABORT, 'oauth_redirect_uri_insecure')
75+
WHERE NOT EXISTS (
76+
SELECT 1
77+
FROM oauth_clients client
78+
WHERE client.id = NEW.client_id
79+
AND (
80+
NEW.redirect_uri LIKE 'https://%'
81+
OR (client.environment = 'development' AND NEW.redirect_uri LIKE 'http://localhost%')
82+
)
83+
);
84+
END;
85+
86+
-- 3. Site başına kullanıcı kimliği.
87+
--
88+
-- Alt siteye giden `sub` claim'i budur ve `accounts.id` DEĞİLDİR. Her istemci
89+
-- aynı insanı farklı bir kimlikle görüyor.
90+
--
91+
-- Dürüst sınır: e-posta her siteye verildiği için siteler kullanıcıyı adres
92+
-- üzerinden yine eşleştirebilir, yani buradaki kazanç gizlilik değil. Kazanç
93+
-- iç kimliklerimizin dışarı hiç çıkmaması: `accounts.id` bir kez dağıldıktan
94+
-- sonra geri toplanamaz ve bir gün başka bir yerde anahtar olarak kullanılır.
95+
CREATE TABLE oauth_client_subjects (
96+
id TEXT PRIMARY KEY,
97+
client_id TEXT NOT NULL REFERENCES oauth_clients(id),
98+
account_id TEXT NOT NULL REFERENCES accounts(id),
99+
subject TEXT NOT NULL UNIQUE CHECK (length(subject) BETWEEN 16 AND 64),
100+
created_at INTEGER NOT NULL,
101+
UNIQUE (client_id, account_id)
102+
);
103+
104+
-- 4. Kullanıcının bir siteye verdiği izin.
105+
--
106+
-- Onayın kaydı bu satır. Kullanıcı panelde "bağlı siteler"i buradan görüyor ve
107+
-- iptal buradan yürüyor. İstemci + hesap başına tek satır: aynı siteye ikinci
108+
-- kez giriş yeni bir izin doğurmuyor, mevcut izni tazeliyor.
109+
CREATE TABLE oauth_client_grants (
110+
id TEXT PRIMARY KEY,
111+
client_id TEXT NOT NULL REFERENCES oauth_clients(id),
112+
account_id TEXT NOT NULL REFERENCES accounts(id),
113+
scopes TEXT NOT NULL CHECK (length(trim(scopes)) BETWEEN 6 AND 500),
114+
-- Hangi onay metniyle verildi. "Bir zamanlar kabul etmişti" bir cevap değil;
115+
-- kapsam genişlediğinde kullanıcının yeniden onaylaması bu alana bakıyor.
116+
consent_version TEXT NOT NULL CHECK (length(trim(consent_version)) BETWEEN 1 AND 40),
117+
created_at INTEGER NOT NULL,
118+
updated_at INTEGER NOT NULL,
119+
last_used_at INTEGER,
120+
revoked_at INTEGER,
121+
revoked_reason TEXT,
122+
UNIQUE (client_id, account_id),
123+
CHECK (updated_at >= created_at),
124+
CHECK (last_used_at IS NULL OR last_used_at >= created_at),
125+
CHECK (revoked_at IS NULL OR revoked_at >= created_at),
126+
CHECK (
127+
(revoked_at IS NULL AND revoked_reason IS NULL)
128+
OR
129+
(
130+
revoked_at IS NOT NULL
131+
AND revoked_reason IS NOT NULL
132+
AND length(trim(revoked_reason)) BETWEEN 1 AND 120
133+
)
134+
)
135+
);
136+
137+
CREATE INDEX oauth_client_grants_account_idx
138+
ON oauth_client_grants (account_id, revoked_at, created_at DESC);
139+
140+
CREATE INDEX oauth_client_grants_client_idx
141+
ON oauth_client_grants (client_id, revoked_at, created_at DESC);
142+
143+
-- İzin yalnız aktif hesaba ve aktif istemciye yazılabiliyor. MCP tarafındaki
144+
-- `mcp_authorization_grants_validate` ile aynı gerekçe: kapıyı uygulama
145+
-- katmanında tutmak, o kontrolü atlayan ikinci bir yazma yolunun eninde
146+
-- sonunda yazılmasına güvenmek demek.
147+
CREATE TRIGGER oauth_client_grants_validate
148+
BEFORE INSERT ON oauth_client_grants
149+
BEGIN
150+
SELECT RAISE(ABORT, 'oauth_grant_account_inactive')
151+
WHERE NOT EXISTS (
152+
SELECT 1 FROM accounts account
153+
WHERE account.id = NEW.account_id AND account.status = 'active'
154+
);
155+
156+
SELECT RAISE(ABORT, 'oauth_grant_client_inactive')
157+
WHERE NOT EXISTS (
158+
SELECT 1 FROM oauth_clients client
159+
WHERE client.id = NEW.client_id AND client.status = 'active'
160+
);
161+
END;
162+
163+
-- Kimin kime izin verdiği değişmez. Kapsam, kullanım ve iptal değişir.
164+
CREATE TRIGGER oauth_client_grants_identity_immutable
165+
BEFORE UPDATE OF id, client_id, account_id, created_at ON oauth_client_grants
166+
BEGIN
167+
SELECT RAISE(ABORT, 'oauth_grant_identity_immutable');
168+
END;
169+
170+
-- 5. Yetkilendirme kodu.
171+
--
172+
-- Tarayıcıdan geçen tek kullanımlık, 60 saniyelik parça. Sırrın kendisi
173+
-- saklanmıyor, digest'i saklanıyor.
174+
CREATE TABLE oauth_authorization_codes (
175+
id TEXT PRIMARY KEY,
176+
grant_id TEXT NOT NULL REFERENCES oauth_client_grants(id),
177+
code_digest TEXT NOT NULL UNIQUE,
178+
hash_version INTEGER NOT NULL CHECK (hash_version > 0),
179+
-- Kod hangi adrese verildiyse yalnız o adresle takas edilebilir. Bu alan
180+
-- olmadan, izinli adreslerden birine verilen kod başka birine taşınabilirdi.
181+
redirect_uri TEXT NOT NULL,
182+
-- PKCE: kodu kim istediyse yalnız o takas edebilsin. Supabase tarafı bu
183+
-- akışı zaten PKCE ile kuruyor.
184+
pkce_challenge TEXT NOT NULL CHECK (length(pkce_challenge) BETWEEN 43 AND 128),
185+
-- `nonce` opsiyonel bir süs değil: Supabase custom OIDC sağlayıcısı
186+
-- varsayılan olarak nonce doğruluyor. İstemci gönderdiyse ID token'a
187+
-- birebir yazılmak zorunda.
188+
nonce TEXT CHECK (nonce IS NULL OR length(nonce) BETWEEN 8 AND 200),
189+
scopes TEXT NOT NULL,
190+
created_at INTEGER NOT NULL,
191+
expires_at INTEGER NOT NULL,
192+
consumed_at INTEGER,
193+
CHECK (expires_at > created_at),
194+
CHECK (consumed_at IS NULL OR consumed_at >= created_at)
195+
);
196+
197+
CREATE INDEX oauth_authorization_codes_grant_idx
198+
ON oauth_authorization_codes (grant_id, created_at DESC);
199+
CREATE INDEX oauth_authorization_codes_expiry_idx
200+
ON oauth_authorization_codes (expires_at);
201+
202+
-- İptal edilmiş bir izne kod yazılamaz. Kullanıcı izni geri aldıktan sonra
203+
-- üretilen bir kod, iptali sessizce geçersiz kılardı.
204+
CREATE TRIGGER oauth_authorization_codes_validate
205+
BEFORE INSERT ON oauth_authorization_codes
206+
BEGIN
207+
SELECT RAISE(ABORT, 'oauth_code_grant_revoked')
208+
WHERE NOT EXISTS (
209+
SELECT 1 FROM oauth_client_grants grant_row
210+
WHERE grant_row.id = NEW.grant_id AND grant_row.revoked_at IS NULL
211+
);
212+
END;
213+
214+
-- 6. Siteye verilen anahtarlar.
215+
--
216+
-- İki cins tek tabloda, çünkü ikisinin de yaşam döngüsü aynı: bir izne bağlı
217+
-- doğuyor, süresi bitiyor, iptal ediliyor. Ayrı tablolar iptali iki yere
218+
-- yazmak olurdu ve bir gün biri unutulurdu.
219+
--
220+
-- Erişim anahtarı 15 dakika yaşıyor. Sebep askıya alma: hesabı askıya
221+
-- alındığında alt sitedeki oturumun en çok 15 dakika içinde ölmesi gerekiyor.
222+
-- Alternatif her istekte Orbit'e sormaktı — daha doğru ama alt siteyi her
223+
-- sayfa için Orbit'e bağımlı yapıyor.
224+
CREATE TABLE oauth_site_tokens (
225+
id TEXT PRIMARY KEY,
226+
grant_id TEXT NOT NULL REFERENCES oauth_client_grants(id),
227+
token_type TEXT NOT NULL CHECK (token_type IN ('access', 'refresh')),
228+
secret_digest TEXT NOT NULL UNIQUE,
229+
hash_version INTEGER NOT NULL CHECK (hash_version > 0),
230+
-- Rotasyon zinciri. Yenileme anahtarı her kullanımda yenisini doğuruyor ve
231+
-- eskisi burada yenisine işaret ediyor. Kullanılmış bir yenileme
232+
-- anahtarının ikinci kez gelmesi çalınmış olmasının işareti; o an bütün
233+
-- zincir düşüyor.
234+
replaced_by_id TEXT REFERENCES oauth_site_tokens(id),
235+
created_at INTEGER NOT NULL,
236+
expires_at INTEGER NOT NULL,
237+
used_at INTEGER,
238+
revoked_at INTEGER,
239+
revoked_reason TEXT,
240+
CHECK (expires_at > created_at),
241+
CHECK (used_at IS NULL OR used_at >= created_at),
242+
CHECK (revoked_at IS NULL OR revoked_at >= created_at),
243+
CHECK (
244+
(revoked_at IS NULL AND revoked_reason IS NULL)
245+
OR
246+
(
247+
revoked_at IS NOT NULL
248+
AND revoked_reason IS NOT NULL
249+
AND length(trim(revoked_reason)) BETWEEN 1 AND 120
250+
)
251+
),
252+
-- Yalnız yenileme anahtarı yenisiyle değiştirilebilir.
253+
CHECK (replaced_by_id IS NULL OR token_type = 'refresh')
254+
);
255+
256+
CREATE INDEX oauth_site_tokens_grant_idx
257+
ON oauth_site_tokens (grant_id, token_type, revoked_at, expires_at);
258+
CREATE INDEX oauth_site_tokens_expiry_idx ON oauth_site_tokens (expires_at);
259+
260+
CREATE TRIGGER oauth_site_tokens_validate
261+
BEFORE INSERT ON oauth_site_tokens
262+
BEGIN
263+
SELECT RAISE(ABORT, 'oauth_token_grant_revoked')
264+
WHERE NOT EXISTS (
265+
SELECT 1 FROM oauth_client_grants grant_row
266+
WHERE grant_row.id = NEW.grant_id AND grant_row.revoked_at IS NULL
267+
);
268+
END;
269+
270+
CREATE TRIGGER oauth_site_tokens_identity_immutable
271+
BEFORE UPDATE OF id, grant_id, token_type, secret_digest, created_at, expires_at
272+
ON oauth_site_tokens
273+
BEGIN
274+
SELECT RAISE(ABORT, 'oauth_token_identity_immutable');
275+
END;

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"staging:slice5:images-cpu": "tsx scripts/orbit-slice5-images-cpu-staging-e2e.ts",
3636
"orbit:import:verify": "tsx scripts/orbit-slice3-manifest.ts",
3737
"orbit:import:local": "tsx scripts/orbit-slice3-import.ts --local --database=orbit-v6-local --config=wrangler.test.jsonc",
38-
"test:d1": "tsx --test --test-concurrency=5 scripts/orbit-agent-identity-tests.ts scripts/orbit-api-contract-tests.ts scripts/orbit-handle-policy-tests.ts scripts/orbit-d1-tests.ts scripts/orbit-email-tests.ts scripts/orbit-mcp-authorization-tests.ts scripts/orbit-public-page-tests.ts scripts/orbit-public-repository-tests.ts scripts/orbit-single-renderer-tests.ts scripts/orbit-style-scale-tests.ts scripts/orbit-slice1-tests.ts scripts/orbit-slice1-worker-tests.ts scripts/orbit-slice3-tests.ts scripts/orbit-slice4-tests.ts scripts/orbit-slice5-tests.ts",
39-
"test:d1:core": "tsx --test --test-concurrency=5 scripts/orbit-agent-identity-tests.ts scripts/orbit-api-contract-tests.ts scripts/orbit-handle-policy-tests.ts scripts/orbit-d1-tests.ts scripts/orbit-email-tests.ts scripts/orbit-public-page-tests.ts scripts/orbit-public-repository-tests.ts scripts/orbit-slice1-tests.ts scripts/orbit-slice1-worker-tests.ts scripts/orbit-slice3-tests.ts",
38+
"test:d1": "tsx --test --test-concurrency=5 scripts/orbit-agent-identity-tests.ts scripts/orbit-api-contract-tests.ts scripts/orbit-handle-policy-tests.ts scripts/orbit-d1-tests.ts scripts/orbit-email-tests.ts scripts/orbit-mcp-authorization-tests.ts scripts/orbit-site-authorization-tests.ts scripts/orbit-public-page-tests.ts scripts/orbit-public-repository-tests.ts scripts/orbit-single-renderer-tests.ts scripts/orbit-style-scale-tests.ts scripts/orbit-slice1-tests.ts scripts/orbit-slice1-worker-tests.ts scripts/orbit-slice3-tests.ts scripts/orbit-slice4-tests.ts scripts/orbit-slice5-tests.ts",
39+
"test:d1:core": "tsx --test --test-concurrency=5 scripts/orbit-site-authorization-tests.ts scripts/orbit-agent-identity-tests.ts scripts/orbit-api-contract-tests.ts scripts/orbit-handle-policy-tests.ts scripts/orbit-d1-tests.ts scripts/orbit-email-tests.ts scripts/orbit-public-page-tests.ts scripts/orbit-public-repository-tests.ts scripts/orbit-slice1-tests.ts scripts/orbit-slice1-worker-tests.ts scripts/orbit-slice3-tests.ts",
4040
"test:d1:publication": "tsx --test scripts/orbit-slice4-tests.ts",
4141
"test:d1:platform": "tsx --test scripts/orbit-slice5-tests.ts",
4242
"v6:check": "npm run test:d1 && npm run check && npm run worker:build",

0 commit comments

Comments
 (0)