@witchcraft/nuxt-auth 0.5.2 → 0.6.0-beta.1
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/README.md +188 -336
- package/dist/module.d.mts +64 -41
- package/dist/module.json +1 -1
- package/dist/module.mjs +25 -35
- package/dist/runtime/components/ExternalCallback.vue +11 -11
- package/dist/runtime/components/LoginProviderButtons.client.d.vue.ts +10 -15
- package/dist/runtime/components/LoginProviderButtons.client.vue +4 -10
- package/dist/runtime/components/LoginProviderButtons.client.vue.d.ts +10 -15
- package/dist/runtime/components/MockBypassRegistrationFields.d.vue.ts +13 -0
- package/dist/runtime/components/MockBypassRegistrationFields.vue +18 -0
- package/dist/runtime/components/MockBypassRegistrationFields.vue.d.ts +13 -0
- package/dist/runtime/components/Mocker.d.vue.ts +5 -1
- package/dist/runtime/components/Mocker.vue +7 -10
- package/dist/runtime/components/Mocker.vue.d.ts +5 -1
- package/dist/runtime/components/SessionStatus.d.vue.ts +86 -42
- package/dist/runtime/components/SessionStatus.vue +29 -8
- package/dist/runtime/components/SessionStatus.vue.d.ts +86 -42
- package/dist/runtime/components/UserRegistration.d.vue.ts +5 -0
- package/dist/runtime/components/UserRegistration.vue +2 -0
- package/dist/runtime/components/UserRegistration.vue.d.ts +5 -0
- package/dist/runtime/composables/useAuth.d.ts +382 -19
- package/dist/runtime/composables/useAuth.js +191 -28
- package/dist/runtime/composables/useAuthExternalCallback.d.ts +13 -6
- package/dist/runtime/composables/useAuthExternalCallback.js +27 -16
- package/dist/runtime/composables/useAuthExternalCode.d.ts +2 -2
- package/dist/runtime/composables/useAuthExternalCode.js +19 -18
- package/dist/runtime/composables/useAuthMocker.d.ts +11 -1
- package/dist/runtime/composables/useAuthMocker.js +21 -11
- package/dist/runtime/composables/useAuthUserRegistration.d.ts +5 -10
- package/dist/runtime/composables/useAuthUserRegistration.js +12 -25
- package/dist/runtime/core/providerStyles.js +12 -4
- package/dist/runtime/electron/index.d.ts +1 -0
- package/dist/runtime/electron/index.js +1 -0
- package/dist/runtime/electron/main/ElectronAuth.d.ts +123 -0
- package/dist/runtime/electron/main/ElectronAuth.js +200 -0
- package/dist/runtime/electron/main/index.d.ts +2 -0
- package/dist/runtime/electron/main/index.js +2 -0
- package/dist/runtime/electron/main/setupElectronAuthMain.d.ts +665 -0
- package/dist/runtime/electron/main/setupElectronAuthMain.js +137 -0
- package/dist/runtime/electron/preload/createElectronAuthApi.d.ts +7 -0
- package/dist/runtime/electron/preload/createElectronAuthApi.js +15 -0
- package/dist/runtime/electron/preload/index.d.ts +2 -0
- package/dist/runtime/electron/preload/index.js +2 -0
- package/dist/runtime/electron/types.d.ts +54 -0
- package/dist/runtime/electron/types.js +2 -0
- package/dist/runtime/middleware/authGlobal.js +30 -14
- package/dist/runtime/middleware/authProtected.js +2 -2
- package/dist/runtime/plugins/client/electronNuxtAuthPlugin.d.ts +48 -0
- package/dist/runtime/plugins/client/electronNuxtAuthPlugin.js +86 -0
- package/dist/runtime/plugins/client/localOnlyUsers.d.ts +25 -0
- package/dist/runtime/plugins/client/localOnlyUsers.js +110 -0
- package/dist/runtime/plugins/server/devMock.d.ts +51 -0
- package/dist/runtime/plugins/server/devMock.js +162 -0
- package/dist/runtime/server/utils/assertEventWithAuthorizedUser.d.ts +11 -0
- package/dist/runtime/server/utils/assertEventWithAuthorizedUser.js +10 -0
- package/dist/runtime/server/utils/createAuthMiddleware.d.ts +4 -4
- package/dist/runtime/server/utils/createAuthMiddleware.js +16 -23
- package/dist/runtime/server/utils/createAuthSchema.d.ts +391 -94
- package/dist/runtime/server/utils/createAuthSchema.js +43 -13
- package/dist/runtime/server/utils/createBetterAuth.d.ts +34 -0
- package/dist/runtime/server/utils/createBetterAuth.js +160 -0
- package/dist/runtime/server/utils/getBetterAuthInstance.d.ts +3 -0
- package/dist/runtime/server/utils/getBetterAuthInstance.js +10 -0
- package/dist/runtime/types.d.ts +358 -248
- package/dist/runtime/types.js +48 -25
- package/dist/runtime/utils/deserializeAuthLocalUser.d.ts +2 -0
- package/dist/runtime/utils/deserializeAuthLocalUser.js +9 -0
- package/package.json +44 -19
- package/dist/runtime/core/providers/github.d.ts +0 -84
- package/dist/runtime/core/providers/github.js +0 -55
- package/dist/runtime/core/providers/google.d.ts +0 -65
- package/dist/runtime/core/providers/google.js +0 -49
- package/dist/runtime/server/helpers/getSafeSecretsInfo.d.ts +0 -4
- package/dist/runtime/server/helpers/getSafeSecretsInfo.js +0 -10
- package/dist/runtime/server/helpers/logSafeRoute.d.ts +0 -1
- package/dist/runtime/server/helpers/logSafeRoute.js +0 -3
- package/dist/runtime/server/helpers/signJwt.d.ts +0 -3
- package/dist/runtime/server/helpers/signJwt.js +0 -16
- package/dist/runtime/server/helpers/verifyJwt.d.ts +0 -5
- package/dist/runtime/server/helpers/verifyJwt.js +0 -19
- package/dist/runtime/server/utils/Auth.d.ts +0 -85
- package/dist/runtime/server/utils/Auth.js +0 -671
- package/dist/runtime/server/utils/SessionManager.d.ts +0 -26
- package/dist/runtime/server/utils/SessionManager.js +0 -94
- package/dist/runtime/server/utils/createAuthHandler.d.ts +0 -9
- package/dist/runtime/server/utils/createAuthHandler.js +0 -29
- package/dist/runtime/utils/createExternalAuthLoginHandler.d.ts +0 -37
- package/dist/runtime/utils/createExternalAuthLoginHandler.js +0 -23
- package/dist/runtime/utils/getAuthApiRoute.d.ts +0 -6
- package/dist/runtime/utils/getAuthApiRoute.js +0 -14
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { sha256 } from "@oslojs/crypto/sha2";
|
|
2
|
-
import { encodeBase32LowerCaseNoPadding, encodeHexLowerCase } from "@oslojs/encoding";
|
|
3
|
-
import { eq } from "drizzle-orm";
|
|
4
|
-
export const defaultSessionCookieName = "authSession";
|
|
5
|
-
export class SessionManager {
|
|
6
|
-
options;
|
|
7
|
-
db;
|
|
8
|
-
sessionTable;
|
|
9
|
-
userTable;
|
|
10
|
-
constructor(rc, db, sessionTable, userTable, options = {}) {
|
|
11
|
-
this.db = db;
|
|
12
|
-
this.sessionTable = sessionTable;
|
|
13
|
-
this.userTable = userTable;
|
|
14
|
-
this.options = {
|
|
15
|
-
expiresAt: rc.public.auth.sessionExpiresAt,
|
|
16
|
-
...options,
|
|
17
|
-
sessionCookie: {
|
|
18
|
-
name: defaultSessionCookieName,
|
|
19
|
-
...options.sessionCookie,
|
|
20
|
-
attributes: {
|
|
21
|
-
secure: rc.public.auth.isSecure,
|
|
22
|
-
...rc.public.auth.sessionCookieOpts ?? {},
|
|
23
|
-
...options.sessionCookie?.attributes
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
generateSessionToken() {
|
|
29
|
-
const bytes = new Uint8Array(20);
|
|
30
|
-
crypto.getRandomValues(bytes);
|
|
31
|
-
const token = encodeBase32LowerCaseNoPadding(bytes);
|
|
32
|
-
return token;
|
|
33
|
-
}
|
|
34
|
-
async createSession(sessionToken, userId) {
|
|
35
|
-
const hashedSessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(sessionToken)));
|
|
36
|
-
const session = {
|
|
37
|
-
id: hashedSessionId,
|
|
38
|
-
userId,
|
|
39
|
-
expiresAt: new Date(Date.now() + this.options.expiresAt)
|
|
40
|
-
};
|
|
41
|
-
await this.db.insert(this.sessionTable).values(session);
|
|
42
|
-
return session;
|
|
43
|
-
}
|
|
44
|
-
createSessionCookie(hashedSessionId) {
|
|
45
|
-
return {
|
|
46
|
-
name: this.options.sessionCookie.name,
|
|
47
|
-
value: hashedSessionId,
|
|
48
|
-
attributes: {
|
|
49
|
-
...this.options.sessionCookie.attributes,
|
|
50
|
-
maxAge: (/* @__PURE__ */ new Date()).getTime() + this.options.expiresAt
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
createBlankSessionCookie() {
|
|
55
|
-
return {
|
|
56
|
-
name: this.options.sessionCookie.name,
|
|
57
|
-
value: "",
|
|
58
|
-
attributes: {
|
|
59
|
-
...this.options.sessionCookie.attributes,
|
|
60
|
-
maxAge: 0
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
async validateSessionToken(sessionToken) {
|
|
65
|
-
const hashedSessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(sessionToken)));
|
|
66
|
-
const result = await this.db.select({ user: this.userTable, session: this.sessionTable }).from(this.sessionTable).innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id)).where(eq(this.sessionTable.id, hashedSessionId));
|
|
67
|
-
if (result.length < 1) {
|
|
68
|
-
return { session: null, user: null, fresh: false };
|
|
69
|
-
}
|
|
70
|
-
const { user, session } = result[0];
|
|
71
|
-
const expired = Date.now() >= session.expiresAt.getTime();
|
|
72
|
-
if (expired) {
|
|
73
|
-
await this.db.delete(this.sessionTable).where(eq(this.sessionTable.id, session.id));
|
|
74
|
-
return { session: null, user: null, fresh: false };
|
|
75
|
-
}
|
|
76
|
-
const canBeExtended = Date.now() >= session.expiresAt.getTime() - this.options.expiresAt / 2;
|
|
77
|
-
if (canBeExtended) {
|
|
78
|
-
session.expiresAt = new Date(Date.now() + this.options.expiresAt);
|
|
79
|
-
await this.db.update(this.sessionTable).set({
|
|
80
|
-
expiresAt: session.expiresAt
|
|
81
|
-
}).where(eq(this.sessionTable.id, session.id));
|
|
82
|
-
}
|
|
83
|
-
return {
|
|
84
|
-
session,
|
|
85
|
-
user,
|
|
86
|
-
// cast because it contains extra attributes
|
|
87
|
-
fresh: canBeExtended
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
async invalidateSession(sessionId) {
|
|
91
|
-
const hashedSessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(sessionId)));
|
|
92
|
-
await this.db.delete(this.sessionTable).where(eq(this.sessionTable.id, hashedSessionId));
|
|
93
|
-
}
|
|
94
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { BaseLogger } from "@witchcraft/nuxt-logger/types";
|
|
2
|
-
import type { PgDatabase } from "drizzle-orm/pg-core";
|
|
3
|
-
import type { PgliteDatabase } from "drizzle-orm/pglite";
|
|
4
|
-
import type { EventHandler } from "h3";
|
|
5
|
-
import type { AuthAccountsTable, UserTable } from "./createAuthSchema.js";
|
|
6
|
-
import type { SessionManager } from "./SessionManager.js";
|
|
7
|
-
import type { ModuleRuntimeConfig } from "../../../module.js";
|
|
8
|
-
import type { AuthHandlerOptions } from "../../types.js";
|
|
9
|
-
export declare function createAuthHandler(rc: ModuleRuntimeConfig, db: PgDatabase<any, any, any> | PgliteDatabase<any>, usersTable: UserTable, authAccountsTable: AuthAccountsTable, sessionManager: SessionManager, logger: BaseLogger, opts?: Partial<AuthHandlerOptions>): EventHandler;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { createRouter } from "h3";
|
|
2
|
-
import { Auth } from "./Auth.js";
|
|
3
|
-
import github from "../../core/providers/github.js";
|
|
4
|
-
import google from "../../core/providers/google.js";
|
|
5
|
-
export function createAuthHandler(rc, db, usersTable, authAccountsTable, sessionManager, logger, opts = {}) {
|
|
6
|
-
const router = createRouter();
|
|
7
|
-
const auth = new Auth(
|
|
8
|
-
rc,
|
|
9
|
-
db,
|
|
10
|
-
usersTable,
|
|
11
|
-
authAccountsTable,
|
|
12
|
-
sessionManager,
|
|
13
|
-
router,
|
|
14
|
-
{
|
|
15
|
-
...opts,
|
|
16
|
-
enabledProviders: rc.public.auth.enabledProviders,
|
|
17
|
-
handlers: {
|
|
18
|
-
github,
|
|
19
|
-
google,
|
|
20
|
-
...opts.customProviders ?? {}
|
|
21
|
-
},
|
|
22
|
-
runtimeConfig: rc.public
|
|
23
|
-
},
|
|
24
|
-
rc,
|
|
25
|
-
// for the env vars
|
|
26
|
-
logger
|
|
27
|
-
);
|
|
28
|
-
return auth.eventHandler;
|
|
29
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { ActionHandler } from "../types.js";
|
|
2
|
-
/**
|
|
3
|
-
* Creates an external (e.g. desktop) auth login handler. A logout one doesn't exist as that is very platform dependant.
|
|
4
|
-
*/
|
|
5
|
-
export declare function createExternalAuthLoginHandler(
|
|
6
|
-
/**
|
|
7
|
-
* The name of the platform.
|
|
8
|
-
*
|
|
9
|
-
* The `deeplink` query param will be set to this.
|
|
10
|
-
*
|
|
11
|
-
* The actual protocol scheme it maps to can be set in {@link createAuthHandler}.
|
|
12
|
-
*/
|
|
13
|
-
name: string,
|
|
14
|
-
/**
|
|
15
|
-
* Should return whether on this platform.
|
|
16
|
-
*
|
|
17
|
-
* e.g. isElectron()
|
|
18
|
-
*/
|
|
19
|
-
isExternal: () => boolean,
|
|
20
|
-
/**
|
|
21
|
-
* The url of the server on which to open the auth action url.
|
|
22
|
-
*
|
|
23
|
-
* This is allowed to be undefined because it might only be available in some environments, BUT, it cannot be undefined when isExternal returns true.
|
|
24
|
-
*/
|
|
25
|
-
serverUrl: string | (() => string) | undefined,
|
|
26
|
-
/**
|
|
27
|
-
* The function on the external **platform** to open the url on a browser.
|
|
28
|
-
*
|
|
29
|
-
* This is allowed to be undefined because it might only be available in some environments, BUT, it cannot be undefined when isExternal returns true.
|
|
30
|
-
*/
|
|
31
|
-
open: ((url: string) => void | Promise<void>) | undefined,
|
|
32
|
-
/**
|
|
33
|
-
* The callback path on the external **platform**, it will be redirected here using `navigateTo`.
|
|
34
|
-
*
|
|
35
|
-
* @default runctimeConfig().public.auth.authRoutes.deeplink
|
|
36
|
-
*/
|
|
37
|
-
deeplinkCallbackPath?: string): ActionHandler;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { navigateTo, useRuntimeConfig } from "#imports";
|
|
2
|
-
export function createExternalAuthLoginHandler(name, isExternal, serverUrl, open, deeplinkCallbackPath = useRuntimeConfig().public.auth.authRoutes.deeplink) {
|
|
3
|
-
return (action, url) => {
|
|
4
|
-
if (action !== "login") return false;
|
|
5
|
-
if (!isExternal()) return false;
|
|
6
|
-
if (deeplinkCallbackPath === void 0) {
|
|
7
|
-
throw new Error("createExternalAuthLoginHandler: deeplinkCallbackPath cannot be undefined. See docs for more info.");
|
|
8
|
-
}
|
|
9
|
-
if (!serverUrl) throw new Error("createExternalAuthLoginHandler: serverUrl cannot be undefined when isExternal returns true. See docs for more info.");
|
|
10
|
-
if (!open) throw new Error("createExternalAuthLoginHandler: open cannot be undefined when isExternal returns true. See docs for more info.");
|
|
11
|
-
const origin = typeof serverUrl === "function" ? serverUrl() : serverUrl;
|
|
12
|
-
const serverAuth = new URL(url, origin);
|
|
13
|
-
serverAuth.searchParams.set("deeplink", name);
|
|
14
|
-
const serverAuthUri = encodeURIComponent(serverAuth.toString());
|
|
15
|
-
const callbackUrl = new URL(deeplinkCallbackPath, window.location.origin);
|
|
16
|
-
callbackUrl.searchParams.set("authUri", serverAuthUri);
|
|
17
|
-
void open(serverAuth.toString());
|
|
18
|
-
setTimeout(() => {
|
|
19
|
-
void navigateTo(callbackUrl.toString(), { external: true });
|
|
20
|
-
}, 0);
|
|
21
|
-
return true;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { ApiRoutesParams, ModulePublicRuntimeConfig } from "../../module.js";
|
|
2
|
-
export declare function getAuthApiRoute<T extends keyof ModulePublicRuntimeConfig["auth"]["authApiRoutes"], TParams extends T extends keyof ApiRoutesParams ? ApiRoutesParams[T] : never>(rc: {
|
|
3
|
-
auth: {
|
|
4
|
-
authApiRoutes: ModulePublicRuntimeConfig["auth"]["authApiRoutes"];
|
|
5
|
-
};
|
|
6
|
-
}, route: T, params?: TParams, queryParams?: Record<string, any>): string;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { keys } from "@alanscodelog/utils/keys";
|
|
2
|
-
export function getAuthApiRoute(rc, route, params, queryParams) {
|
|
3
|
-
const routes = rc.auth.authApiRoutes;
|
|
4
|
-
let finalRoute = routes[route];
|
|
5
|
-
if (!finalRoute) throw new Error(`Invalid route: ${route}`);
|
|
6
|
-
if (params) {
|
|
7
|
-
for (const key of keys(params)) {
|
|
8
|
-
finalRoute = finalRoute.replace(`:${key}`, params[key]);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
if (route === "base") return finalRoute;
|
|
12
|
-
const query = queryParams ? `?${new URLSearchParams(queryParams).toString()}` : "";
|
|
13
|
-
return `${routes.base}${finalRoute}${query}`;
|
|
14
|
-
}
|