| 1 | import { sql } from "drizzle-orm"; |
| 2 | import { |
| 3 | boolean, |
| 4 | index, |
| 5 | integer, |
| 6 | jsonb, |
| 7 | pgTable, |
| 8 | text, |
| 9 | timestamp, |
| 10 | } from "drizzle-orm/pg-core"; |
| 11 | |
| 12 | import { session, user |
| 13 | |
| 14 | /** Tables for Better Auth's jwt and oauth-provider plugins. The plugins read them by these export names, so names and columns follow the plugins' schema. */ |
| 15 | |
| 16 | export const jwks = |
| 17 | id: text("id |
| 18 | publicKey: text("public_key |
| 19 | privateKey: text("private_key |
| 20 | createdAt: timestamp("created_at |
| 21 | expiresAt: timestamp("expires_at |
| 22 | alg: text("alg |
| 23 | crv: text("crv |
| 24 | }); |
| 25 | |
| 26 | /** An OAuth app. `userId` is null for apps built into the instance, such as gh. */ |
| 27 | export const oauthClient = |
| 28 | "oauth_client", |
| 29 | { |
| 30 | id: text("id |
| 31 | clientId: text("client_id |
| 32 | clientSecret: text("client_secret |
| 33 | clientDiscoveryId: text("client_discovery_id |
| 34 | disabled: boolean("disabled |
| 35 | skipConsent: boolean("skip_consent |
| 36 | enableEndSession: boolean("enable_end_session |
| 37 | subjectType: text("subject_type |
| 38 | scopes: text("scopes |
| 39 | clientCredentialsScopes: text("client_credentials_scopes |
| 40 | .array() |
| 41 | .default(sql`'{}'::text[] |
| 42 | userId: text("user_id |
| 43 | createdAt: timestamp("created_at |
| 44 | updatedAt: timestamp("updated_at |
| 45 | name: text("name |
| 46 | uri: text("uri |
| 47 | icon: text("icon |
| 48 | contacts: text("contacts |
| 49 | tos: text("tos |
| 50 | policy: text("policy |
| 51 | softwareId: text("software_id |
| 52 | softwareVersion: text("software_version |
| 53 | softwareStatement: text("software_statement |
| 54 | redirectUris: text("redirect_uris |
| 55 | postLogoutRedirectUris: text("post_logout_redirect_uris |
| 56 | backchannelLogoutUri: text("backchannel_logout_uri |
| 57 | backchannelLogoutSessionRequired: boolean( |
| 58 | "backchannel_logout_session_required", |
| 59 | ), |
| 60 | tokenEndpointAuthMethod: text("token_endpoint_auth_method |
| 61 | applicationType: text("application_type |
| 62 | jwks: text("jwks |
| 63 | jwksUri: text("jwks_uri |
| 64 | grantTypes: text("grant_types |
| 65 | responseTypes: text("response_types |
| 66 | requirePKCE: boolean("require_pkce |
| 67 | dpopBoundAccessTokens: boolean("dpop_bound_access_tokens |
| 68 | referenceId: text("reference_id |
| 69 | metadata: jsonb("metadata |
| 70 | }, |
| 71 | (table) => |
| 72 | ); |
| 73 | |
| 74 | export const oauthResource = |
| 75 | id: text("id |
| 76 | identifier: text("identifier |
| 77 | name: text("name |
| 78 | accessTokenTtl: integer("access_token_ttl |
| 79 | refreshTokenTtl: integer("refresh_token_ttl |
| 80 | signingAlgorithm: text("signing_algorithm |
| 81 | signingKeyId: text("signing_key_id |
| 82 | allowedScopes: text("allowed_scopes |
| 83 | customClaims: jsonb("custom_claims |
| 84 | dpopBoundAccessTokensRequired: boolean( |
| 85 | "dpop_bound_access_tokens_required", |
| 86 | ).default(false), |
| 87 | disabled: boolean("disabled |
| 88 | createdAt: timestamp("created_at |
| 89 | updatedAt: timestamp("updated_at |
| 90 | policyVersion: integer("policy_version |
| 91 | metadata: jsonb("metadata |
| 92 | }); |
| 93 | |
| 94 | export const oauthClientResource = |
| 95 | "oauth_client_resource", |
| 96 | { |
| 97 | id: text("id |
| 98 | clientId: text("client_id |
| 99 | .notNull() |
| 100 | .references(() => |
| 101 | resourceId: text("resource_id |
| 102 | .notNull() |
| 103 | .references(() => |
| 104 | metadata: jsonb("metadata |
| 105 | createdAt: timestamp("created_at |
| 106 | }, |
| 107 | (table) => |
| 108 | index("oauth_client_resource_client_id_idx"). |
| 109 | index("oauth_client_resource_resource_id_idx"). |
| 110 | ], |
| 111 | ); |
| 112 | |
| 113 | export const oauthRefreshToken = |
| 114 | "oauth_refresh_token", |
| 115 | { |
| 116 | id: text("id |
| 117 | token: text("token |
| 118 | clientId: text("client_id |
| 119 | .notNull() |
| 120 | .references(() => |
| 121 | sessionId: text("session_id |
| 122 | onDelete: "set null", |
| 123 | }), |
| 124 | userId: text("user_id |
| 125 | .notNull() |
| 126 | .references(() => |
| 127 | referenceId: text("reference_id |
| 128 | authorizationCodeId: text("authorization_code_id |
| 129 | resources: text("resources |
| 130 | requestedUserInfoClaims: text("requested_user_info_claims |
| 131 | expiresAt: timestamp("expires_at |
| 132 | createdAt: timestamp("created_at |
| 133 | revoked: timestamp("revoked |
| 134 | rotatedAt: timestamp("rotated_at |
| 135 | rotationReplayResponse: text("rotation_replay_response |
| 136 | rotationReplayExpiresAt: timestamp("rotation_replay_expires_at |
| 137 | withTimezone: true, |
| 138 | }), |
| 139 | authTime: timestamp("auth_time |
| 140 | confirmation: jsonb("confirmation |
| 141 | scopes: text("scopes |
| 142 | }, |
| 143 | (table) => |
| 144 | index("oauth_refresh_token_client_id_idx"). |
| 145 | index("oauth_refresh_token_session_id_idx"). |
| 146 | index("oauth_refresh_token_user_id_idx"). |
| 147 | index("oauth_refresh_token_authorization_code_id_idx"). |
| 148 | table.authorizationCodeId, |
| 149 | ), |
| 150 | ], |
| 151 | ); |
| 152 | |
| 153 | export const oauthAccessToken = |
| 154 | "oauth_access_token", |
| 155 | { |
| 156 | id: text("id |
| 157 | token: text("token |
| 158 | clientId: text("client_id |
| 159 | .notNull() |
| 160 | .references(() => |
| 161 | sessionId: text("session_id |
| 162 | onDelete: "set null", |
| 163 | }), |
| 164 | userId: text("user_id |
| 165 | referenceId: text("reference_id |
| 166 | authorizationCodeId: text("authorization_code_id |
| 167 | resources: text("resources |
| 168 | requestedUserInfoClaims: text("requested_user_info_claims |
| 169 | refreshId: text("refresh_id |
| 170 | onDelete: "cascade", |
| 171 | }), |
| 172 | expiresAt: timestamp("expires_at |
| 173 | createdAt: timestamp("created_at |
| 174 | revoked: timestamp("revoked |
| 175 | confirmation: jsonb("confirmation |
| 176 | scopes: text("scopes |
| 177 | }, |
| 178 | (table) => |
| 179 | index("oauth_access_token_client_id_idx"). |
| 180 | index("oauth_access_token_session_id_idx"). |
| 181 | index("oauth_access_token_user_id_idx"). |
| 182 | index("oauth_access_token_authorization_code_id_idx"). |
| 183 | table.authorizationCodeId, |
| 184 | ), |
| 185 | index("oauth_access_token_refresh_id_idx"). |
| 186 | ], |
| 187 | ); |
| 188 | |
| 189 | export const oauthConsent = |
| 190 | "oauth_consent", |
| 191 | { |
| 192 | id: text("id |
| 193 | clientId: text("client_id |
| 194 | .notNull() |
| 195 | .references(() => |
| 196 | userId: text("user_id |
| 197 | referenceId: text("reference_id |
| 198 | resources: text("resources |
| 199 | requestedUserInfoClaims: text("requested_user_info_claims |
| 200 | scopes: text("scopes |
| 201 | createdAt: timestamp("created_at |
| 202 | updatedAt: timestamp("updated_at |
| 203 | }, |
| 204 | (table) => |
| 205 | index("oauth_consent_client_id_idx"). |
| 206 | index("oauth_consent_user_id_idx"). |
| 207 | ], |
| 208 | ); |
| 209 | |
| 210 | export const oauthClientAssertion = |
| 211 | id: text("id |
| 212 | expiresAt: timestamp("expires_at |
| 213 | }); |