2022-04-26 09:51:11 +02:00
|
|
|
import NextAuth, { Account } from 'next-auth'
|
2021-11-29 15:19:07 +01:00
|
|
|
import EmailProvider from 'next-auth/providers/email'
|
|
|
|
import GitHubProvider from 'next-auth/providers/github'
|
2022-04-26 09:50:02 +02:00
|
|
|
import GitlabProvider from 'next-auth/providers/gitlab'
|
2021-11-29 15:19:07 +01:00
|
|
|
import GoogleProvider from 'next-auth/providers/google'
|
|
|
|
import FacebookProvider from 'next-auth/providers/facebook'
|
2022-06-13 11:50:05 +02:00
|
|
|
import AzureADProvider from 'next-auth/providers/azure-ad'
|
2022-11-15 09:35:48 +01:00
|
|
|
import prisma from '@/lib/prisma'
|
2021-12-06 15:48:50 +01:00
|
|
|
import { Provider } from 'next-auth/providers'
|
2021-12-27 15:59:32 +01:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2022-02-24 11:13:19 +01:00
|
|
|
import { CustomAdapter } from './adapter'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { User } from '@typebot.io/prisma'
|
|
|
|
import { env, getAtPath, isDefined, isNotEmpty } from '@typebot.io/lib'
|
2023-03-13 11:20:28 +01:00
|
|
|
import { sendVerificationRequest } from './sendVerificationRequest'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { mockedUser } from '@/features/auth/mockedUser'
|
|
|
|
import { getNewUserInvitations } from '@/features/auth/helpers/getNewUserInvitations'
|
2021-11-29 15:19:07 +01:00
|
|
|
|
2022-03-13 08:56:10 +01:00
|
|
|
const providers: Provider[] = []
|
|
|
|
|
2022-08-08 08:21:36 +02:00
|
|
|
if (
|
|
|
|
isNotEmpty(process.env.GITHUB_CLIENT_ID) &&
|
|
|
|
isNotEmpty(process.env.GITHUB_CLIENT_SECRET)
|
|
|
|
)
|
2022-04-12 14:58:55 -05:00
|
|
|
providers.push(
|
|
|
|
GitHubProvider({
|
2022-05-30 16:40:13 +02:00
|
|
|
clientId: process.env.GITHUB_CLIENT_ID,
|
2022-04-12 14:58:55 -05:00
|
|
|
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
|
|
|
})
|
|
|
|
)
|
2021-12-06 15:48:50 +01:00
|
|
|
|
2022-06-22 07:36:11 +02:00
|
|
|
if (isNotEmpty(env('SMTP_FROM')) && process.env.SMTP_AUTH_DISABLED !== 'true')
|
2021-12-06 15:48:50 +01:00
|
|
|
providers.push(
|
2022-03-13 08:56:10 +01:00
|
|
|
EmailProvider({
|
|
|
|
server: {
|
|
|
|
host: process.env.SMTP_HOST,
|
|
|
|
port: process.env.SMTP_PORT ? Number(process.env.SMTP_PORT) : 25,
|
2022-09-01 07:40:35 +02:00
|
|
|
secure: process.env.SMTP_SECURE
|
|
|
|
? process.env.SMTP_SECURE === 'true'
|
|
|
|
: false,
|
2022-03-13 08:56:10 +01:00
|
|
|
auth: {
|
|
|
|
user: process.env.SMTP_USERNAME,
|
|
|
|
pass: process.env.SMTP_PASSWORD,
|
|
|
|
},
|
|
|
|
},
|
2022-06-22 07:36:11 +02:00
|
|
|
from: env('SMTP_FROM'),
|
2023-03-13 11:20:28 +01:00
|
|
|
sendVerificationRequest,
|
2021-12-06 15:48:50 +01:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2022-03-13 08:56:10 +01:00
|
|
|
if (
|
2022-05-30 16:40:13 +02:00
|
|
|
isNotEmpty(process.env.GOOGLE_CLIENT_ID) &&
|
2022-05-25 08:13:35 -07:00
|
|
|
isNotEmpty(process.env.GOOGLE_CLIENT_SECRET)
|
2022-03-13 08:56:10 +01:00
|
|
|
)
|
2021-12-06 15:48:50 +01:00
|
|
|
providers.push(
|
2021-11-29 15:19:07 +01:00
|
|
|
GoogleProvider({
|
2022-05-30 16:40:13 +02:00
|
|
|
clientId: process.env.GOOGLE_CLIENT_ID,
|
2021-12-06 15:48:50 +01:00
|
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2022-03-13 08:56:10 +01:00
|
|
|
if (
|
2022-05-30 16:40:13 +02:00
|
|
|
isNotEmpty(process.env.FACEBOOK_CLIENT_ID) &&
|
2022-05-25 08:13:35 -07:00
|
|
|
isNotEmpty(process.env.FACEBOOK_CLIENT_SECRET)
|
2022-03-13 08:56:10 +01:00
|
|
|
)
|
2021-12-06 15:48:50 +01:00
|
|
|
providers.push(
|
2021-11-29 15:19:07 +01:00
|
|
|
FacebookProvider({
|
2022-05-30 16:40:13 +02:00
|
|
|
clientId: process.env.FACEBOOK_CLIENT_ID,
|
2021-12-06 15:48:50 +01:00
|
|
|
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2022-04-26 09:50:02 +02:00
|
|
|
if (
|
2022-05-30 16:40:13 +02:00
|
|
|
isNotEmpty(process.env.GITLAB_CLIENT_ID) &&
|
2022-05-25 08:13:35 -07:00
|
|
|
isNotEmpty(process.env.GITLAB_CLIENT_SECRET)
|
2022-04-26 09:50:02 +02:00
|
|
|
) {
|
2022-04-30 07:10:02 -07:00
|
|
|
const BASE_URL = process.env.GITLAB_BASE_URL || 'https://gitlab.com'
|
|
|
|
providers.push(
|
|
|
|
GitlabProvider({
|
2022-05-30 16:40:13 +02:00
|
|
|
clientId: process.env.GITLAB_CLIENT_ID,
|
2022-04-30 07:10:02 -07:00
|
|
|
clientSecret: process.env.GITLAB_CLIENT_SECRET,
|
|
|
|
authorization: `${BASE_URL}/oauth/authorize?scope=read_api`,
|
|
|
|
token: `${BASE_URL}/oauth/token`,
|
|
|
|
userinfo: `${BASE_URL}/api/v4/user`,
|
2022-05-30 16:40:13 +02:00
|
|
|
name: process.env.GITLAB_NAME || 'GitLab',
|
2022-04-30 07:10:02 -07:00
|
|
|
})
|
|
|
|
)
|
2022-04-26 09:50:02 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 15:34:47 +02:00
|
|
|
if (
|
|
|
|
isNotEmpty(process.env.AZURE_AD_CLIENT_ID) &&
|
|
|
|
isNotEmpty(process.env.AZURE_AD_CLIENT_SECRET) &&
|
|
|
|
isNotEmpty(process.env.AZURE_AD_TENANT_ID)
|
|
|
|
) {
|
|
|
|
providers.push(
|
|
|
|
AzureADProvider({
|
|
|
|
clientId: process.env.AZURE_AD_CLIENT_ID,
|
|
|
|
clientSecret: process.env.AZURE_AD_CLIENT_SECRET,
|
|
|
|
tenantId: process.env.AZURE_AD_TENANT_ID,
|
2022-06-13 11:50:05 +02:00
|
|
|
})
|
2022-06-06 15:34:47 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-10 08:24:44 +01:00
|
|
|
if (isNotEmpty(process.env.CUSTOM_OAUTH_WELL_KNOWN_URL)) {
|
2023-01-02 08:29:46 +01:00
|
|
|
providers.push({
|
|
|
|
id: 'custom-oauth',
|
|
|
|
name: process.env.CUSTOM_OAUTH_NAME ?? 'Custom OAuth',
|
|
|
|
type: 'oauth',
|
2023-01-06 14:17:29 +01:00
|
|
|
clientId: process.env.CUSTOM_OAUTH_CLIENT_ID,
|
|
|
|
clientSecret: process.env.CUSTOM_OAUTH_CLIENT_SECRET,
|
2023-01-10 08:24:44 +01:00
|
|
|
wellKnown: process.env.CUSTOM_OAUTH_WELL_KNOWN_URL,
|
2023-01-02 08:29:46 +01:00
|
|
|
profile(profile) {
|
|
|
|
return {
|
|
|
|
id: getAtPath(profile, process.env.CUSTOM_OAUTH_USER_ID_PATH ?? 'id'),
|
|
|
|
name: getAtPath(
|
|
|
|
profile,
|
|
|
|
process.env.CUSTOM_OAUTH_USER_NAME_PATH ?? 'name'
|
|
|
|
),
|
|
|
|
email: getAtPath(
|
|
|
|
profile,
|
|
|
|
process.env.CUSTOM_OAUTH_USER_EMAIL_PATH ?? 'email'
|
|
|
|
),
|
|
|
|
image: getAtPath(
|
|
|
|
profile,
|
|
|
|
process.env.CUSTOM_OAUTH_USER_IMAGE_PATH ?? 'image'
|
|
|
|
),
|
|
|
|
} as User
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-28 17:57:14 +01:00
|
|
|
const handler = (req: NextApiRequest, res: NextApiResponse) => {
|
2023-04-03 09:00:39 +02:00
|
|
|
if (req.url === '/api/auth/session' && env('E2E_TEST') === 'true')
|
|
|
|
return res.send({ user: mockedUser })
|
|
|
|
if (req.method === 'HEAD') return res.status(200)
|
|
|
|
return NextAuth(req, res, {
|
2022-02-24 11:13:19 +01:00
|
|
|
adapter: CustomAdapter(prisma),
|
2022-02-11 15:30:02 +01:00
|
|
|
secret: process.env.ENCRYPTION_SECRET,
|
2022-01-28 17:57:14 +01:00
|
|
|
providers,
|
|
|
|
session: {
|
|
|
|
strategy: 'database',
|
2021-12-06 15:48:50 +01:00
|
|
|
},
|
2023-02-18 09:00:04 +01:00
|
|
|
pages: {
|
|
|
|
signIn: '/signin',
|
|
|
|
},
|
2022-01-28 17:57:14 +01:00
|
|
|
callbacks: {
|
2022-02-24 14:52:35 +01:00
|
|
|
session: async ({ session, user }) => {
|
|
|
|
const userFromDb = user as User
|
|
|
|
await updateLastActivityDate(userFromDb)
|
|
|
|
return {
|
|
|
|
...session,
|
2022-02-24 15:36:03 +01:00
|
|
|
user: userFromDb,
|
2022-02-24 14:52:35 +01:00
|
|
|
}
|
|
|
|
},
|
2022-11-03 08:13:22 +01:00
|
|
|
signIn: async ({ account, user }) => {
|
2023-01-20 09:30:25 +01:00
|
|
|
if (!account) return false
|
|
|
|
const isNewUser = !('createdAt' in user && isDefined(user.createdAt))
|
|
|
|
if (process.env.DISABLE_SIGNUP === 'true' && isNewUser && user.email) {
|
|
|
|
const { invitations, workspaceInvitations } =
|
|
|
|
await getNewUserInvitations(prisma, user.email)
|
|
|
|
if (invitations.length === 0 && workspaceInvitations.length === 0)
|
|
|
|
return false
|
|
|
|
}
|
2022-04-26 09:51:11 +02:00
|
|
|
const requiredGroups = getRequiredGroups(account.provider)
|
|
|
|
if (requiredGroups.length > 0) {
|
|
|
|
const userGroups = await getUserGroups(account)
|
|
|
|
return checkHasGroups(userGroups, requiredGroups)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
2021-12-06 15:48:50 +01:00
|
|
|
},
|
2022-01-28 17:57:14 +01:00
|
|
|
})
|
2021-12-27 15:59:32 +01:00
|
|
|
}
|
2022-01-28 17:57:14 +01:00
|
|
|
|
2022-02-24 14:52:35 +01:00
|
|
|
const updateLastActivityDate = async (user: User) => {
|
|
|
|
const datesAreOnSameDay = (first: Date, second: Date) =>
|
|
|
|
first.getFullYear() === second.getFullYear() &&
|
|
|
|
first.getMonth() === second.getMonth() &&
|
|
|
|
first.getDate() === second.getDate()
|
|
|
|
|
|
|
|
if (!datesAreOnSameDay(user.lastActivityAt, new Date()))
|
|
|
|
await prisma.user.update({
|
|
|
|
where: { id: user.id },
|
|
|
|
data: { lastActivityAt: new Date() },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-30 07:10:02 -07:00
|
|
|
const getUserGroups = async (account: Account): Promise<string[]> => {
|
2022-04-26 09:51:11 +02:00
|
|
|
switch (account.provider) {
|
|
|
|
case 'gitlab': {
|
2022-05-16 08:02:02 -07:00
|
|
|
const getGitlabGroups = async (
|
|
|
|
accessToken: string,
|
|
|
|
page = 1
|
|
|
|
): Promise<{ full_path: string }[]> => {
|
2022-05-16 15:49:24 +02:00
|
|
|
const res = await fetch(
|
2022-05-16 08:02:02 -07:00
|
|
|
`${
|
|
|
|
process.env.GITLAB_BASE_URL || 'https://gitlab.com'
|
|
|
|
}/api/v4/groups?per_page=100&page=${page}`,
|
2022-05-16 15:49:24 +02:00
|
|
|
{ headers: { Authorization: `Bearer ${accessToken}` } }
|
|
|
|
)
|
2022-05-16 08:02:02 -07:00
|
|
|
const groups: { full_path: string }[] = await res.json()
|
2022-05-16 15:49:24 +02:00
|
|
|
const nextPage = parseInt(res.headers.get('X-Next-Page') || '')
|
2022-05-16 08:02:02 -07:00
|
|
|
if (nextPage)
|
|
|
|
groups.push(...(await getGitlabGroups(accessToken, nextPage)))
|
2022-05-16 15:49:24 +02:00
|
|
|
return groups
|
|
|
|
}
|
2022-05-16 08:02:02 -07:00
|
|
|
const groups = await getGitlabGroups(account.access_token as string)
|
|
|
|
return groups.map((group) => group.full_path)
|
2022-04-26 09:51:11 +02:00
|
|
|
}
|
2022-04-30 07:10:02 -07:00
|
|
|
default:
|
|
|
|
return []
|
2022-04-26 09:51:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 07:10:02 -07:00
|
|
|
const getRequiredGroups = (provider: string): string[] => {
|
2022-04-26 09:51:11 +02:00
|
|
|
switch (provider) {
|
2022-04-30 07:10:02 -07:00
|
|
|
case 'gitlab':
|
|
|
|
return process.env.GITLAB_REQUIRED_GROUPS?.split(',') || []
|
|
|
|
default:
|
|
|
|
return []
|
2022-04-26 09:51:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 07:10:02 -07:00
|
|
|
const checkHasGroups = (userGroups: string[], requiredGroups: string[]) =>
|
|
|
|
userGroups?.some((userGroup) => requiredGroups?.includes(userGroup))
|
2022-04-26 09:51:11 +02:00
|
|
|
|
2022-12-16 08:39:14 +01:00
|
|
|
export default handler
|