refactor(auth): reset the admin and count users through the Better Auth adapter

The admin password reset and the first-user check read and wrote the
Better Auth tables with raw SQL, so session delete hooks never ran on the
reset path.

- the admin is found with findUserByEmail and its credential account is
  updated by id with the configured password hasher
- sessions are removed with deleteUserSessions, which runs the session
  delete hooks
- a missing credential account logs a warning instead of updating nothing
- /api/auth-info/config counts users through the adapter
This commit is contained in:
Chris Scott committed 2026-10-24 19:26:21 -04:00
1 parent b9d8d18124
commit cbe4f7ed9e
4 files changed
+186 -131

No files matched your search

+2 -2
View File
@@ -244,7 +244,7 @@ try {
await gitAuthService.initialize(ipcServer, db)
logger.info(`Git IPC server running at ${ipcServer.ipcHandlePath}`)
await syncAdminFromEnv(auth, db)
await syncAdminFromEnv(auth)
opencodeServerManager.setDatabase(db)
detectSandboxCapability()
@@ -389,7 +389,7 @@ const previewSessionStore = new PreviewSessionStore()
const previewAvailability = createPreviewAvailability(false)
app.route('/api/auth', createAuthRoutes(auth))
app.route('/api/auth-info', createAuthInfoRoutes(auth, db))
app.route('/api/auth-info', createAuthInfoRoutes(auth))
app.route('/api/health', createHealthRoutes(db, openCodeSupervisor))
app.route('/api/mcp-oauth-proxy', createMcpOauthProxyRoutes(openCodeClient, requireAuth))
+19 -18
View File
@@ -1,10 +1,8 @@
import { Hono } from 'hono'
import { isAdminConfigured, type AuthInstance } from '../auth'
import { applyClientIpHeaders, resolveSocketIp } from '../auth/client-ip'
import { Database } from 'bun:sqlite'
import { ENV } from '@opencode-manager/shared/config/env'
import { logger } from '../utils/logger'
import { hashPassword } from 'better-auth/crypto'
export function createAuthRoutes(auth: AuthInstance): Hono {
const app = new Hono()
@@ -17,26 +15,29 @@ export function createAuthRoutes(auth: AuthInstance): Hono {
return app
}
export async function syncAdminFromEnv(auth: AuthInstance, db: Database): Promise<void> {
export async function syncAdminFromEnv(auth: AuthInstance): Promise<void> {
if (!isAdminConfigured()) return
const adminEmail = ENV.AUTH.ADMIN_EMAIL!
const adminPassword = ENV.AUTH.ADMIN_PASSWORD!
const existingUser = db.prepare('SELECT id, email FROM "user" WHERE lower(email) = lower(?)').get(adminEmail) as { id: string; email: string } | undefined
const ctx = await auth.$context
const existingAdmin = await ctx.internalAdapter.findUserByEmail(adminEmail)
if (existingUser) {
if (existingAdmin) {
if (ENV.AUTH.ADMIN_PASSWORD_RESET) {
const hashedPassword = await hashPassword(adminPassword)
const resetAdminCredentials = db.transaction(() => {
db.prepare('UPDATE "account" SET password = ? WHERE "userId" = ? AND "providerId" = ?').run(
hashedPassword,
existingUser.id,
'credential'
)
db.prepare('DELETE FROM "session" WHERE "userId" = ?').run(existingUser.id)
})
resetAdminCredentials()
const credentialAccount = (await ctx.internalAdapter.findAccounts(existingAdmin.user.id))
.find((account) => account.providerId === 'credential')
if (credentialAccount) {
await ctx.internalAdapter.updateAccount(credentialAccount.id, {
password: await ctx.password.hash(adminPassword),
})
} else {
logger.warn(`No credential account found for ${adminEmail}; skipped the password reset`)
}
await ctx.internalAdapter.deleteUserSessions(existingAdmin.user.id)
logger.info(`Admin password reset from environment for ${adminEmail}`)
logger.warn('Remove ADMIN_PASSWORD_RESET=true from environment after password reset')
}
@@ -57,7 +58,7 @@ export async function syncAdminFromEnv(auth: AuthInstance, db: Database): Promis
}
}
export function createAuthInfoRoutes(auth: AuthInstance, db: Database) {
export function createAuthInfoRoutes(auth: AuthInstance) {
const app = new Hono()
app.get('/config', async (c) => {
@@ -75,13 +76,13 @@ export function createAuthInfoRoutes(auth: AuthInstance, db: Database) {
enabledProviders.push('passkey')
const hasUsers = db.prepare('SELECT COUNT(*) as count FROM "user"').get() as { count: number }
const userCount = await (await auth.$context).adapter.count({ model: 'user' })
const adminConfigured = isAdminConfigured()
return c.json({
enabledProviders,
registrationEnabled: !adminConfigured,
isFirstUser: hasUsers.count === 0,
isFirstUser: userCount === 0,
adminConfigured,
})
})
+1 -1
View File
@@ -78,7 +78,7 @@ describe('createAuth', () => {
body: { email: 'other@example.com', password: 'test-password', name: 'Other' },
})).rejects.toThrow('Registration is disabled')
await syncAdminFromEnv(auth, db)
await syncAdminFromEnv(auth)
const response = await auth.handler(new Request('http://localhost:5173/api/auth/sign-in/email', {
method: 'POST',
headers: { 'content-type': 'application/json', origin: 'http://localhost:5173' },
+164 -110
View File
@@ -1,7 +1,8 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { createTestDb } from '../helpers/assistant-workspace'
import { createAuthRoutes, createAuthInfoRoutes, syncAdminFromEnv } from '../../src/routes/auth'
import type { AuthInstance, Session } from '../../src/auth'
import { createAuthInfoRoutes, createAuthRoutes, syncAdminFromEnv } from '../../src/routes/auth'
import { createAuth, type AuthInstance, type Session } from '../../src/auth'
import { registerSessionConnection } from '../../src/services/session-connections'
import { createAuthDatabase } from '../helpers/auth-database'
const { ENV } = vi.hoisted(() => ({
ENV: {
@@ -27,18 +28,18 @@ const { ENV } = vi.hoisted(() => ({
},
}))
vi.mock('@opencode-manager/shared/config/env', () => ({ ENV }))
vi.mock('@opencode-manager/shared/config/env', () => ({
ENV,
getTrustedOrigins: () => ENV.AUTH.TRUSTED_ORIGINS
.split(',')
.map((origin: string) => origin.trim())
.filter((origin: string) => origin.length > 0),
}))
vi.mock('../../src/utils/logger', () => ({
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
}))
const { hashPasswordMock } = vi.hoisted(() => ({
hashPasswordMock: vi.fn(),
}))
vi.mock('better-auth/crypto', () => ({ hashPassword: hashPasswordMock }))
import { logger } from '../../src/utils/logger'
const mockLoggerInfo = vi.mocked(logger.info)
@@ -46,6 +47,9 @@ const mockLoggerWarn = vi.mocked(logger.warn)
const mockLoggerError = vi.mocked(logger.error)
const mockLoggerDebug = vi.mocked(logger.debug)
const ORIGIN = 'http://localhost:5173'
const SIGN_IN_URL = `${ORIGIN}/api/auth/sign-in/email`
function resetEnv(): void {
ENV.AUTH.TRUSTED_ORIGINS = 'http://localhost:5173,http://localhost:5003'
ENV.AUTH.SECURE_COOKIES = false
@@ -60,27 +64,42 @@ function resetEnv(): void {
ENV.AUTH.DISCORD_CLIENT_SECRET = undefined
}
function insertUser(db: ReturnType<typeof createTestDb>, id: string, email: string): void {
db.prepare(
'INSERT INTO "user" (id, name, email, emailVerified, createdAt, updatedAt, role) VALUES (?, ?, ?, ?, ?, ?, ?)'
).run(id, 'Test User', email, 0, Date.now(), Date.now(), 'user')
function cookieHeaders(response: Response): Headers {
return new Headers({
cookie: response.headers.getSetCookie().map((cookie) => cookie.split(';')[0]).join('; '),
})
}
function insertCredentialAccount(db: ReturnType<typeof createTestDb>, id: string, userId: string, password: string): void {
db.prepare(
'INSERT INTO "account" (id, accountId, providerId, userId, password, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?)'
).run(id, userId, 'credential', userId, password, Date.now(), Date.now())
function signInRequest(auth: AuthInstance, email: string, password: string): Promise<Response> {
return auth.handler(new Request(SIGN_IN_URL, {
method: 'POST',
headers: { 'content-type': 'application/json', origin: ORIGIN },
body: JSON.stringify({ email, password }),
}))
}
function insertSession(db: ReturnType<typeof createTestDb>, id: string, userId: string, token: string): void {
db.prepare(
'INSERT INTO "session" (id, expiresAt, token, createdAt, updatedAt, userId) VALUES (?, ?, ?, ?, ?, ?)'
).run(id, Date.now() + 86_400_000, token, Date.now(), Date.now(), userId)
async function createUserSession(auth: AuthInstance, email: string, password: string): Promise<Headers> {
const response = await signInRequest(auth, email, password)
expect(response.status).toBe(200)
return cookieHeaders(response)
}
function listSessionIds(db: ReturnType<typeof createTestDb>, userId: string): string[] {
const rows = db.prepare('SELECT id FROM "session" WHERE "userId" = ? ORDER BY id').all(userId) as { id: string }[]
return rows.map((row) => row.id)
async function countRows(auth: AuthInstance, model: 'user' | 'session', userId?: string): Promise<number> {
const ctx = await auth.$context
return ctx.adapter.count({
model,
where: userId ? [{ field: 'userId', value: userId }] : undefined,
})
}
async function findPasswordHash(auth: AuthInstance, userId: string): Promise<string> {
const ctx = await auth.$context
const account = await ctx.adapter.findOne<{ password: string }>({
model: 'account',
where: [{ field: 'userId', value: userId }],
})
expect(account?.password).toBeTypeOf('string')
return account!.password
}
function createSession(): Session {
@@ -153,159 +172,182 @@ describe('createAuthRoutes', () => {
})
describe('syncAdminFromEnv', () => {
let db: ReturnType<typeof createTestDb>
let signUpEmail: ReturnType<typeof vi.fn>
let db: ReturnType<typeof createAuthDatabase>
let auth: AuthInstance
beforeEach(() => {
resetEnv()
hashPasswordMock.mockImplementation(async (password: string) => `hashed:${password}`)
db = createTestDb()
db = createAuthDatabase()
auth = createAuth(db)
vi.clearAllMocks()
signUpEmail = vi.fn(async () => ({}))
})
afterEach(() => {
afterEach(async () => {
await (await auth.$context).checkSchema?.()
db.close()
})
it('does nothing when the admin credentials are not configured', async () => {
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth)
await syncAdminFromEnv(auth, db)
expect(signUpEmail).not.toHaveBeenCalled()
expect(await countRows(auth, 'user')).toBe(0)
expect(mockLoggerInfo).not.toHaveBeenCalled()
})
it('does nothing when only one admin credential is configured', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
await syncAdminFromEnv(auth)
expect(signUpEmail).not.toHaveBeenCalled()
expect(await countRows(auth, 'user')).toBe(0)
})
it('returns without resetting when an existing user has no password reset flag', async () => {
await auth.api.signUpEmail({ body: { email: 'admin@example.com', password: 'old-password', name: 'Admin' } })
const cookies = await createUserSession(auth, 'admin@example.com', 'old-password')
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
insertUser(db, 'user-1', 'admin@example.com')
insertCredentialAccount(db, 'account-1', 'user-1', 'old-hash')
insertSession(db, 'session-1', 'user-1', 'token-1')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
await syncAdminFromEnv(auth, db)
await syncAdminFromEnv(auth)
expect(signUpEmail).not.toHaveBeenCalled()
expect(hashPasswordMock).not.toHaveBeenCalled()
const account = db.prepare('SELECT password FROM "account" WHERE "userId" = ?').get('user-1') as { password: string }
expect(account.password).toBe('old-hash')
expect(listSessionIds(db, 'user-1')).toEqual(['session-1'])
})
expect((await signInRequest(auth, 'admin@example.com', 'old-password')).status).toBe(200)
expect((await signInRequest(auth, 'admin@example.com', 'new-password')).status).toBe(401)
expect(await auth.api.getSession({ headers: cookies })).not.toBeNull()
}, 30000)
it('finds the existing admin user when ADMIN_EMAIL differs only by case', async () => {
await auth.api.signUpEmail({ body: { email: 'admin@example.com', password: 'old-password', name: 'Admin' } })
ENV.AUTH.ADMIN_EMAIL = 'Admin@Example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
insertUser(db, 'user-1', 'admin@example.com')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
await syncAdminFromEnv(auth, db)
await syncAdminFromEnv(auth)
expect(signUpEmail).not.toHaveBeenCalled()
expect(db.prepare('SELECT COUNT(*) AS count FROM "user"').get()).toEqual({ count: 1 })
})
expect(await countRows(auth, 'user')).toBe(1)
expect((await signInRequest(auth, 'Admin@Example.com', 'new-password')).status).toBe(200)
expect((await signInRequest(auth, 'admin@example.com', 'old-password')).status).toBe(401)
}, 30000)
it('resets the credential account password for an existing user', async () => {
await auth.api.signUpEmail({ body: { email: 'admin@example.com', password: 'old-password', name: 'Admin' } })
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
insertUser(db, 'user-1', 'admin@example.com')
insertCredentialAccount(db, 'account-1', 'user-1', 'old-hash')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
await syncAdminFromEnv(auth)
expect(hashPasswordMock).toHaveBeenCalledWith('new-password')
expect(signUpEmail).not.toHaveBeenCalled()
const account = db.prepare('SELECT password FROM "account" WHERE "userId" = ?').get('user-1') as { password: string }
expect(account.password).toBe('hashed:new-password')
expect((await signInRequest(auth, 'admin@example.com', 'new-password')).status).toBe(200)
expect((await signInRequest(auth, 'admin@example.com', 'old-password')).status).toBe(401)
expect(mockLoggerInfo).toHaveBeenCalledWith(expect.stringContaining('Admin password reset'))
expect(mockLoggerWarn).toHaveBeenCalledWith(expect.stringContaining('ADMIN_PASSWORD_RESET'))
})
}, 30000)
it('revokes the existing sessions of the admin whose password was reset', async () => {
it('revokes every session of the admin whose password was reset', async () => {
await auth.api.signUpEmail({ body: { email: 'admin@example.com', password: 'admin-password', name: 'Admin' } })
await auth.api.signUpEmail({ body: { email: 'other@example.com', password: 'other-password', name: 'Other' } })
const firstAdminCookies = await createUserSession(auth, 'admin@example.com', 'admin-password')
await createUserSession(auth, 'admin@example.com', 'admin-password')
const otherCookies = await createUserSession(auth, 'other@example.com', 'other-password')
const adminId = (await auth.api.getSession({ headers: firstAdminCookies }))!.user.id
const otherId = (await auth.api.getSession({ headers: otherCookies }))!.user.id
const otherSessionsBefore = await countRows(auth, 'session', otherId)
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
insertUser(db, 'user-1', 'admin@example.com')
insertUser(db, 'user-2', 'other@example.com')
insertCredentialAccount(db, 'account-1', 'user-1', 'old-hash')
insertSession(db, 'session-1', 'user-1', 'token-1')
insertSession(db, 'session-2', 'user-1', 'token-2')
insertSession(db, 'session-3', 'user-2', 'token-3')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
await syncAdminFromEnv(auth)
expect(listSessionIds(db, 'user-1')).toEqual([])
expect(listSessionIds(db, 'user-2')).toEqual(['session-3'])
})
expect(await countRows(auth, 'session', adminId)).toBe(0)
expect(await countRows(auth, 'session', otherId)).toBe(otherSessionsBefore)
}, 30000)
it('rolls back the password reset when revoking the sessions fails', async () => {
it('runs the session delete hook for every revoked admin session', async () => {
await auth.api.signUpEmail({ body: { email: 'admin@example.com', password: 'admin-password', name: 'Admin' } })
const cookies = await createUserSession(auth, 'admin@example.com', 'admin-password')
const sessionId = (await auth.api.getSession({ headers: cookies }))!.session.id
const close = vi.fn()
registerSessionConnection(sessionId, close)
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
insertUser(db, 'user-1', 'admin@example.com')
insertCredentialAccount(db, 'account-1', 'user-1', 'old-hash')
await syncAdminFromEnv(auth)
expect(close).toHaveBeenCalledTimes(1)
}, 30000)
it('warns and still revokes the sessions when the admin has no credential account', async () => {
await auth.api.signUpEmail({ body: { email: 'admin@example.com', password: 'admin-password', name: 'Admin' } })
const cookies = await createUserSession(auth, 'admin@example.com', 'admin-password')
const adminId = (await auth.api.getSession({ headers: cookies }))!.user.id
const ctx = await auth.$context
await ctx.adapter.deleteMany({ model: 'account', where: [{ field: 'userId', value: adminId }] })
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
await syncAdminFromEnv(auth)
expect(mockLoggerWarn).toHaveBeenCalledWith(expect.stringContaining('No credential account found'))
expect(mockLoggerInfo).toHaveBeenCalledWith(expect.stringContaining('Admin password reset'))
expect(await countRows(auth, 'session', adminId)).toBe(0)
}, 30000)
it('keeps the new password when revoking the sessions fails', async () => {
await auth.api.signUpEmail({ body: { email: 'admin@example.com', password: 'old-password', name: 'Admin' } })
const cookies = await createUserSession(auth, 'admin@example.com', 'old-password')
const adminId = (await auth.api.getSession({ headers: cookies }))!.user.id
db.exec('DROP TABLE "session"')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
await expect(syncAdminFromEnv(auth, db)).rejects.toThrow()
await expect(syncAdminFromEnv(auth)).rejects.toThrow()
const account = db.prepare('SELECT password FROM "account" WHERE "userId" = ?').get('user-1') as { password: string }
expect(account.password).toBe('old-hash')
})
const ctx = await auth.$context
expect(await ctx.password.verify({
password: 'new-password',
hash: await findPasswordHash(auth, adminId),
})).toBe(true)
}, 30000)
it('creates a new admin user when none exists', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
await syncAdminFromEnv(auth)
expect(signUpEmail).toHaveBeenCalledWith({
body: { email: 'admin@example.com', password: 'admin-password', name: 'Admin' },
})
expect(await countRows(auth, 'user')).toBe(1)
expect((await signInRequest(auth, 'admin@example.com', 'admin-password')).status).toBe(200)
expect(mockLoggerInfo).toHaveBeenCalledWith(expect.stringContaining('Admin user created'))
})
}, 30000)
it('logs and swallows sign-up failures', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
const failure = new Error('sign up failed')
signUpEmail.mockRejectedValueOnce(failure)
const auth = { api: { signUpEmail } } as unknown as AuthInstance
ENV.AUTH.ADMIN_PASSWORD = 'short'
await expect(syncAdminFromEnv(auth, db)).resolves.toBeUndefined()
await expect(syncAdminFromEnv(auth)).resolves.toBeUndefined()
expect(mockLoggerError).toHaveBeenCalledWith('Failed to create admin user from environment:', failure)
})
expect(await countRows(auth, 'user')).toBe(0)
expect(mockLoggerError).toHaveBeenCalledWith('Failed to create admin user from environment:', expect.anything())
}, 30000)
})
describe('createAuthInfoRoutes', () => {
let db: ReturnType<typeof createTestDb>
let getSession: ReturnType<typeof vi.fn>
describe('createAuthInfoRoutes /config', () => {
let db: ReturnType<typeof createAuthDatabase>
let auth: AuthInstance
let app: ReturnType<typeof createAuthInfoRoutes>
beforeEach(() => {
resetEnv()
db = createAuthDatabase()
auth = createAuth(db)
app = createAuthInfoRoutes(auth)
vi.clearAllMocks()
db = createTestDb()
getSession = vi.fn()
app = createAuthInfoRoutes({ api: { getSession } } as unknown as AuthInstance, db)
})
afterEach(() => {
afterEach(async () => {
await (await auth.$context).checkSchema?.()
db.close()
})
@@ -358,17 +400,25 @@ describe('createAuthInfoRoutes', () => {
})
it('reports that it is not the first user once a user exists', async () => {
insertUser(db, 'user-1', 'user@example.com')
await auth.api.signUpEmail({ body: { email: 'user@example.com', password: 'user-password', name: 'User' } })
const res = await app.fetch(new Request('http://localhost/config'))
const json = await res.json() as { isFirstUser: boolean }
expect(json.isFirstUser).toBe(false)
}, 30000)
})
describe('createAuthInfoRoutes /me', () => {
beforeEach(() => {
resetEnv()
vi.clearAllMocks()
})
it('returns the session and user from GET /me', async () => {
const session = createSession()
getSession.mockResolvedValueOnce(session)
const getSession = vi.fn(async () => session)
const app = createAuthInfoRoutes({ api: { getSession } } as unknown as AuthInstance)
const res = await app.fetch(new Request('http://localhost/me'))
const json = await res.json() as { user: unknown; session: { id: string; expiresAt: string } }
@@ -386,7 +436,8 @@ describe('createAuthInfoRoutes', () => {
})
it('returns nulls from GET /me when no session is found', async () => {
getSession.mockResolvedValueOnce(null)
const getSession = vi.fn(async () => null)
const app = createAuthInfoRoutes({ api: { getSession } } as unknown as AuthInstance)
const res = await app.fetch(new Request('http://localhost/me'))
@@ -394,7 +445,10 @@ describe('createAuthInfoRoutes', () => {
})
it('returns nulls from GET /me when the session lookup throws', async () => {
getSession.mockRejectedValueOnce(new Error('database unavailable'))
const getSession = vi.fn(async () => {
throw new Error('database unavailable')
})
const app = createAuthInfoRoutes({ api: { getSession } } as unknown as AuthInstance)
const res = await app.fetch(new Request('http://localhost/me'))