-
-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathgetSignedObject.ts
More file actions
103 lines (91 loc) · 3.13 KB
/
getSignedObject.ts
File metadata and controls
103 lines (91 loc) · 3.13 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
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { getConfig } from '../../../config'
import { SignedToken, verifyJWT } from '../../../internal/auth'
import { getJwtSecret } from '../../../internal/database'
import { ROUTE_OPERATIONS } from '../operations'
import { ERRORS } from '../../../internal/errors'
const { storageS3Bucket } = getConfig()
const getSignedObjectParamsSchema = {
type: 'object',
properties: {
bucketName: { type: 'string', examples: ['avatars'] },
'*': { type: 'string', examples: ['folder/cat.png'] },
},
required: ['bucketName', '*'],
} as const
const getSignedObjectQSSchema = {
type: 'object',
properties: {
download: { type: 'string', examples: ['filename.jpg', null] },
token: {
type: 'string',
examples: [
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1cmwiOiJidWNrZXQyL3B1YmxpYy9zYWRjYXQtdXBsb2FkMjMucG5nIiwiaWF0IjoxNjE3NzI2MjczLCJleHAiOjE2MTc3MjcyNzN9.uBQcXzuvXxfw-9WgzWMBfE_nR3VOgpvfZe032sfLSSk',
],
},
},
required: ['token'],
} as const
interface GetSignedObjectRequestInterface {
Params: FromSchema<typeof getSignedObjectParamsSchema>
Querystring: FromSchema<typeof getSignedObjectQSSchema>
Headers: {
range?: string
}
}
export default async function routes(fastify: FastifyInstance) {
const summary = 'Retrieve an object via a presigned URL'
fastify.get<GetSignedObjectRequestInterface>(
'/sign/:bucketName/*',
{
// @todo add success response schema here
schema: {
params: getSignedObjectParamsSchema,
querystring: getSignedObjectQSSchema,
summary,
response: { '4xx': { $ref: 'errorSchema#', description: 'Error response' } },
tags: ['object'],
},
config: {
operation: { type: ROUTE_OPERATIONS.GET_SIGNED_OBJECT },
},
},
async (request, response) => {
const { token } = request.query
const { download } = request.query
let payload: SignedToken
const { secret: jwtSecret, jwks } = await getJwtSecret(request.tenantId)
try {
payload = (await verifyJWT(token, jwtSecret, jwks)) as SignedToken
} catch (e) {
const err = e as Error
throw ERRORS.InvalidJWT(err)
}
const { url, exp } = payload
const path = `${request.params.bucketName}/${request.params['*']}`
if (url !== path) {
throw ERRORS.InvalidSignature()
}
const s3Key = `${request.tenantId}/${url}`
const [bucketName, ...objParts] = url.split('/')
const obj = await request.storage
.asSuperUser()
.from(bucketName)
.findObject({
objectName: objParts.join('/'),
columns: 'id,version,metadata',
signal: request.signals.disconnect.signal,
})
return request.storage.renderer('asset').render(request, response, {
bucket: storageS3Bucket,
key: s3Key,
version: obj.version,
download,
expires: new Date(exp * 1000).toUTCString(),
xRobotsTag: obj.metadata?.['xRobotsTag'] as string | undefined,
signal: request.signals.disconnect.signal,
})
}
)
}