-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
166 lines (155 loc) · 4.69 KB
/
Copy pathauth.ts
File metadata and controls
166 lines (155 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import NextAuth from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import type { NextAuthConfig } from 'next-auth';
import type { OIDCConfig } from 'next-auth/providers';
import { createProdServices } from './lib/di';
import {
IdentityError,
normalizeIdpSubject,
} from './lib/tenancy/identity';
import {
isEmailVerifiedClaim,
oidcButtonLabel,
oidcClientId,
oidcClientSecret,
oidcIssuer,
shouldIncludeOidcProvider,
} from './lib/tenancy/oidcConfig';
import {
applyJwtToSessionUser,
applyUserToJwtToken,
} from './lib/tenancy/sessionToken';
/** Phase-1 DI: auth wires through the composition root. */
const services = createProdServices();
type OidcProfile = {
sub?: string;
email?: string;
name?: string;
preferred_username?: string;
email_verified?: boolean | string;
};
/**
* Auth.js v5 — JWT sessions; credentials + optional generic OIDC (#76).
* OIDC provider registered only when OIDC env is complete (feature-env only).
*/
function buildOidcProvider(): OIDCConfig<OidcProfile> {
return {
id: 'oidc',
name: oidcButtonLabel(),
type: 'oidc',
issuer: oidcIssuer(),
clientId: oidcClientId(),
clientSecret: oidcClientSecret(),
authorization: {
params: { scope: 'openid email profile' },
},
profile(profile) {
// Email claim only — never preferred_username (not an email; email_verified
// does not attest it). Missing email → signIn fails closed.
const email = typeof profile.email === 'string' ? profile.email.trim() : '';
return {
id: profile.sub ?? '',
email: email || null,
name: typeof profile.name === 'string' ? profile.name : null,
emailVerified: isEmailVerifiedClaim(profile.email_verified)
? new Date()
: null,
};
},
};
}
function buildProviders() {
const credentials = Credentials({
id: 'credentials',
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
const emailRaw =
typeof credentials?.email === 'string' ? credentials.email : '';
const password =
typeof credentials?.password === 'string' ? credentials.password : '';
const user = await services.authenticate.authenticateCredentials(
emailRaw,
password,
);
return user;
},
});
if (shouldIncludeOidcProvider()) {
return [credentials, buildOidcProvider()];
}
return [credentials];
}
const authConfig = {
trustHost: true,
session: { strategy: 'jwt' as const },
pages: {
signIn: '/login',
},
providers: buildProviders(),
callbacks: {
async signIn({ user, account, profile }) {
if (account?.provider !== 'oidc') {
return true;
}
try {
const issuer =
(typeof account.issuer === 'string' && account.issuer.trim()) ||
oidcIssuer();
const sub =
(profile &&
typeof (profile as OidcProfile).sub === 'string' &&
(profile as OidcProfile).sub) ||
(typeof user.id === 'string' ? user.id : '');
const email =
(typeof user.email === 'string' && user.email) ||
(profile && typeof (profile as OidcProfile).email === 'string'
? (profile as OidcProfile).email
: '') ||
'';
if (!issuer || !sub || !email) {
return false;
}
const emailVerified = isEmailVerifiedClaim(
profile
? (profile as OidcProfile).email_verified
: undefined,
);
const { user: dbUser } = await services.identity.findOrCreateOidcUser({
subject: normalizeIdpSubject(issuer, String(sub)),
email,
name:
(typeof user.name === 'string' && user.name) ||
(profile && typeof (profile as OidcProfile).name === 'string'
? (profile as OidcProfile).name
: null),
emailVerified,
});
// Force JWT sub = internal users.id (not IdP sub)
user.id = dbUser.id;
user.email = dbUser.email;
user.name = dbUser.name ?? undefined;
return true;
} catch (err) {
if (err instanceof IdentityError) {
// Generic deny — no enumeration
return false;
}
throw err;
}
},
async jwt({ token, user }) {
return applyUserToJwtToken(token, user);
},
async session({ session, token }) {
if (session.user) {
applyJwtToSessionUser(session.user, token);
}
return session;
},
},
} satisfies NextAuthConfig;
export const { handlers, auth, signIn, signOut } = NextAuth(authConfig);