Files
sase.tr/apps/api/drizzle/0023_api_keys.sql
Semih Yesilyurt 7a4fe51d09 feat(api): public API POC — API-key auth + /api/v1 decode & OEM xref
Kapalı-beta public API temeli (rapor: api-widget-analizi-2026-07-03):
- better-auth apiKey plugin (defaultPrefix sase_, enableMetadata, anahtar
  başına 120 istek/dk); enableSessionForAPIKeys KAPALI — anahtar dashboard
  oturumu yerine geçmez, cookie AuthGuard davranışı değişmez
- apikeys tablosu (migration 0023, plugin şemasının birebir karşılığı)
- ApiKeyGuard: Authorization Bearer / x-api-key → verifyApiKey → kullanıcı
  yüklenir (status kontrolü), request.user + request.apiKey doldurulur
- /api/v1/vin/decode: mevcut slim kontrat + aday akışı; günlük başarılı-decode
  kotası (Redis, TR günü, aynı VIN idempotent; limit: key metadata
  dailyDecodeLimit → PUBLIC_API_DAILY_DECODE_LIMIT → 100) ve
  X-Decode-Quota-* header'ları
- /api/v1/oem/:code: P çapraz-referans (kotasız)
- internal/admin/api-keys: x-internal-token ile anahtar üret/listele/kapat
  (düz metin yalnız create cevabında)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 16:15:17 +03:00

39 lines
1.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Public API anahtarları — better-auth `apiKey` plugin'inin tablosu (model
-- "apikey", usePlural → "apikeys"). Kolon seti plugin şemasının birebir
-- karşılığı; "key" kolonu secret'ın SHA256 hash'i (düz metin sadece üretim
-- anında bir kez gösterilir). "metadata" JSON string (dailyDecodeLimit gibi
-- anahtar-bazlı limitler burada taşınır).
CREATE TABLE IF NOT EXISTS "apikeys" (
"id" text PRIMARY KEY NOT NULL,
"name" text,
"start" text,
"prefix" text,
"key" text NOT NULL,
"user_id" uuid NOT NULL,
"refill_interval" integer,
"refill_amount" integer,
"last_refill_at" timestamp with time zone,
"enabled" boolean DEFAULT true NOT NULL,
"rate_limit_enabled" boolean DEFAULT true NOT NULL,
"rate_limit_time_window" integer,
"rate_limit_max" integer,
"request_count" integer DEFAULT 0 NOT NULL,
"remaining" integer,
"last_request" timestamp with time zone,
"expires_at" timestamp with time zone,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
"permissions" text,
"metadata" text
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "apikeys" ADD CONSTRAINT "apikeys_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "apikeys_key_idx" ON "apikeys" ("key");
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "apikeys_user_id_idx" ON "apikeys" ("user_id");