Keep NextAuth as the OIDC/session layer and add a trusted Go provisioning endpoint that maps verified Authelia subjects to local users. Existing password accounts require explicit admin linking; new users are provisioned without a password. Enable the Authelia login path and update docs. Also fix test schema search_path so pg_trgm opclasses resolve.
This commit is contained in:
+77
-7
@@ -18,7 +18,60 @@ const signClientIP = (secret: string, ip: string): { ts: string; sig: string } =
|
||||
return { ts, sig: createHmac('sha256', secret).update(`${ip}|${ts}`).digest('hex') }
|
||||
}
|
||||
|
||||
// NextAuth owns the browser session and the eventual client-managed OIDC flow.
|
||||
// Exchange a verified Authelia identity for a local Go user. The Next server
|
||||
// is the only holder of AUTH_SECRET; Go accepts the resulting oidc-provision
|
||||
// token only on this endpoint. The OIDC subject is an account-link key, never
|
||||
// a local User.id.
|
||||
const provisionAuthelia = async (account: any, profile: any): Promise<{ id: string; role?: string; tokenVersion: number } | null> => {
|
||||
const secret = process.env.NEXTAUTH_SECRET || process.env.AUTH_SECRET
|
||||
const issuer = process.env.OIDC_ISSUER?.trim()
|
||||
const subject = typeof account?.providerAccountId === 'string' ? account.providerAccountId : ''
|
||||
if (!secret || !issuer || !subject) return null
|
||||
const serviceToken = jwt.sign(
|
||||
{ typ: 'oidc-provision', aud: 'superbrain-api' },
|
||||
secret,
|
||||
{ algorithm: 'HS256', issuer: 'superbrain', expiresIn: '60s' },
|
||||
)
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), 5000)
|
||||
try {
|
||||
const response = await fetch(`${process.env.API_BASE_URL || 'http://localhost:8080'}/api/auth/oidc/identity`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${serviceToken}` },
|
||||
cache: 'no-store',
|
||||
signal: controller.signal,
|
||||
body: JSON.stringify({
|
||||
issuer,
|
||||
subject,
|
||||
email: profile?.email ?? null,
|
||||
emailVerified: profile?.email_verified === true,
|
||||
name: profile?.name ?? profile?.preferred_username ?? null,
|
||||
image: profile?.picture ?? null,
|
||||
}),
|
||||
})
|
||||
if (!response.ok) {
|
||||
// 409 means the local account needs explicit linking; never bypass it.
|
||||
console.error(`oidc provision: Go answered ${response.status}`)
|
||||
return null
|
||||
}
|
||||
const data = await response.json()
|
||||
const id = data?.user?.id
|
||||
const tokenVersion = data?.tokenVersion
|
||||
if (typeof id !== 'string' || typeof tokenVersion !== 'number') {
|
||||
console.error('oidc provision: malformed Go response')
|
||||
return null
|
||||
}
|
||||
return { id, role: typeof data.user.role === 'string' ? data.user.role : undefined, tokenVersion }
|
||||
} catch (error) {
|
||||
const kind = error instanceof Error ? error.name : 'Error'
|
||||
console.error(`oidc provision: Go unreachable (${kind})`)
|
||||
return null
|
||||
} finally {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
|
||||
// NextAuth owns the browser session and the client-managed OIDC flow.
|
||||
// Password verification and data ownership live in Go, not Prisma.
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
trustHost: true,
|
||||
@@ -88,14 +141,31 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
],
|
||||
callbacks: {
|
||||
async signIn({ account }) {
|
||||
// Client integration point: exchange a verified Authelia identity for a
|
||||
// local Go User/Account before enabling OIDC. No subject is trusted as a
|
||||
// database user ID; the backend exchange stays an explicit 501 stub.
|
||||
return account?.provider !== 'authelia'
|
||||
// Authelia identities are exchanged for local Go users in the jwt callback.
|
||||
return account?.provider === 'authelia' || account?.provider === 'credentials'
|
||||
},
|
||||
async jwt({ token, user, account, profile, trigger }) {
|
||||
if (trigger === 'signIn' && account?.provider === 'authelia') {
|
||||
const local = await provisionAuthelia(account, profile)
|
||||
if (!local) throw new Error('Authelia identity mapping failed')
|
||||
token.id = local.id
|
||||
token.ver = local.tokenVersion
|
||||
token.role = local.role
|
||||
return token
|
||||
}
|
||||
if (user) {
|
||||
token.id = user.id
|
||||
token.ver = user.tokenVersion
|
||||
token.role = user.role
|
||||
}
|
||||
return token
|
||||
},
|
||||
async jwt({ token, user }) { if (user) { token.id = user.id; token.ver = user.tokenVersion } return token },
|
||||
async session({ session, token }) {
|
||||
if (session.user) { session.user.id = token.id as string; session.user.ver = token.ver }
|
||||
if (session.user) {
|
||||
session.user.id = token.id as string
|
||||
session.user.ver = token.ver
|
||||
session.user.role = token.role
|
||||
}
|
||||
return session
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user