fix: file upload and OAuth2 (#10)

This commit is contained in:
Divlo
2022-04-08 21:36:29 +02:00
committed by GitHub
parent 69c567cb66
commit a4c77fec50
30 changed files with 529 additions and 366 deletions

View File

@ -6,10 +6,6 @@ import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { fastifyErrors } from '../../../../models/utils.js'
import prisma from '../../../../tools/database/prisma.js'
import { uploadFile } from '../../../../tools/utils/uploadFile.js'
import {
MAXIMUM_IMAGE_SIZE,
SUPPORTED_IMAGE_MIMETYPE
} from '../../../../tools/configurations/index.js'
const putServiceSchema: FastifySchema = {
description: 'Edit the current connected user logo',
@ -51,9 +47,7 @@ export const putCurrentUserLogo: FastifyPluginAsync = async (fastify) => {
const file = await uploadFile({
fastify,
request,
folderInUploadsFolder: 'users',
maximumFileSize: MAXIMUM_IMAGE_SIZE,
supportedFileMimetype: SUPPORTED_IMAGE_MIMETYPE
folderInUploadsFolder: 'users'
})
await prisma.user.update({
where: { id: request.user.current.id },

View File

@ -20,6 +20,12 @@ import { getCallbackGoogleOAuth2Service } from './oauth2/google/callback/get.js'
import { getSigninGitHubOAuth2Service } from './oauth2/github/signin/get.js'
import { getCallbackGitHubOAuth2Service } from './oauth2/github/callback/get.js'
import { deleteProviderService } from './oauth2/[provider]/delete.js'
import { getCallbackAddStrategyDiscordOAuth2Service } from './oauth2/discord/callback-add-strategy/get.js'
import { getAddStrategyDiscordOAuth2Service } from './oauth2/discord/add-strategy/get.js'
import { getAddStrategyGitHubOAuth2Service } from './oauth2/github/add-strategy/get.js'
import { getCallbackAddStrategyGitHubOAuth2Service } from './oauth2/github/callback-add-strategy/get.js'
import { getCallbackAddStrategyGoogleOAuth2Service } from './oauth2/google/callback-add-strategy/get.js'
import { getAddStrategyGoogleOAuth2Service } from './oauth2/google/add-strategy/get.js'
export const usersService: FastifyPluginAsync = async (fastify) => {
await fastify.register(postSignupUser)
@ -38,12 +44,18 @@ export const usersService: FastifyPluginAsync = async (fastify) => {
await fastify.register(getSigninDiscordOAuth2Service)
await fastify.register(getCallbackDiscordOAuth2Service)
await fastify.register(getCallbackAddStrategyDiscordOAuth2Service)
await fastify.register(getAddStrategyDiscordOAuth2Service)
await fastify.register(getSigninGoogleOAuth2Service)
await fastify.register(getCallbackGoogleOAuth2Service)
await fastify.register(getCallbackAddStrategyGoogleOAuth2Service)
await fastify.register(getAddStrategyGoogleOAuth2Service)
await fastify.register(getSigninGitHubOAuth2Service)
await fastify.register(getCallbackGitHubOAuth2Service)
await fastify.register(getCallbackAddStrategyGitHubOAuth2Service)
await fastify.register(getAddStrategyGitHubOAuth2Service)
await fastify.register(deleteProviderService)
}

View File

@ -0,0 +1,53 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations/index.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { DISCORD_BASE_URL, DISCORD_CLIENT_ID } from '../__utils__/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
redirectURI: Type.String({ format: 'uri-reference' })
})
type QuerySchemaType = Static<typeof querySchema>
const getServiceSchema: FastifySchema = {
description: 'Discord OAuth2 - add-strategy',
tags: ['users'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
querystring: querySchema,
response: {
200: Type.String(),
400: fastifyErrors[400],
500: fastifyErrors[500]
}
} as const
export const getAddStrategyDiscordOAuth2Service: FastifyPluginAsync = async (
fastify
) => {
await fastify.register(authenticateUser)
await fastify.route<{
Querystring: QuerySchemaType
}>({
method: 'GET',
url: '/users/oauth2/discord/add-strategy',
schema: getServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { redirectURI } = request.query
const redirectCallback = `${API_URL}/users/oauth2/discord/callback-add-strategy?redirectURI=${redirectURI}`
const url = `${DISCORD_BASE_URL}/oauth2/authorize?client_id=${DISCORD_CLIENT_ID}&scope=identify&response_type=code&state=${request.user.accessToken}&redirect_uri=${redirectCallback}`
reply.statusCode = 200
return url
}
})
}

View File

@ -0,0 +1,56 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations/index.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { discordStrategy, getDiscordUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '../../../../../tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
code: Type.String(),
state: Type.String(),
redirectURI: Type.String({ format: 'uri-reference' })
})
type QuerySchemaType = Static<typeof querySchema>
const getServiceSchema: FastifySchema = {
description: 'Discord OAuth2 - callback-add-strategy',
tags: ['users'] as string[],
querystring: querySchema,
response: {
200: Type.String(),
400: fastifyErrors[400],
500: fastifyErrors[500]
}
} as const
export const getCallbackAddStrategyDiscordOAuth2Service: FastifyPluginAsync =
async (fastify) => {
await fastify.route<{
Querystring: QuerySchemaType
}>({
method: 'GET',
url: '/users/oauth2/discord/callback-add-strategy',
schema: getServiceSchema,
handler: async (request, reply) => {
const { redirectURI, code, state: accessToken } = request.query
const userRequest = await getUserWithBearerToken(
`Bearer ${accessToken}`
)
const discordUser = await getDiscordUserData(
code,
`${API_URL}/users/oauth2/discord/callback-add-strategy?redirectURI=${redirectURI}`
)
const message = await discordStrategy.callbackAddStrategy(
{
name: discordUser.username,
id: discordUser.id
},
userRequest
)
return await reply.redirect(buildQueryURL(redirectURI, { message }))
}
})
}

View File

@ -0,0 +1,53 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations/index.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from '../__utils__/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
redirectURI: Type.String({ format: 'uri-reference' })
})
type QuerySchemaType = Static<typeof querySchema>
const getServiceSchema: FastifySchema = {
description: 'GitHub OAuth2 - add-strategy',
tags: ['users'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
querystring: querySchema,
response: {
200: Type.String(),
400: fastifyErrors[400],
500: fastifyErrors[500]
}
} as const
export const getAddStrategyGitHubOAuth2Service: FastifyPluginAsync = async (
fastify
) => {
await fastify.register(authenticateUser)
await fastify.route<{
Querystring: QuerySchemaType
}>({
method: 'GET',
url: '/users/oauth2/github/add-strategy',
schema: getServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { redirectURI } = request.query
const redirectCallback = `${API_URL}/users/oauth2/github/callback-add-strategy?redirectURI=${redirectURI}`
const url = `${GITHUB_BASE_URL}/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}&state=${request.user.accessToken}&redirect_uri=${redirectCallback}`
reply.statusCode = 200
return url
}
})
}

View File

@ -0,0 +1,56 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations/index.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { githubStrategy, getGitHubUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '../../../../../tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
code: Type.String(),
state: Type.String(),
redirectURI: Type.String({ format: 'uri-reference' })
})
type QuerySchemaType = Static<typeof querySchema>
const getServiceSchema: FastifySchema = {
description: 'GitHub OAuth2 - callback-add-strategy',
tags: ['users'] as string[],
querystring: querySchema,
response: {
200: Type.String(),
400: fastifyErrors[400],
500: fastifyErrors[500]
}
} as const
export const getCallbackAddStrategyGitHubOAuth2Service: FastifyPluginAsync =
async (fastify) => {
await fastify.route<{
Querystring: QuerySchemaType
}>({
method: 'GET',
url: '/users/oauth2/github/callback-add-strategy',
schema: getServiceSchema,
handler: async (request, reply) => {
const { redirectURI, code, state: accessToken } = request.query
const userRequest = await getUserWithBearerToken(
`Bearer ${accessToken}`
)
const githubUser = await getGitHubUserData(
code,
`${API_URL}/users/oauth2/github/callback-add-strategy?redirectURI=${redirectURI}`
)
const message = await githubStrategy.callbackAddStrategy(
{
name: githubUser.name,
id: githubUser.id
},
userRequest
)
return await reply.redirect(buildQueryURL(redirectURI, { message }))
}
})
}

View File

@ -0,0 +1,53 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations/index.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { GOOGLE_BASE_URL, GOOGLE_CLIENT_ID } from '../__utils__/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
redirectURI: Type.String({ format: 'uri-reference' })
})
type QuerySchemaType = Static<typeof querySchema>
const getServiceSchema: FastifySchema = {
description: 'Google OAuth2 - add-strategy',
tags: ['users'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
querystring: querySchema,
response: {
200: Type.String(),
400: fastifyErrors[400],
500: fastifyErrors[500]
}
} as const
export const getAddStrategyGoogleOAuth2Service: FastifyPluginAsync = async (
fastify
) => {
await fastify.register(authenticateUser)
await fastify.route<{
Querystring: QuerySchemaType
}>({
method: 'GET',
url: '/users/oauth2/google/add-strategy',
schema: getServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { redirectURI } = request.query
const redirectCallback = `${API_URL}/users/oauth2/google/callback-add-strategy?redirectURI=${redirectURI}`
const url = `${GOOGLE_BASE_URL}?client_id=${GOOGLE_CLIENT_ID}&state=${request.user.accessToken}&redirect_uri=${redirectCallback}&response_type=code&scope=profile&access_type=online`
reply.statusCode = 200
return url
}
})
}

View File

@ -0,0 +1,56 @@
import { Static, Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations/index.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { googleStrategy, getGoogleUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '../../../../../tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
code: Type.String(),
state: Type.String(),
redirectURI: Type.String({ format: 'uri-reference' })
})
type QuerySchemaType = Static<typeof querySchema>
const getServiceSchema: FastifySchema = {
description: 'Google OAuth2 - callback-add-strategy',
tags: ['users'] as string[],
querystring: querySchema,
response: {
200: Type.String(),
400: fastifyErrors[400],
500: fastifyErrors[500]
}
} as const
export const getCallbackAddStrategyGoogleOAuth2Service: FastifyPluginAsync =
async (fastify) => {
await fastify.route<{
Querystring: QuerySchemaType
}>({
method: 'GET',
url: '/users/oauth2/google/callback-add-strategy',
schema: getServiceSchema,
handler: async (request, reply) => {
const { redirectURI, code, state: accessToken } = request.query
const userRequest = await getUserWithBearerToken(
`Bearer ${accessToken}`
)
const googleUser = await getGoogleUserData(
code,
`${API_URL}/users/oauth2/google/callback-add-strategy?redirectURI=${redirectURI}`
)
const message = await googleStrategy.callbackAddStrategy(
{
name: googleUser.name,
id: googleUser.id
},
userRequest
)
return await reply.redirect(buildQueryURL(redirectURI, { message }))
}
})
}