|
| 1 | +-- Migration: 0001_better_auth.sql |
| 2 | +-- Better Auth core + OAuth 2.1 Provider + MCP schema for Cloudflare D1 |
| 3 | + |
| 4 | +CREATE TABLE IF NOT EXISTS "user" ( |
| 5 | + "id" text PRIMARY KEY NOT NULL, |
| 6 | + "name" text NOT NULL, |
| 7 | + "email" text NOT NULL UNIQUE, |
| 8 | + "emailVerified" integer NOT NULL DEFAULT 0, |
| 9 | + "image" text, |
| 10 | + "createdAt" integer NOT NULL, |
| 11 | + "updatedAt" integer NOT NULL |
| 12 | +); |
| 13 | + |
| 14 | +CREATE TABLE IF NOT EXISTS "session" ( |
| 15 | + "id" text PRIMARY KEY NOT NULL, |
| 16 | + "expiresAt" integer NOT NULL, |
| 17 | + "token" text NOT NULL UNIQUE, |
| 18 | + "createdAt" integer NOT NULL, |
| 19 | + "updatedAt" integer NOT NULL, |
| 20 | + "ipAddress" text, |
| 21 | + "userAgent" text, |
| 22 | + "userId" text NOT NULL REFERENCES "user"("id") ON DELETE CASCADE |
| 23 | +); |
| 24 | + |
| 25 | +CREATE INDEX IF NOT EXISTS "session_userId_idx" ON "session" ("userId"); |
| 26 | + |
| 27 | +CREATE TABLE IF NOT EXISTS "account" ( |
| 28 | + "id" text PRIMARY KEY NOT NULL, |
| 29 | + "accountId" text NOT NULL, |
| 30 | + "providerId" text NOT NULL, |
| 31 | + "userId" text NOT NULL REFERENCES "user"("id") ON DELETE CASCADE, |
| 32 | + "accessToken" text, |
| 33 | + "refreshToken" text, |
| 34 | + "idToken" text, |
| 35 | + "accessTokenExpiresAt" integer, |
| 36 | + "refreshTokenExpiresAt" integer, |
| 37 | + "scope" text, |
| 38 | + "password" text, |
| 39 | + "createdAt" integer NOT NULL, |
| 40 | + "updatedAt" integer NOT NULL |
| 41 | +); |
| 42 | + |
| 43 | +CREATE INDEX IF NOT EXISTS "account_userId_idx" ON "account" ("userId"); |
| 44 | + |
| 45 | +CREATE TABLE IF NOT EXISTS "verification" ( |
| 46 | + "id" text PRIMARY KEY NOT NULL, |
| 47 | + "identifier" text NOT NULL, |
| 48 | + "value" text NOT NULL, |
| 49 | + "expiresAt" integer NOT NULL, |
| 50 | + "createdAt" integer NOT NULL, |
| 51 | + "updatedAt" integer NOT NULL |
| 52 | +); |
| 53 | + |
| 54 | +CREATE TABLE IF NOT EXISTS "jwks" ( |
| 55 | + "id" text PRIMARY KEY NOT NULL, |
| 56 | + "publicKey" text NOT NULL, |
| 57 | + "privateKey" text NOT NULL, |
| 58 | + "createdAt" integer NOT NULL, |
| 59 | + "expiresAt" integer, |
| 60 | + "alg" text, |
| 61 | + "crv" text |
| 62 | +); |
| 63 | + |
| 64 | +CREATE TABLE IF NOT EXISTS "oauthClient" ( |
| 65 | + "id" text PRIMARY KEY NOT NULL, |
| 66 | + "clientId" text NOT NULL UNIQUE, |
| 67 | + "clientSecret" text, |
| 68 | + "clientDiscoveryId" text, |
| 69 | + "disabled" integer DEFAULT 0, |
| 70 | + "skipConsent" integer DEFAULT 0, |
| 71 | + "enableEndSession" integer DEFAULT 0, |
| 72 | + "subjectType" text, |
| 73 | + "scopes" text, |
| 74 | + "clientCredentialsScopes" text, |
| 75 | + "userId" text REFERENCES "user"("id") ON DELETE CASCADE, |
| 76 | + "createdAt" integer, |
| 77 | + "updatedAt" integer, |
| 78 | + "name" text, |
| 79 | + "uri" text, |
| 80 | + "icon" text, |
| 81 | + "contacts" text, |
| 82 | + "tos" text, |
| 83 | + "policy" text, |
| 84 | + "softwareId" text, |
| 85 | + "softwareVersion" text, |
| 86 | + "softwareStatement" text, |
| 87 | + "redirectUris" text NOT NULL, |
| 88 | + "postLogoutRedirectUris" text, |
| 89 | + "backchannelLogoutUri" text, |
| 90 | + "backchannelLogoutSessionRequired" integer, |
| 91 | + "tokenEndpointAuthMethod" text, |
| 92 | + "applicationType" text, |
| 93 | + "jwks" text, |
| 94 | + "jwksUri" text, |
| 95 | + "grantTypes" text, |
| 96 | + "responseTypes" text, |
| 97 | + "requirePKCE" integer DEFAULT 1, |
| 98 | + "dpopBoundAccessTokens" integer DEFAULT 0, |
| 99 | + "referenceId" text, |
| 100 | + "metadata" text |
| 101 | +); |
| 102 | + |
| 103 | +CREATE INDEX IF NOT EXISTS "oauthClient_userId_idx" ON "oauthClient" ("userId"); |
| 104 | + |
| 105 | +CREATE TABLE IF NOT EXISTS "oauthResource" ( |
| 106 | + "id" text PRIMARY KEY NOT NULL, |
| 107 | + "identifier" text NOT NULL UNIQUE, |
| 108 | + "name" text NOT NULL, |
| 109 | + "accessTokenTtl" integer, |
| 110 | + "refreshTokenTtl" integer, |
| 111 | + "signingAlgorithm" text, |
| 112 | + "signingKeyId" text, |
| 113 | + "allowedScopes" text, |
| 114 | + "customClaims" text, |
| 115 | + "dpopBoundAccessTokensRequired" integer DEFAULT 0, |
| 116 | + "disabled" integer DEFAULT 0, |
| 117 | + "createdAt" integer, |
| 118 | + "updatedAt" integer, |
| 119 | + "policyVersion" integer, |
| 120 | + "metadata" text |
| 121 | +); |
| 122 | + |
| 123 | +CREATE TABLE IF NOT EXISTS "oauthClientResource" ( |
| 124 | + "id" text PRIMARY KEY NOT NULL, |
| 125 | + "clientId" text NOT NULL REFERENCES "oauthClient"("clientId") ON DELETE CASCADE, |
| 126 | + "resourceId" text NOT NULL REFERENCES "oauthResource"("identifier") ON DELETE CASCADE, |
| 127 | + "metadata" text, |
| 128 | + "createdAt" integer |
| 129 | +); |
| 130 | + |
| 131 | +CREATE UNIQUE INDEX IF NOT EXISTS "oauthClientResource_clientId_resourceId_uidx" ON "oauthClientResource" ("clientId", "resourceId"); |
| 132 | + |
| 133 | +CREATE TABLE IF NOT EXISTS "oauthRefreshToken" ( |
| 134 | + "id" text PRIMARY KEY NOT NULL, |
| 135 | + "token" text NOT NULL UNIQUE, |
| 136 | + "clientId" text NOT NULL REFERENCES "oauthClient"("clientId") ON DELETE CASCADE, |
| 137 | + "sessionId" text REFERENCES "session"("id") ON DELETE SET NULL, |
| 138 | + "userId" text NOT NULL REFERENCES "user"("id") ON DELETE CASCADE, |
| 139 | + "referenceId" text, |
| 140 | + "authorizationCodeId" text, |
| 141 | + "resources" text, |
| 142 | + "requestedUserInfoClaims" text, |
| 143 | + "expiresAt" integer NOT NULL, |
| 144 | + "createdAt" integer NOT NULL, |
| 145 | + "revoked" integer, |
| 146 | + "rotatedAt" integer, |
| 147 | + "rotationReplayResponse" text, |
| 148 | + "rotationReplayExpiresAt" integer, |
| 149 | + "authTime" integer, |
| 150 | + "confirmation" text, |
| 151 | + "scopes" text NOT NULL |
| 152 | +); |
| 153 | + |
| 154 | +CREATE INDEX IF NOT EXISTS "oauthRefreshToken_clientId_idx" ON "oauthRefreshToken" ("clientId"); |
| 155 | +CREATE INDEX IF NOT EXISTS "oauthRefreshToken_userId_idx" ON "oauthRefreshToken" ("userId"); |
| 156 | +CREATE INDEX IF NOT EXISTS "oauthRefreshToken_sessionId_idx" ON "oauthRefreshToken" ("sessionId"); |
| 157 | +CREATE INDEX IF NOT EXISTS "oauthRefreshToken_authorizationCodeId_idx" ON "oauthRefreshToken" ("authorizationCodeId"); |
| 158 | + |
| 159 | +CREATE TABLE IF NOT EXISTS "oauthAccessToken" ( |
| 160 | + "id" text PRIMARY KEY NOT NULL, |
| 161 | + "token" text NOT NULL UNIQUE, |
| 162 | + "clientId" text NOT NULL REFERENCES "oauthClient"("clientId") ON DELETE CASCADE, |
| 163 | + "sessionId" text REFERENCES "session"("id") ON DELETE SET NULL, |
| 164 | + "userId" text REFERENCES "user"("id") ON DELETE CASCADE, |
| 165 | + "referenceId" text, |
| 166 | + "authorizationCodeId" text, |
| 167 | + "resources" text, |
| 168 | + "requestedUserInfoClaims" text, |
| 169 | + "refreshId" text REFERENCES "oauthRefreshToken"("id") ON DELETE CASCADE, |
| 170 | + "expiresAt" integer NOT NULL, |
| 171 | + "createdAt" integer NOT NULL, |
| 172 | + "revoked" integer, |
| 173 | + "confirmation" text, |
| 174 | + "scopes" text NOT NULL |
| 175 | +); |
| 176 | + |
| 177 | +CREATE INDEX IF NOT EXISTS "oauthAccessToken_clientId_idx" ON "oauthAccessToken" ("clientId"); |
| 178 | +CREATE INDEX IF NOT EXISTS "oauthAccessToken_userId_idx" ON "oauthAccessToken" ("userId"); |
| 179 | +CREATE INDEX IF NOT EXISTS "oauthAccessToken_sessionId_idx" ON "oauthAccessToken" ("sessionId"); |
| 180 | +CREATE INDEX IF NOT EXISTS "oauthAccessToken_authorizationCodeId_idx" ON "oauthAccessToken" ("authorizationCodeId"); |
| 181 | +CREATE INDEX IF NOT EXISTS "oauthAccessToken_refreshId_idx" ON "oauthAccessToken" ("refreshId"); |
| 182 | + |
| 183 | +CREATE TABLE IF NOT EXISTS "oauthConsent" ( |
| 184 | + "id" text PRIMARY KEY NOT NULL, |
| 185 | + "clientId" text NOT NULL REFERENCES "oauthClient"("clientId") ON DELETE CASCADE, |
| 186 | + "userId" text REFERENCES "user"("id") ON DELETE CASCADE, |
| 187 | + "referenceId" text, |
| 188 | + "resources" text, |
| 189 | + "requestedUserInfoClaims" text, |
| 190 | + "scopes" text NOT NULL, |
| 191 | + "createdAt" integer NOT NULL, |
| 192 | + "updatedAt" integer NOT NULL |
| 193 | +); |
| 194 | + |
| 195 | +CREATE INDEX IF NOT EXISTS "oauthConsent_clientId_idx" ON "oauthConsent" ("clientId"); |
| 196 | +CREATE INDEX IF NOT EXISTS "oauthConsent_userId_idx" ON "oauthConsent" ("userId"); |
| 197 | + |
| 198 | +CREATE TABLE IF NOT EXISTS "oauthClientAssertion" ( |
| 199 | + "id" text PRIMARY KEY NOT NULL, |
| 200 | + "expiresAt" integer NOT NULL |
| 201 | +); |
0 commit comments