@vite-hub/auth 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +79 -34
- package/dist/agent.d.ts +1 -0
- package/dist/agent.js +1 -1
- package/dist/host-declarations.d.ts +13 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +6 -1
- package/dist/nuxt.d.ts +38 -0
- package/dist/nuxt.js +61 -0
- package/dist/runtime/empty-definition.d.ts +1 -1
- package/dist/runtime/nuxt.d.ts +4 -0
- package/dist/runtime/nuxt.js +8 -0
- package/dist/{server-Diw3mAbX.js → server-BZn8iQrB.js} +51 -3
- package/dist/server.d.ts +5 -2
- package/dist/server.js +2 -2
- package/dist/{types-B0fg9XFd.d.ts → types-jaRlW3D1.d.ts} +12 -1
- package/dist/vite-Cmey8ODS.js +1018 -0
- package/dist/vite.d.ts +18 -3
- package/dist/vite.js +2 -995
- package/dist/vue.d.ts +38 -0
- package/dist/vue.js +34 -0
- package/package.json +20 -5
|
@@ -251,6 +251,13 @@ function getAuthForRequest(request, runtimeOptions, event) {
|
|
|
251
251
|
if (!hasRequestRuntimeOptions(definition) && !hasRuntimeOptions(runtimeOptions)) return getAuthForDefinition(definition);
|
|
252
252
|
return createAuthenticationProvider(resolveBetterAuthOptionsForRequest(definition, request, runtimeOptions, event));
|
|
253
253
|
}
|
|
254
|
+
async function assertAuthOrigin(request, event) {
|
|
255
|
+
const origin = request.headers.get("origin");
|
|
256
|
+
if (!origin) throw new TypeError("The request origin is required.");
|
|
257
|
+
const auth = getAuthForRequest(request, void 0, event);
|
|
258
|
+
if (!(await auth.$context).isTrustedOrigin(origin)) throw new TypeError("The request origin is not trusted.");
|
|
259
|
+
return auth;
|
|
260
|
+
}
|
|
254
261
|
function handleAuth(input, runtimeOptions) {
|
|
255
262
|
const request = unwrapAuthRequest(input);
|
|
256
263
|
return handleAuthRequest(resolveDefaultDefinition(), request, runtimeOptions, input);
|
|
@@ -265,6 +272,20 @@ function authBaseURL(definition, request, event) {
|
|
|
265
272
|
function wantsHtml(request) {
|
|
266
273
|
return request.method === "GET" && request.headers.get("accept")?.includes("text/html") === true;
|
|
267
274
|
}
|
|
275
|
+
function createForbiddenResponse(request) {
|
|
276
|
+
return wantsHtml(request) ? new Response("Forbidden.", {
|
|
277
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
278
|
+
status: 403
|
|
279
|
+
}) : Response.json({ error: "Forbidden." }, { status: 403 });
|
|
280
|
+
}
|
|
281
|
+
function authAccessRoutes(definition, request, event, routeIndexes) {
|
|
282
|
+
const routes = resolveDefinitionOptions(definition, request, event).access?.routes;
|
|
283
|
+
return routeIndexes.map((routeIndex) => {
|
|
284
|
+
const route = routes?.[routeIndex];
|
|
285
|
+
if (!route) throw new TypeError(`[vitehub] Auth access route ${routeIndex} is unavailable at runtime.`);
|
|
286
|
+
return route;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
268
289
|
async function createSignInResponse(definition, request, signIn, event) {
|
|
269
290
|
const origin = authBaseURL(definition, request, event);
|
|
270
291
|
const response = await handleAuthRequest(definition, new Request(`${origin}${authRequestBasePath(definition, request, event)}/sign-in/social`, {
|
|
@@ -293,9 +314,28 @@ async function createSignInResponse(definition, request, signIn, event) {
|
|
|
293
314
|
status: 302
|
|
294
315
|
});
|
|
295
316
|
}
|
|
296
|
-
async function
|
|
317
|
+
async function requireAuthRequest(input, definition, routeIndexes, requiredAuthorizeRouteIndexes = []) {
|
|
297
318
|
const request = unwrapAuthRequest(input);
|
|
298
|
-
|
|
319
|
+
const session = await getAuthenticationSession(createAuthenticationProvider(resolveBetterAuthOptionsForRequest(definition, request, void 0, input)), { headers: request.headers });
|
|
320
|
+
if (session) {
|
|
321
|
+
if (routeIndexes === void 0) return;
|
|
322
|
+
const context = {
|
|
323
|
+
request,
|
|
324
|
+
session: session.session,
|
|
325
|
+
user: session.user
|
|
326
|
+
};
|
|
327
|
+
const requiredAuthorizeRoutes = new Set(requiredAuthorizeRouteIndexes);
|
|
328
|
+
const routes = authAccessRoutes(definition, request, input, routeIndexes);
|
|
329
|
+
for (const [index, route] of routes.entries()) {
|
|
330
|
+
const authorize = route instanceof Object ? route.authorize : void 0;
|
|
331
|
+
if (!authorize && requiredAuthorizeRoutes.has(routeIndexes[index])) return createForbiddenResponse(request);
|
|
332
|
+
if (!authorize) continue;
|
|
333
|
+
const result = await authorize(context);
|
|
334
|
+
if (result instanceof Response) return result;
|
|
335
|
+
if (result !== true) return createForbiddenResponse(request);
|
|
336
|
+
}
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
299
339
|
if (!wantsHtml(request)) return Response.json({ error: "Unauthorized." }, { status: 401 });
|
|
300
340
|
if (new URL(request.url).searchParams.has("auth_error")) return new Response("Unauthorized.", {
|
|
301
341
|
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
@@ -304,6 +344,14 @@ async function requireAuth(input, definition = resolveDefaultDefinition()) {
|
|
|
304
344
|
const signIn = resolveDefinitionOptions(definition, request, input).access?.signIn;
|
|
305
345
|
return signIn ? await createSignInResponse(definition, request, signIn, input) : Response.json({ error: "Unauthorized." }, { status: 401 });
|
|
306
346
|
}
|
|
347
|
+
async function requireAuth(input, definition = resolveDefaultDefinition()) {
|
|
348
|
+
return requireAuthRequest(input, definition);
|
|
349
|
+
}
|
|
350
|
+
async function requireAuthAccessRoutes(input, routeIndexes, definition = resolveDefaultDefinition(), requiredAuthorizeRouteIndexes = []) {
|
|
351
|
+
if (!Array.isArray(routeIndexes) || routeIndexes.length === 0 || routeIndexes.some((routeIndex) => !Number.isSafeInteger(routeIndex) || routeIndex < 0)) throw new TypeError("[vitehub] Auth access route indexes must be a non-empty array of non-negative integers.");
|
|
352
|
+
if (!Array.isArray(requiredAuthorizeRouteIndexes) || requiredAuthorizeRouteIndexes.some((routeIndex) => !Number.isSafeInteger(routeIndex) || routeIndex < 0)) throw new TypeError("[vitehub] Required Auth authorize route indexes must be an array of non-negative integers.");
|
|
353
|
+
return requireAuthRequest(input, definition, routeIndexes, requiredAuthorizeRouteIndexes);
|
|
354
|
+
}
|
|
307
355
|
const auth = new Proxy({}, {
|
|
308
356
|
get(_target, property, receiver) {
|
|
309
357
|
return Reflect.get(getAuth(), property, receiver);
|
|
@@ -319,4 +367,4 @@ const auth = new Proxy({}, {
|
|
|
319
367
|
}
|
|
320
368
|
});
|
|
321
369
|
//#endregion
|
|
322
|
-
export {
|
|
370
|
+
export { getAuthenticationSession as _, createAuthHandler as a, getAuth as c, handleAuth as d, handleAuthRequest as f, setAuthRuntimeEnvResolver as g, resetAuth as h, createAuthForRequest as i, getAuthForDefinition as l, requireAuthAccessRoutes as m, auth as n, createAuthRequestRuntimeOptions as o, requireAuth as p, createAuth as r, createBetterAuthOptions as s, assertAuthOrigin as t, getAuthForRequest as u, invalidAuthenticationErrorOptions as v, throwAuthenticationProviderError as y };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference path="./host-declarations.d.ts" />
|
|
2
|
+
import { F as ViteHubAuth, T as AuthRuntimeOptions, _ as AuthRequestInput, c as AuthBetterAuthRuntimeOptions, d as AuthDefinition, g as AuthRequest } from "./types-jaRlW3D1.js";
|
|
2
3
|
|
|
3
4
|
//#region src/server.d.ts
|
|
4
5
|
type AuthRuntimeEnvResolver = (event?: unknown) => Record<string, unknown>;
|
|
@@ -13,8 +14,10 @@ declare function resetAuth(): void;
|
|
|
13
14
|
declare function getAuthForDefinition(definition: AuthDefinition, runtimeOptions?: AuthRuntimeOptions): ViteHubAuth;
|
|
14
15
|
declare function getAuth(runtimeOptions?: AuthRuntimeOptions): ViteHubAuth;
|
|
15
16
|
declare function getAuthForRequest(request: Pick<Request, "headers" | "url">, runtimeOptions?: AuthRuntimeOptions, event?: unknown): ViteHubAuth;
|
|
17
|
+
declare function assertAuthOrigin(request: Pick<Request, "headers" | "url">, event?: unknown): Promise<ViteHubAuth>;
|
|
16
18
|
declare function handleAuth(input: AuthRequestInput, runtimeOptions?: AuthRuntimeOptions): Promise<Response>;
|
|
17
19
|
declare function requireAuth(input: AuthRequestInput, definition?: AuthDefinition): Promise<Response | undefined>;
|
|
20
|
+
declare function requireAuthAccessRoutes(input: AuthRequestInput, routeIndexes: number[], definition?: AuthDefinition, requiredAuthorizeRouteIndexes?: number[]): Promise<Response | undefined>;
|
|
18
21
|
declare const auth: ViteHubAuth;
|
|
19
22
|
//#endregion
|
|
20
|
-
export { auth, createAuth, createAuthForRequest, createAuthHandler, createAuthRequestRuntimeOptions, createBetterAuthOptions, handleAuth as default, handleAuth, getAuth, getAuthForDefinition, getAuthForRequest, handleAuthRequest, requireAuth, resetAuth, setAuthRuntimeEnvResolver };
|
|
23
|
+
export { assertAuthOrigin, auth, createAuth, createAuthForRequest, createAuthHandler, createAuthRequestRuntimeOptions, createBetterAuthOptions, handleAuth as default, handleAuth, getAuth, getAuthForDefinition, getAuthForRequest, handleAuthRequest, requireAuth, requireAuthAccessRoutes, resetAuth, setAuthRuntimeEnvResolver };
|
package/dist/server.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { auth, createAuth, createAuthForRequest, createAuthHandler, createAuthRequestRuntimeOptions, createBetterAuthOptions, handleAuth as default, handleAuth, getAuth, getAuthForDefinition, getAuthForRequest, handleAuthRequest, requireAuth, resetAuth, setAuthRuntimeEnvResolver };
|
|
1
|
+
import { a as createAuthHandler, c as getAuth, d as handleAuth, f as handleAuthRequest, g as setAuthRuntimeEnvResolver, h as resetAuth, i as createAuthForRequest, l as getAuthForDefinition, m as requireAuthAccessRoutes, n as auth, o as createAuthRequestRuntimeOptions, p as requireAuth, r as createAuth, s as createBetterAuthOptions, t as assertAuthOrigin, u as getAuthForRequest } from "./server-BZn8iQrB.js";
|
|
2
|
+
export { assertAuthOrigin, auth, createAuth, createAuthForRequest, createAuthHandler, createAuthRequestRuntimeOptions, createBetterAuthOptions, handleAuth as default, handleAuth, getAuth, getAuthForDefinition, getAuthForRequest, handleAuthRequest, requireAuth, requireAuthAccessRoutes, resetAuth, setAuthRuntimeEnvResolver };
|
|
@@ -26,10 +26,20 @@ interface AuthSignInConfiguration {
|
|
|
26
26
|
scopes?: string[];
|
|
27
27
|
}
|
|
28
28
|
interface AuthAccessRouteConfiguration {
|
|
29
|
+
authorize?: AuthAccessAuthorize;
|
|
29
30
|
method?: string;
|
|
30
31
|
route: string;
|
|
31
32
|
}
|
|
32
33
|
type AuthAccessRoute = string | AuthAccessRouteConfiguration;
|
|
34
|
+
interface AuthAccessAuthorizationContext {
|
|
35
|
+
request: AuthRequest;
|
|
36
|
+
session: Record<string, unknown>;
|
|
37
|
+
user: Record<string, unknown> & {
|
|
38
|
+
id: string;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
type AuthAccessAuthorizeResult = boolean | Response;
|
|
42
|
+
type AuthAccessAuthorize = (context: AuthAccessAuthorizationContext) => AuthAccessAuthorizeResult | Promise<AuthAccessAuthorizeResult>;
|
|
33
43
|
interface AuthAccessConfiguration {
|
|
34
44
|
routes?: AuthAccessRoute[];
|
|
35
45
|
signIn?: AuthSignInConfiguration;
|
|
@@ -81,6 +91,7 @@ type ResolvedAuthSecondaryStorageConfiguration = {
|
|
|
81
91
|
store: string;
|
|
82
92
|
};
|
|
83
93
|
interface ResolvedAuthAccessRoute {
|
|
94
|
+
authorize?: true;
|
|
84
95
|
method?: string;
|
|
85
96
|
route: string;
|
|
86
97
|
}
|
|
@@ -103,4 +114,4 @@ type AuthRequestInput = AuthRequest | {
|
|
|
103
114
|
type ViteHubAuth<Options extends BetterAuthOptions = BetterAuthOptions> = Auth<Options>;
|
|
104
115
|
type AuthModuleOptions = false | Record<string, never>;
|
|
105
116
|
//#endregion
|
|
106
|
-
export {
|
|
117
|
+
export { AuthViteHubOptions as A, AuthRuntimeOnlyOptions as C, AuthSecondaryStorageConfiguration as D, AuthRuntimeOptionsResolver as E, ViteHubAuth as F, ResolvedAuthDatabaseConfiguration as M, ResolvedAuthSecondaryStorageConfiguration as N, AuthSecondaryStorageReference as O, ResolvedAuthViteConfig as P, AuthRuntimeEnv as S, AuthRuntimeOptions as T, AuthRequestInput as _, AuthAccessRoute as a, AuthRuntimeConfiguration as b, AuthBetterAuthRuntimeOptions as c, AuthDefinition as d, AuthDefinitionInput as f, AuthRequest as g, AuthModuleOptions as h, AuthAccessConfiguration as i, DiscoveredAuthDefinition as j, AuthSignInConfiguration as k, AuthDatabaseConfiguration as l, AuthDefinitionResolver as m, AuthAccessAuthorize as n, AuthAccessRouteConfiguration as o, AuthDefinitionOptions as p, AuthAccessAuthorizeResult as r, AuthBetterAuthOptions as s, AuthAccessAuthorizationContext as t, AuthDatabaseReference as u, AuthReservedOption as v, AuthRuntimeOption as w, AuthRuntimeContext as x, AuthResolvedDefinitionOptions as y };
|