-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathconfig.ts
More file actions
146 lines (132 loc) · 4.44 KB
/
config.ts
File metadata and controls
146 lines (132 loc) · 4.44 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
import {
GitHubConfigSchema,
GoogleConfigSchema,
MicrosoftConfigSchema,
OktaConfigSchema,
GitHubConfig,
GoogleConfig,
MicrosoftConfig,
OktaConfig,
AuthConfig,
AuthConfigSchema,
} from "app-types/authentication";
// Conditionally import React taint
let experimental_taintUniqueValue: any = () => {};
try {
// Only use taint in Next.js runtime
if (typeof window !== "undefined" || process.env.NEXT_RUNTIME) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const react = require("react");
experimental_taintUniqueValue = react.experimental_taintUniqueValue;
}
} catch (_e) {
// No-op for non-React contexts
}
import { parseEnvBoolean } from "../utils";
function parseSocialAuthConfigs() {
const configs: {
github?: GitHubConfig;
google?: GoogleConfig;
microsoft?: MicrosoftConfig;
okta?: OktaConfig;
} = {};
// DISABLE_SIGN_UP only applies to OAuth signups, not email signups
const disableSignUp = parseEnvBoolean(process.env.DISABLE_SIGN_UP);
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
const githubResult = GitHubConfigSchema.safeParse({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
disableSignUp,
});
if (githubResult.success) {
configs.github = githubResult.data;
experimental_taintUniqueValue(
"Do not pass GITHUB_CLIENT_SECRET to the client",
configs,
configs.github.clientSecret,
);
}
}
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
const forceAccountSelection = parseEnvBoolean(
process.env.GOOGLE_FORCE_ACCOUNT_SELECTION,
);
const googleConfig: GoogleConfig = {
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
...(forceAccountSelection && { prompt: "select_account" as const }),
disableSignUp,
};
const googleResult = GoogleConfigSchema.safeParse(googleConfig);
if (googleResult.success) {
configs.google = googleResult.data;
experimental_taintUniqueValue(
"Do not pass GOOGLE_CLIENT_SECRET to the client",
configs,
configs.google.clientSecret,
);
}
}
if (process.env.MICROSOFT_CLIENT_ID && process.env.MICROSOFT_CLIENT_SECRET) {
const forceAccountSelection = parseEnvBoolean(
process.env.MICROSOFT_FORCE_ACCOUNT_SELECTION,
);
const tenantId = process.env.MICROSOFT_TENANT_ID || "common";
const microsoftConfig: MicrosoftConfig = {
clientId: process.env.MICROSOFT_CLIENT_ID,
clientSecret: process.env.MICROSOFT_CLIENT_SECRET,
tenantId,
...(forceAccountSelection && { prompt: "select_account" as const }),
disableSignUp,
};
const microsoftResult = MicrosoftConfigSchema.safeParse(microsoftConfig);
if (microsoftResult.success) {
configs.microsoft = microsoftResult.data;
experimental_taintUniqueValue(
"Do not pass MICROSOFT_CLIENT_SECRET to the client",
configs,
configs.microsoft.clientSecret,
);
}
}
// Okta uses OIDC/OAuth 2.0 with issuer base URL like https://dev-xxxx.okta.com/oauth2/default
if (
process.env.OKTA_CLIENT_ID &&
process.env.OKTA_CLIENT_SECRET &&
process.env.OKTA_ISSUER
) {
const oktaResult = OktaConfigSchema.safeParse({
clientId: process.env.OKTA_CLIENT_ID,
clientSecret: process.env.OKTA_CLIENT_SECRET,
issuer: process.env.OKTA_ISSUER,
disableSignUp,
});
if (oktaResult.success) {
configs.okta = oktaResult.data;
experimental_taintUniqueValue(
"Do not pass OKTA_CLIENT_SECRET to the client",
configs,
configs.okta.clientSecret,
);
}
}
return configs;
}
export function getAuthConfig(): AuthConfig {
const rawConfig = {
emailAndPasswordEnabled: process.env.DISABLE_EMAIL_SIGN_IN
? !parseEnvBoolean(process.env.DISABLE_EMAIL_SIGN_IN)
: true,
// signUpEnabled now only applies to email signups
// OAuth signups are controlled separately via DISABLE_SIGN_UP in parseSocialAuthConfigs
signUpEnabled: process.env.DISABLE_EMAIL_SIGN_UP
? !parseEnvBoolean(process.env.DISABLE_EMAIL_SIGN_UP)
: true,
socialAuthenticationProviders: parseSocialAuthConfigs(),
};
const result = AuthConfigSchema.safeParse(rawConfig);
if (!result.success) {
throw new Error(`Invalid auth configuration: ${result.error.message}`);
}
return result.data;
}