Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"fastify": "^3.14.0",
"fastify-cors": "^5.2.0",
"fastify-multipart": "^4.0.1",
"fastify-plugin": "^3.0.0",
"fastify-swagger": "^4.4.1",
"fs-extra": "^8.1.0",
"fs-xattr": "^0.3.1",
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fastifyPlugin from 'fastify-plugin'

declare module 'fastify' {
interface FastifyRequest {
jwt: string
}
}

export default fastifyPlugin(async (fastify) => {
fastify.decorateRequest('jwt', null)
fastify.addHook('preHandler', async (request) => {
request.jwt = (request.headers.authorization as string).substring('Bearer '.length)
})
})
16 changes: 16 additions & 0 deletions src/plugins/postgrest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PostgrestClient } from '@supabase/postgrest-js'
import fastifyPlugin from 'fastify-plugin'
import { getPostgrestClient } from '../utils'

declare module 'fastify' {
interface FastifyRequest {
postgrest: PostgrestClient
}
}

export default fastifyPlugin(async (fastify) => {
fastify.decorateRequest('postgrest', null)
fastify.addHook('preHandler', async (request) => {
request.postgrest = getPostgrestClient(request.jwt)
})
})
9 changes: 3 additions & 6 deletions src/routes/bucket/createBucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { AuthenticatedRequest, Bucket } from '../../types/types'
import { getOwner, getPostgrestClient, isValidKey, transformPostgrestError } from '../../utils'
import { getOwner, isValidKey, transformPostgrestError } from '../../utils'
import { createDefaultSchema, createResponse } from '../../utils/generic-routes'

const createBucketBodySchema = {
Expand Down Expand Up @@ -39,12 +39,9 @@ export default async function routes(fastify: FastifyInstance) {
schema,
},
async (request, response) => {
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)
const postgrest = getPostgrestClient(jwt)
let owner
try {
owner = await getOwner(jwt)
owner = await getOwner(request.jwt)
} catch (err) {
console.log(err)
return response.status(400).send(createResponse(err.message, '400', err.message))
Expand All @@ -64,7 +61,7 @@ export default async function routes(fastify: FastifyInstance) {
.send(createResponse('The key contains invalid characters', '400', 'Invalid key'))
}

const { data: results, error, status } = await postgrest
const { data: results, error, status } = await request.postgrest
.from<Bucket>('buckets')
.insert(
[
Expand Down
7 changes: 2 additions & 5 deletions src/routes/bucket/deleteBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ export default async function routes(fastify: FastifyInstance) {
schema,
},
async (request, response) => {
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)
const { bucketId } = request.params
const userPostgrest = getPostgrestClient(jwt)
const superUserPostgrest = getPostgrestClient(serviceKey)

const {
data: bucketResults,
error: bucketError,
status: bucketStatus,
} = await userPostgrest.from<Bucket>('buckets').select('id').eq('id', bucketId).single()
} = await request.postgrest.from<Bucket>('buckets').select('id').eq('id', bucketId).single()

if (bucketError) {
request.log.error({ error: bucketError }, 'error bucket')
Expand Down Expand Up @@ -84,7 +81,7 @@ export default async function routes(fastify: FastifyInstance) {
)
}

const { data: results, error, status } = await userPostgrest
const { data: results, error, status } = await request.postgrest
.from<Bucket>('buckets')
.delete()
.eq('id', bucketId)
Expand Down
15 changes: 8 additions & 7 deletions src/routes/bucket/emptyBucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { AuthenticatedRequest, Bucket, Obj } from '../../types/types'
import { getPostgrestClient, transformPostgrestError } from '../../utils'
import { transformPostgrestError } from '../../utils'
import { getConfig } from '../../utils/config'
import { createDefaultSchema, createResponse } from '../../utils/generic-routes'
import { S3Backend } from '../../backend/s3'
Expand Down Expand Up @@ -47,12 +47,9 @@ export default async function routes(fastify: FastifyInstance) {
schema,
},
async (request, response) => {
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)
const { bucketId } = request.params
const postgrest = getPostgrestClient(jwt)

const bucketResponse = await postgrest
const bucketResponse = await request.postgrest
.from<Bucket>('buckets')
.select('name')
.eq('id', bucketId)
Expand All @@ -67,7 +64,11 @@ export default async function routes(fastify: FastifyInstance) {

let deleteError, deleteData, objectError, objects, objectStatus
do {
;({ data: objects, error: objectError, status: objectStatus } = await postgrest
;({
data: objects,
error: objectError,
status: objectStatus,
} = await request.postgrest
.from<Obj>('objects')
.select('name, id')
.eq('bucket_id', bucketId)
Expand All @@ -80,7 +81,7 @@ export default async function routes(fastify: FastifyInstance) {
request.log.info({ results: objects }, 'results')

if (objects && objects.length > 0) {
;({ error: deleteError, data: deleteData } = await postgrest
;({ error: deleteError, data: deleteData } = await request.postgrest
.from<Obj>('objects')
.delete()
.in(
Expand Down
8 changes: 2 additions & 6 deletions src/routes/bucket/getAllBuckets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { bucketSchema } from '../../schemas/bucket'
import { AuthenticatedRequest, Bucket } from '../../types/types'
import { getPostgrestClient, transformPostgrestError } from '../../utils'
import { transformPostgrestError } from '../../utils'
import { createDefaultSchema } from '../../utils/generic-routes'

const successResponseSchema = {
Expand Down Expand Up @@ -33,11 +33,7 @@ export default async function routes(fastify: FastifyInstance) {
},
async (request, response) => {
// get list of all buckets
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)

const postgrest = getPostgrestClient(jwt)
const { data: results, error, status } = await postgrest
const { data: results, error, status } = await request.postgrest
.from<Bucket>('buckets')
.select('id, name, public, owner, created_at, updated_at')

Expand Down
7 changes: 2 additions & 5 deletions src/routes/bucket/getBucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { bucketSchema } from '../../schemas/bucket'
import { AuthenticatedRequest, Bucket } from '../../types/types'
import { getPostgrestClient, transformPostgrestError } from '../../utils'
import { transformPostgrestError } from '../../utils'
import { createDefaultSchema } from '../../utils/generic-routes'

const getBucketParamsSchema = {
Expand Down Expand Up @@ -32,11 +32,8 @@ export default async function routes(fastify: FastifyInstance) {
schema,
},
async (request, response) => {
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)
const { bucketId } = request.params
const postgrest = getPostgrestClient(jwt)
const { data: results, error, status } = await postgrest
const { data: results, error, status } = await request.postgrest
.from<Bucket>('buckets')
.select('id, name, owner, public, created_at, updated_at')
.eq('id', bucketId)
Expand Down
5 changes: 5 additions & 0 deletions src/routes/bucket/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FastifyInstance } from 'fastify'
import jwt from '../../plugins/jwt'
import postgrest from '../../plugins/postgrest'
import createBucket from './createBucket'
import deleteBucket from './deleteBucket'
import emptyBucket from './emptyBucket'
Expand All @@ -8,6 +10,9 @@ import updateBucket from './updateBucket'

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default async function routes(fastify: FastifyInstance) {
fastify.register(jwt)
fastify.register(postgrest)

fastify.register(createBucket)
fastify.register(deleteBucket)
fastify.register(emptyBucket)
Expand Down
7 changes: 2 additions & 5 deletions src/routes/bucket/updateBucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { AuthenticatedRequest, Bucket } from '../../types/types'
import { getPostgrestClient, transformPostgrestError } from '../../utils'
import { transformPostgrestError } from '../../utils'
import { createDefaultSchema, createResponse } from '../../utils/generic-routes'

const updateBucketBodySchema = {
Expand Down Expand Up @@ -44,14 +44,11 @@ export default async function routes(fastify: FastifyInstance) {
schema,
},
async (request, response) => {
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)
const postgrest = getPostgrestClient(jwt)
const { bucketId } = request.params

const { public: isPublic } = request.body

const { error, status } = await postgrest
const { error, status } = await request.postgrest
.from<Bucket>('buckets')
.update({
public: isPublic,
Expand Down
3 changes: 1 addition & 2 deletions src/routes/object/copyObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export default async function routes(fastify: FastifyInstance) {
return response.status(400).send(responseValue)
}

const postgrest = getPostgrestClient(jwt)
const superUserPostgrest = getPostgrestClient(serviceKey)

let owner
Expand Down Expand Up @@ -114,7 +113,7 @@ export default async function routes(fastify: FastifyInstance) {
owner,
})
request.log.info({ origObject }, 'newObject')
const { data: results, error, status } = await postgrest
const { data: results, error, status } = await request.postgrest
.from<Obj>('objects')
.insert([newObject], {
returning: 'minimal',
Expand Down
11 changes: 3 additions & 8 deletions src/routes/object/createObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ export default async function routes(fastify: FastifyInstance) {
schema,
},
async (request, response) => {
// check if the user is able to insert that row
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)

const contentType = request.headers['content-type']
request.log.info(`content-type is ${contentType}`)

Expand All @@ -96,12 +92,11 @@ export default async function routes(fastify: FastifyInstance) {
.send(createResponse('The key contains invalid characters', '400', 'Invalid key'))
}

const postgrest = getPostgrestClient(jwt)
const superUserPostgrest = getPostgrestClient(serviceKey)

let owner
try {
owner = await getOwner(jwt)
owner = await getOwner(request.jwt)
} catch (err) {
request.log.error(err)
return response.status(400).send({
Expand All @@ -117,7 +112,7 @@ export default async function routes(fastify: FastifyInstance) {
let postgrestResponse: PostgrestSingleResponse<Obj>

if (isUpsert) {
postgrestResponse = await postgrest
postgrestResponse = await request.postgrest
.from<Obj>('objects')
.upsert(
[
Expand All @@ -134,7 +129,7 @@ export default async function routes(fastify: FastifyInstance) {
)
.single()
} else {
postgrestResponse = await postgrest
postgrestResponse = await request.postgrest
.from<Obj>('objects')
.insert(
[
Expand Down
10 changes: 2 additions & 8 deletions src/routes/object/deleteObject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { AuthenticatedRequest, Obj } from '../../types/types'
import { getPostgrestClient, isValidKey, transformPostgrestError } from '../../utils'
import { isValidKey, transformPostgrestError } from '../../utils'
import { getConfig } from '../../utils/config'
import { createDefaultSchema, createResponse } from '../../utils/generic-routes'
import { S3Backend } from '../../backend/s3'
Expand Down Expand Up @@ -51,22 +51,16 @@ export default async function routes(fastify: FastifyInstance) {
schema,
},
async (request, response) => {
// check if the user is able to insert that row
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)

const { bucketName } = request.params
const objectName = request.params['*']

const postgrest = getPostgrestClient(jwt)

if (!isValidKey(objectName) || !isValidKey(bucketName)) {
return response
.status(400)
.send(createResponse('The key contains invalid characters', '400', 'Invalid key'))
}

const objectResponse = await postgrest
const objectResponse = await request.postgrest
.from<Obj>('objects')
.delete()
.match({
Expand Down
10 changes: 2 additions & 8 deletions src/routes/object/deleteObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { objectSchema } from '../../schemas/object'
import { AuthenticatedRequest, Obj } from '../../types/types'
import { getPostgrestClient, transformPostgrestError } from '../../utils'
import { transformPostgrestError } from '../../utils'
import { getConfig } from '../../utils/config'
import { createDefaultSchema } from '../../utils/generic-routes'
import { S3Backend } from '../../backend/s3'
Expand Down Expand Up @@ -64,16 +64,10 @@ export default async function routes(fastify: FastifyInstance) {
schema,
},
async (request, response) => {
// check if the user is able to insert that row
const authHeader = request.headers.authorization
const jwt = authHeader.substring('Bearer '.length)

const { bucketName } = request.params
const prefixes = request.body['prefixes']

const postgrest = getPostgrestClient(jwt)

const objectResponse = await postgrest
const objectResponse = await request.postgrest
.from<Obj>('objects')
.delete()
.eq('bucket_id', bucketName)
Expand Down
8 changes: 2 additions & 6 deletions src/routes/object/getObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { IncomingMessage, Server, ServerResponse } from 'http'
import { AuthenticatedRangeRequest, Obj } from '../../types/types'
import { getPostgrestClient, isValidKey, transformPostgrestError } from '../../utils'
import { isValidKey, transformPostgrestError } from '../../utils'
import { getConfig } from '../../utils/config'
import { normalizeContentType } from '../../utils'
import { createResponse } from '../../utils/generic-routes'
Expand Down Expand Up @@ -41,11 +41,7 @@ async function requestHandler(
unknown
>
) {
const authHeader = request.headers.authorization
const range = request.headers.range
const jwt = authHeader.substring('Bearer '.length)

const postgrest = getPostgrestClient(jwt)

const { bucketName } = request.params
const objectName = request.params['*']
Expand All @@ -56,7 +52,7 @@ async function requestHandler(
.send(createResponse('The key contains invalid characters', '400', 'Invalid key'))
}

const objectResponse = await postgrest
const objectResponse = await request.postgrest
.from<Obj>('objects')
.select('id')
.match({
Expand Down
Loading