mirror of
https://github.com/bitbaum/morulia.git
synced 2026-12-04 05:48:23 +01:00
* feat: the shared bitbaum sign-in (OIDC provider, pairwise subjects) One OIDC provider holding the Google, Apple, GitHub, X and Meta credentials once, for every accountkit app (fleet STACK.md, "Identity"): - better-auth 1.7 + @better-auth/oauth-provider + jwt (RS256); discovery at the origin, authorize with PKCE S256 required, token with client_secret_post, userinfo, JWKS - pairwise subject per app (HMAC of redirect host + user); the email scope is answered with a per-app .invalid placeholder - upstream providers each on only when all their env is set, asked for no email and no profile; provider tokens are never stored - consent-free first-party clients from a registry (lib/clients.ts), synced at start from per-app secrets in env - one pairwise-subject derivation (lib/subject.ts), pinned to a literal, with a persona input for future non-root lives only - tests: PKCE flow end to end against fake upstreams, pairwise sub differs per app and is stable per app, two providers reporting one verified address stay two people, and accountkit's real runtime signs in with SIGNIN_* env alone Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9 * build: standalone output for the self-host deploy loki deploy.sh ships .next/standalone and stops without it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9 * ci: browser smoke on the production build The fleet rollout gate holds any consumer of a shared package (accountkit, design-tokens, limitkit) until ci.yml and browser-smoke.yml pass on the commit. This one starts the production build against PostgreSQL built from drizzle/ and drives Chromium through a real authorize request from Solon: health with the database up, discovery for the given issuer, and the app's sign-in page at 390px with no sideways scroll and no console errors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9 --------- Co-authored-by: Claude <noreply@anthropic.com>
398 lines
13 KiB
TypeScript
398 lines
13 KiB
TypeScript
// Generated by `pnpm dlx auth@1.7.12 generate --adapter drizzle --dialect postgresql`
|
|
// (better-auth core + jwt + @better-auth/oauth-provider), then one addition:
|
|
// the unique (provider_id, account_id) index on account.
|
|
import { relations } from "drizzle-orm";
|
|
import {
|
|
pgTable,
|
|
text,
|
|
timestamp,
|
|
boolean,
|
|
integer,
|
|
jsonb,
|
|
index,
|
|
uniqueIndex,
|
|
} from "drizzle-orm/pg-core";
|
|
|
|
export const user = pgTable("user", {
|
|
id: text("id").primaryKey(),
|
|
name: text("name").notNull(),
|
|
email: text("email").notNull().unique(),
|
|
emailVerified: boolean("email_verified").default(false).notNull(),
|
|
image: text("image"),
|
|
createdAt: timestamp("created_at").defaultNow().notNull(),
|
|
updatedAt: timestamp("updated_at")
|
|
.defaultNow()
|
|
.$onUpdate(() => /* @__PURE__ */ new Date())
|
|
.notNull(),
|
|
});
|
|
|
|
export const session = pgTable(
|
|
"session",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
expiresAt: timestamp("expires_at").notNull(),
|
|
token: text("token").notNull().unique(),
|
|
createdAt: timestamp("created_at").defaultNow().notNull(),
|
|
updatedAt: timestamp("updated_at")
|
|
.$onUpdate(() => /* @__PURE__ */ new Date())
|
|
.notNull(),
|
|
ipAddress: text("ip_address"),
|
|
userAgent: text("user_agent"),
|
|
userId: text("user_id")
|
|
.notNull()
|
|
.references(() => user.id, { onDelete: "cascade" }),
|
|
},
|
|
(table) => [index("session_userId_idx").on(table.userId)],
|
|
);
|
|
|
|
export const account = pgTable(
|
|
"account",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
accountId: text("account_id").notNull(),
|
|
providerId: text("provider_id").notNull(),
|
|
userId: text("user_id")
|
|
.notNull()
|
|
.references(() => user.id, { onDelete: "cascade" }),
|
|
accessToken: text("access_token"),
|
|
refreshToken: text("refresh_token"),
|
|
idToken: text("id_token"),
|
|
accessTokenExpiresAt: timestamp("access_token_expires_at"),
|
|
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
|
|
scope: text("scope"),
|
|
password: text("password"),
|
|
createdAt: timestamp("created_at").defaultNow().notNull(),
|
|
updatedAt: timestamp("updated_at")
|
|
.$onUpdate(() => /* @__PURE__ */ new Date())
|
|
.notNull(),
|
|
},
|
|
(table) => [
|
|
index("account_userId_idx").on(table.userId),
|
|
// One person per (provider, subject): a race between two first sign-ins
|
|
// must fail, not create two people.
|
|
uniqueIndex("account_provider_subject_uidx").on(table.providerId, table.accountId),
|
|
],
|
|
);
|
|
|
|
export const verification = pgTable(
|
|
"verification",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
identifier: text("identifier").notNull(),
|
|
value: text("value").notNull(),
|
|
expiresAt: timestamp("expires_at").notNull(),
|
|
createdAt: timestamp("created_at").defaultNow().notNull(),
|
|
updatedAt: timestamp("updated_at")
|
|
.defaultNow()
|
|
.$onUpdate(() => /* @__PURE__ */ new Date())
|
|
.notNull(),
|
|
},
|
|
(table) => [index("verification_identifier_idx").on(table.identifier)],
|
|
);
|
|
|
|
export const jwks = pgTable("jwks", {
|
|
id: text("id").primaryKey(),
|
|
publicKey: text("public_key").notNull(),
|
|
privateKey: text("private_key").notNull(),
|
|
createdAt: timestamp("created_at").notNull(),
|
|
expiresAt: timestamp("expires_at"),
|
|
alg: text("alg"),
|
|
crv: text("crv"),
|
|
});
|
|
|
|
export const oauthClient = pgTable(
|
|
"oauth_client",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
clientId: text("client_id").notNull().unique(),
|
|
clientSecret: text("client_secret"),
|
|
clientDiscoveryId: text("client_discovery_id"),
|
|
disabled: boolean("disabled").default(false),
|
|
skipConsent: boolean("skip_consent"),
|
|
enableEndSession: boolean("enable_end_session"),
|
|
subjectType: text("subject_type"),
|
|
scopes: text("scopes").array(),
|
|
clientCredentialsScopes: text("client_credentials_scopes")
|
|
.array()
|
|
.default([]),
|
|
userId: text("user_id").references(() => user.id, { onDelete: "cascade" }),
|
|
createdAt: timestamp("created_at"),
|
|
updatedAt: timestamp("updated_at"),
|
|
name: text("name"),
|
|
uri: text("uri"),
|
|
icon: text("icon"),
|
|
contacts: text("contacts").array(),
|
|
tos: text("tos"),
|
|
policy: text("policy"),
|
|
softwareId: text("software_id"),
|
|
softwareVersion: text("software_version"),
|
|
softwareStatement: text("software_statement"),
|
|
redirectUris: text("redirect_uris").array().notNull(),
|
|
postLogoutRedirectUris: text("post_logout_redirect_uris").array(),
|
|
backchannelLogoutUri: text("backchannel_logout_uri"),
|
|
backchannelLogoutSessionRequired: boolean(
|
|
"backchannel_logout_session_required",
|
|
),
|
|
tokenEndpointAuthMethod: text("token_endpoint_auth_method"),
|
|
applicationType: text("application_type"),
|
|
jwks: text("jwks"),
|
|
jwksUri: text("jwks_uri"),
|
|
grantTypes: text("grant_types").array(),
|
|
responseTypes: text("response_types").array(),
|
|
requirePKCE: boolean("require_pkce"),
|
|
dpopBoundAccessTokens: boolean("dpop_bound_access_tokens").default(false),
|
|
referenceId: text("reference_id"),
|
|
metadata: jsonb("metadata"),
|
|
},
|
|
(table) => [index("oauthClient_userId_idx").on(table.userId)],
|
|
);
|
|
|
|
export const oauthResource = pgTable("oauth_resource", {
|
|
id: text("id").primaryKey(),
|
|
identifier: text("identifier").notNull().unique(),
|
|
name: text("name").notNull(),
|
|
accessTokenTtl: integer("access_token_ttl"),
|
|
refreshTokenTtl: integer("refresh_token_ttl"),
|
|
signingAlgorithm: text("signing_algorithm"),
|
|
signingKeyId: text("signing_key_id"),
|
|
allowedScopes: text("allowed_scopes").array(),
|
|
customClaims: jsonb("custom_claims"),
|
|
dpopBoundAccessTokensRequired: boolean(
|
|
"dpop_bound_access_tokens_required",
|
|
).default(false),
|
|
disabled: boolean("disabled").default(false),
|
|
createdAt: timestamp("created_at"),
|
|
updatedAt: timestamp("updated_at"),
|
|
policyVersion: integer("policy_version").default(1),
|
|
metadata: jsonb("metadata"),
|
|
});
|
|
|
|
export const oauthClientResource = pgTable(
|
|
"oauth_client_resource",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
clientId: text("client_id")
|
|
.notNull()
|
|
.references(() => oauthClient.clientId, { onDelete: "cascade" }),
|
|
resourceId: text("resource_id")
|
|
.notNull()
|
|
.references(() => oauthResource.identifier, { onDelete: "cascade" }),
|
|
metadata: jsonb("metadata"),
|
|
createdAt: timestamp("created_at"),
|
|
},
|
|
(table) => [
|
|
uniqueIndex("oauthClientResource_clientId_resourceId_uidx").on(
|
|
table.clientId,
|
|
table.resourceId,
|
|
),
|
|
index("oauthClientResource_clientId_idx").on(table.clientId),
|
|
index("oauthClientResource_resourceId_idx").on(table.resourceId),
|
|
],
|
|
);
|
|
|
|
export const oauthRefreshToken = pgTable(
|
|
"oauth_refresh_token",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
token: text("token").notNull().unique(),
|
|
clientId: text("client_id")
|
|
.notNull()
|
|
.references(() => oauthClient.clientId, { onDelete: "cascade" }),
|
|
sessionId: text("session_id").references(() => session.id, {
|
|
onDelete: "set null",
|
|
}),
|
|
userId: text("user_id")
|
|
.notNull()
|
|
.references(() => user.id, { onDelete: "cascade" }),
|
|
referenceId: text("reference_id"),
|
|
authorizationCodeId: text("authorization_code_id"),
|
|
resources: text("resources").array(),
|
|
requestedUserInfoClaims: text("requested_user_info_claims").array(),
|
|
expiresAt: timestamp("expires_at").notNull(),
|
|
createdAt: timestamp("created_at").notNull(),
|
|
revoked: timestamp("revoked"),
|
|
rotatedAt: timestamp("rotated_at"),
|
|
rotationReplayResponse: text("rotation_replay_response"),
|
|
rotationReplayExpiresAt: timestamp("rotation_replay_expires_at"),
|
|
authTime: timestamp("auth_time"),
|
|
confirmation: jsonb("confirmation"),
|
|
scopes: text("scopes").array().notNull(),
|
|
},
|
|
(table) => [
|
|
index("oauthRefreshToken_clientId_idx").on(table.clientId),
|
|
index("oauthRefreshToken_sessionId_idx").on(table.sessionId),
|
|
index("oauthRefreshToken_userId_idx").on(table.userId),
|
|
index("oauthRefreshToken_authorizationCodeId_idx").on(
|
|
table.authorizationCodeId,
|
|
),
|
|
],
|
|
);
|
|
|
|
export const oauthAccessToken = pgTable(
|
|
"oauth_access_token",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
token: text("token").notNull().unique(),
|
|
clientId: text("client_id")
|
|
.notNull()
|
|
.references(() => oauthClient.clientId, { onDelete: "cascade" }),
|
|
sessionId: text("session_id").references(() => session.id, {
|
|
onDelete: "set null",
|
|
}),
|
|
userId: text("user_id").references(() => user.id, { onDelete: "cascade" }),
|
|
referenceId: text("reference_id"),
|
|
authorizationCodeId: text("authorization_code_id"),
|
|
resources: text("resources").array(),
|
|
requestedUserInfoClaims: text("requested_user_info_claims").array(),
|
|
refreshId: text("refresh_id").references(() => oauthRefreshToken.id, {
|
|
onDelete: "cascade",
|
|
}),
|
|
expiresAt: timestamp("expires_at").notNull(),
|
|
createdAt: timestamp("created_at").notNull(),
|
|
revoked: timestamp("revoked"),
|
|
confirmation: jsonb("confirmation"),
|
|
scopes: text("scopes").array().notNull(),
|
|
},
|
|
(table) => [
|
|
index("oauthAccessToken_clientId_idx").on(table.clientId),
|
|
index("oauthAccessToken_sessionId_idx").on(table.sessionId),
|
|
index("oauthAccessToken_userId_idx").on(table.userId),
|
|
index("oauthAccessToken_authorizationCodeId_idx").on(
|
|
table.authorizationCodeId,
|
|
),
|
|
index("oauthAccessToken_refreshId_idx").on(table.refreshId),
|
|
],
|
|
);
|
|
|
|
export const oauthConsent = pgTable(
|
|
"oauth_consent",
|
|
{
|
|
id: text("id").primaryKey(),
|
|
clientId: text("client_id")
|
|
.notNull()
|
|
.references(() => oauthClient.clientId, { onDelete: "cascade" }),
|
|
userId: text("user_id").references(() => user.id, { onDelete: "cascade" }),
|
|
referenceId: text("reference_id"),
|
|
resources: text("resources").array(),
|
|
requestedUserInfoClaims: text("requested_user_info_claims").array(),
|
|
scopes: text("scopes").array().notNull(),
|
|
createdAt: timestamp("created_at").notNull(),
|
|
updatedAt: timestamp("updated_at").notNull(),
|
|
},
|
|
(table) => [
|
|
index("oauthConsent_clientId_idx").on(table.clientId),
|
|
index("oauthConsent_userId_idx").on(table.userId),
|
|
],
|
|
);
|
|
|
|
export const oauthClientAssertion = pgTable("oauth_client_assertion", {
|
|
id: text("id").primaryKey(),
|
|
expiresAt: timestamp("expires_at").notNull(),
|
|
});
|
|
|
|
export const userRelations = relations(user, ({ many }) => ({
|
|
sessions: many(session),
|
|
accounts: many(account),
|
|
oauthClients: many(oauthClient),
|
|
oauthRefreshTokens: many(oauthRefreshToken),
|
|
oauthAccessTokens: many(oauthAccessToken),
|
|
oauthConsents: many(oauthConsent),
|
|
}));
|
|
|
|
export const sessionRelations = relations(session, ({ one, many }) => ({
|
|
user: one(user, {
|
|
fields: [session.userId],
|
|
references: [user.id],
|
|
}),
|
|
oauthRefreshTokens: many(oauthRefreshToken),
|
|
oauthAccessTokens: many(oauthAccessToken),
|
|
}));
|
|
|
|
export const accountRelations = relations(account, ({ one }) => ({
|
|
user: one(user, {
|
|
fields: [account.userId],
|
|
references: [user.id],
|
|
}),
|
|
}));
|
|
|
|
export const oauthClientRelations = relations(oauthClient, ({ one, many }) => ({
|
|
user: one(user, {
|
|
fields: [oauthClient.userId],
|
|
references: [user.id],
|
|
}),
|
|
oauthClientResources: many(oauthClientResource),
|
|
oauthRefreshTokens: many(oauthRefreshToken),
|
|
oauthAccessTokens: many(oauthAccessToken),
|
|
oauthConsents: many(oauthConsent),
|
|
}));
|
|
|
|
export const oauthResourceRelations = relations(oauthResource, ({ many }) => ({
|
|
oauthClientResources: many(oauthClientResource),
|
|
}));
|
|
|
|
export const oauthClientResourceRelations = relations(
|
|
oauthClientResource,
|
|
({ one }) => ({
|
|
oauthClient: one(oauthClient, {
|
|
fields: [oauthClientResource.clientId],
|
|
references: [oauthClient.clientId],
|
|
}),
|
|
oauthResource: one(oauthResource, {
|
|
fields: [oauthClientResource.resourceId],
|
|
references: [oauthResource.identifier],
|
|
}),
|
|
}),
|
|
);
|
|
|
|
export const oauthRefreshTokenRelations = relations(
|
|
oauthRefreshToken,
|
|
({ one, many }) => ({
|
|
oauthClient: one(oauthClient, {
|
|
fields: [oauthRefreshToken.clientId],
|
|
references: [oauthClient.clientId],
|
|
}),
|
|
session: one(session, {
|
|
fields: [oauthRefreshToken.sessionId],
|
|
references: [session.id],
|
|
}),
|
|
user: one(user, {
|
|
fields: [oauthRefreshToken.userId],
|
|
references: [user.id],
|
|
}),
|
|
oauthAccessTokens: many(oauthAccessToken),
|
|
}),
|
|
);
|
|
|
|
export const oauthAccessTokenRelations = relations(
|
|
oauthAccessToken,
|
|
({ one }) => ({
|
|
oauthClient: one(oauthClient, {
|
|
fields: [oauthAccessToken.clientId],
|
|
references: [oauthClient.clientId],
|
|
}),
|
|
session: one(session, {
|
|
fields: [oauthAccessToken.sessionId],
|
|
references: [session.id],
|
|
}),
|
|
user: one(user, {
|
|
fields: [oauthAccessToken.userId],
|
|
references: [user.id],
|
|
}),
|
|
oauthRefreshToken: one(oauthRefreshToken, {
|
|
fields: [oauthAccessToken.refreshId],
|
|
references: [oauthRefreshToken.id],
|
|
}),
|
|
}),
|
|
);
|
|
|
|
export const oauthConsentRelations = relations(oauthConsent, ({ one }) => ({
|
|
oauthClient: one(oauthClient, {
|
|
fields: [oauthConsent.clientId],
|
|
references: [oauthClient.clientId],
|
|
}),
|
|
user: one(user, {
|
|
fields: [oauthConsent.userId],
|
|
references: [user.id],
|
|
}),
|
|
}));
|