@ -1,16 +1,43 @@
|
||||
import { objectLoop } from '@dzeio/object-util'
|
||||
import URLManager from '@dzeio/url-manager'
|
||||
import { defineMiddleware } from "astro/middleware"
|
||||
import { validateAuth } from '../libs/validateAuth'
|
||||
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'
|
||||
}
|
||||
}
|
||||
|
||||
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 auth = await validateAuth(context.request, {
|
||||
name: 'slicing.slice',
|
||||
api: true,
|
||||
cookie: true
|
||||
})
|
||||
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
|
||||
}
|
||||
|
@ -1,9 +1,21 @@
|
||||
import { defineMiddleware } from "astro/middleware"
|
||||
import { buildRFC7807 } from '../libs/RFCs/RFC7807'
|
||||
import ResponseBuilder from '../libs/ResponseBuilder'
|
||||
|
||||
// `context` and `next` are automatically typed
|
||||
export default defineMiddleware(async (context, next) => {
|
||||
context.locals.responseBuilder = new ResponseBuilder()
|
||||
export default defineMiddleware(async ({ request, locals }, next) => {
|
||||
locals.responseBuilder = new ResponseBuilder()
|
||||
console.log(`[${new Date().toISOString()}] ${request.headers.get('user-agent')?.slice(0, 32).padEnd(32)} ${request.method.padEnd(7)} ${request.url}`)
|
||||
|
||||
return next()
|
||||
try {
|
||||
const res = await next()
|
||||
console.log(`[${new Date().toISOString()}] ${request.headers.get('user-agent')?.slice(0, 32).padEnd(32)} ${request.method.padEnd(7)} ${res.status} ${request.url}`)
|
||||
return res
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return buildRFC7807({
|
||||
type: '/docs/errors/global-error',
|
||||
status: 500
|
||||
})
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user