feat: first version
Some checks failed
Build, check & Test / run (push) Failing after 39s

Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
2023-07-20 17:41:16 +02:00
parent 2bd59f902f
commit 09ed4c487d
80 changed files with 1171 additions and 2755 deletions

9
src/middleware/README.md Normal file
View 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`

View File

@ -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()
})

View File

@ -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()
})

View File

@ -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)