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'
|
2021-12-06 15:48:50 +01:00
|
|
|
import prisma from 'libs/prisma'
|
|
|
|
import { Provider } from 'next-auth/providers'
|
2021-12-27 15:59:32 +01:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2022-02-21 11:46:56 +01:00
|
|
|
import { withSentry } from '@sentry/nextjs'
|
2022-02-24 11:13:19 +01:00
|
|
|
import { CustomAdapter } from './adapter'
|
2022-02-24 14:52:35 +01:00
|
|
|
import { User } from 'db'
|
2022-06-22 07:36:11 +02:00
|
|
|
import { env, isNotEmpty } from 'utils'
|
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,
|
|
|
|
auth: {
|
|
|
|
user: process.env.SMTP_USERNAME,
|
|
|
|
pass: process.env.SMTP_PASSWORD,
|
|
|
|
},
|
2022-06-13 11:50:05 +02:00
|
|
|
ignoreTLS: process.env.SMTP_IGNORE_TLS === 'true',
|
2022-03-13 08:56:10 +01:00
|
|
|
},
|
2022-06-22 07:36:11 +02:00
|
|
|
from: env('SMTP_FROM'),
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-28 17:57:14 +01:00
|
|
|
const handler = (req: NextApiRequest, res: NextApiResponse) => {
|
2022-03-10 15:16:44 +01:00
|
|
|
if (req.method === 'HEAD') {
|
|
|
|
res.status(200)
|
|
|
|
return
|
|
|
|
}
|
2022-01-28 17:57:14 +01:00
|
|
|
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
|
|
|
},
|
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-04-26 09:51:11 +02:00
|
|
|
signIn: async ({ account }) => {
|
|
|
|
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-02-21 11:46:56 +01:00
|
|
|
export default withSentry(handler)
|