-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- are now available in SSR - are also avaiable for Nitro preset deployments
- Loading branch information
Showing
30 changed files
with
1,837 additions
and
1,270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { NuxtSecurityRouteRules } from "~/src/types" | ||
import { createRouter, toRouteMatcher } from "radix3" | ||
import type { H3Event } from "h3" | ||
import { defuReplaceArray } from "../utils" | ||
|
||
// This is the global singleton that holds all of the application security rules | ||
const nitroAppSecurityOptions: Record<string, NuxtSecurityRouteRules> = {} | ||
|
||
/** | ||
* Returns all the security rules of the Nitro application | ||
*/ | ||
export function getAppSecurityOptions() { | ||
return nitroAppSecurityOptions | ||
} | ||
|
||
/** | ||
* Returns the security rules applicable to a specific request | ||
*/ | ||
export function resolveSecurityRules(event: H3Event): NuxtSecurityRouteRules { | ||
if (!event.context.security) { | ||
event.context.security = {} | ||
} | ||
if (!event.context.security.rules) { | ||
const router = createRouter<NuxtSecurityRouteRules>({ routes: structuredClone(nitroAppSecurityOptions) }) | ||
const matcher = toRouteMatcher(router) | ||
const matches = matcher.matchAll(event.path.split('?')[0]) | ||
const rules: NuxtSecurityRouteRules = defuReplaceArray({}, ...matches.reverse()) | ||
event.context.security.rules = rules | ||
} | ||
return event.context.security.rules | ||
} | ||
|
||
/** | ||
* Returns the security route that was matched for a specific request | ||
*/ | ||
export function resolveSecurityRoute(event: H3Event) { | ||
if (!event.context.security) { | ||
event.context.security = {} | ||
} | ||
if (!event.context.security.route) { | ||
const routeNames = Object.fromEntries(Object.entries(nitroAppSecurityOptions).map(([name]) => [name, { name }])) | ||
const router = createRouter<{ name: string }>({ routes: routeNames}) | ||
const match = router.lookup(event.path.split('?')[0]) | ||
const route = match?.name ?? '' | ||
event.context.security.route = route | ||
} | ||
return event.context.security.route | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.