Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
9
src/middleware/README.md
Normal file
9
src/middleware/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Middlewares
|
||||
|
||||
This folder contains middlewares for the SSR pages/endpoints
|
||||
|
||||
They are run for every paths independent of the middleware and in the specified order of the `index.ts`
|
||||
|
||||
## locals
|
||||
|
||||
You can pass variables to other middlewares and endpoints by adding a variable in `locals` and in `App.Locals` in `env.d.ts`
|
@ -1,52 +0,0 @@
|
||||
import { objectLoop } from '@dzeio/object-util'
|
||||
import URLManager from '@dzeio/url-manager'
|
||||
import { defineMiddleware } from "astro/middleware"
|
||||
import { buildRFC7807 } from '../libs/RFCs/RFC7807'
|
||||
import { Permission, validateAuth } from '../libs/validateAuth'
|
||||
|
||||
const endpointsPermissions: Record<string, Permission> = {
|
||||
'/api/v1/users/[userId]/configs/[configId]/files/[fileName]': {
|
||||
api: true,
|
||||
cookie: true,
|
||||
name: 'configs.get'
|
||||
},
|
||||
'/api/v1/slice/[configId]': {
|
||||
api: true,
|
||||
cookie: true,
|
||||
name: 'slice.slice'
|
||||
}
|
||||
}
|
||||
|
||||
function objectFind(obj: object, fn: (value: any, key: any) => boolean): {key: string, value: any} | null {
|
||||
let res: {key: string, value: any} | null = null
|
||||
objectLoop(obj, (value, key) => {
|
||||
const tmp = fn(value, key)
|
||||
if (tmp) {
|
||||
res = {
|
||||
key, value
|
||||
}
|
||||
}
|
||||
return !tmp
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
// `context` and `next` are automatically typed
|
||||
export default defineMiddleware(async (context, next) => {
|
||||
if (!context.request.url.includes('api')) {
|
||||
return next()
|
||||
}
|
||||
const permission = objectFind(endpointsPermissions, (_, key) => new URLManager(key).toString(context.params as any) === context.url.pathname)
|
||||
if (!permission) {
|
||||
return buildRFC7807({
|
||||
type: 'idk'
|
||||
})
|
||||
}
|
||||
const auth = await validateAuth(context.request, permission.value)
|
||||
if (typeof auth === 'object') {
|
||||
return auth
|
||||
}
|
||||
context.locals.authKey = auth
|
||||
|
||||
return next()
|
||||
})
|
@ -1,18 +0,0 @@
|
||||
import { defineMiddleware } from "astro/middleware"
|
||||
import RateLimiter from '../libs/RateLimiter'
|
||||
|
||||
// `context` and `next` are automatically typed
|
||||
export default defineMiddleware(async ({ request, locals }, next) => {
|
||||
if (!request.url.includes('api')) {
|
||||
return next()
|
||||
}
|
||||
|
||||
const limit = RateLimiter.getInstance().consume(locals.authKey as string)
|
||||
|
||||
if ('status' in limit) {
|
||||
return limit
|
||||
}
|
||||
locals.responseBuilder.addHeaders(limit)
|
||||
|
||||
return next()
|
||||
})
|
@ -1,7 +1,5 @@
|
||||
import { sequence } from "astro/middleware"
|
||||
|
||||
import apiAuth from './apiAuth'
|
||||
import apiRateLimit from './apiRateLimit'
|
||||
import responseBuilder from './responseBuilder'
|
||||
|
||||
export const onRequest = sequence(responseBuilder, apiAuth, apiRateLimit)
|
||||
export const onRequest = sequence(responseBuilder)
|
||||
|
Reference in New Issue
Block a user