@thecodeorigin/auth 0.0.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.
Files changed (50) hide show
  1. package/README.md +84 -0
  2. package/dist/contract/index.d.mts +117 -0
  3. package/dist/contract/index.d.ts +117 -0
  4. package/dist/contract/index.mjs +52 -0
  5. package/dist/module.d.mts +23 -0
  6. package/dist/module.d.ts +23 -0
  7. package/dist/module.json +9 -0
  8. package/dist/module.mjs +51 -0
  9. package/dist/runtime/app/composables/useAuth.d.ts +106 -0
  10. package/dist/runtime/app/composables/useAuth.js +60 -0
  11. package/dist/runtime/app/composables/useCasl.d.ts +8 -0
  12. package/dist/runtime/app/composables/useCasl.js +9 -0
  13. package/dist/runtime/app/middleware/auth.global.d.ts +2 -0
  14. package/dist/runtime/app/middleware/auth.global.js +11 -0
  15. package/dist/runtime/app/middleware/casl.global.d.ts +2 -0
  16. package/dist/runtime/app/middleware/casl.global.js +17 -0
  17. package/dist/runtime/app/plugins/0.session.d.ts +2 -0
  18. package/dist/runtime/app/plugins/0.session.js +11 -0
  19. package/dist/runtime/app/plugins/ability.d.ts +7 -0
  20. package/dist/runtime/app/plugins/ability.js +23 -0
  21. package/dist/runtime/server/api/_auth/impersonatable-users.get.d.ts +2 -0
  22. package/dist/runtime/server/api/_auth/impersonatable-users.get.js +9 -0
  23. package/dist/runtime/server/api/_auth/impersonate.post.d.ts +36 -0
  24. package/dist/runtime/server/api/_auth/impersonate.post.js +36 -0
  25. package/dist/runtime/server/api/_auth/organizations/switch.post.d.ts +36 -0
  26. package/dist/runtime/server/api/_auth/organizations/switch.post.js +16 -0
  27. package/dist/runtime/server/api/_auth/organizations.get.d.ts +8 -0
  28. package/dist/runtime/server/api/_auth/organizations.get.js +8 -0
  29. package/dist/runtime/server/api/_auth/session.get.d.ts +36 -0
  30. package/dist/runtime/server/api/_auth/session.get.js +6 -0
  31. package/dist/runtime/server/api/_auth/stop-impersonating.post.d.ts +36 -0
  32. package/dist/runtime/server/api/_auth/stop-impersonating.post.js +21 -0
  33. package/dist/runtime/server/index.d.ts +2 -0
  34. package/dist/runtime/server/index.js +1 -0
  35. package/dist/runtime/server/routes/callback.get.d.ts +2 -0
  36. package/dist/runtime/server/routes/callback.get.js +61 -0
  37. package/dist/runtime/server/routes/sign-in.get.d.ts +2 -0
  38. package/dist/runtime/server/routes/sign-in.get.js +24 -0
  39. package/dist/runtime/server/routes/sign-out.get.d.ts +2 -0
  40. package/dist/runtime/server/routes/sign-out.get.js +19 -0
  41. package/dist/runtime/server/tsconfig.json +3 -0
  42. package/dist/runtime/server/utils/idp.d.ts +11 -0
  43. package/dist/runtime/server/utils/idp.js +58 -0
  44. package/dist/runtime/server/utils/oidc.d.ts +29 -0
  45. package/dist/runtime/server/utils/oidc.js +62 -0
  46. package/dist/runtime/server/utils/session.d.ts +74 -0
  47. package/dist/runtime/server/utils/session.js +79 -0
  48. package/dist/runtime/types.d.ts +17 -0
  49. package/dist/types.d.mts +3 -0
  50. package/package.json +77 -0
@@ -0,0 +1,74 @@
1
+ import type { H3Event } from 'h3';
2
+ import type { AbilityRule, PublicSession } from '../../../contract/index.js';
3
+ export interface SessionRecord {
4
+ sub: string;
5
+ user: {
6
+ sub: string;
7
+ email: string;
8
+ name: string | null;
9
+ picture: string | null;
10
+ };
11
+ abilities: AbilityRule[];
12
+ systemRole: string | null;
13
+ organizations: PublicSession['organizations'];
14
+ activeOrg: string | null;
15
+ entitlement: PublicSession['entitlement'];
16
+ accessToken: string;
17
+ refreshToken: string | null;
18
+ accessExpiresAt: number;
19
+ isImpersonation: boolean;
20
+ impersonator: {
21
+ sub: string;
22
+ email: string;
23
+ name: string | null;
24
+ picture: string | null;
25
+ } | null;
26
+ backupId: string | null;
27
+ }
28
+ export declare function newSessionId(): string;
29
+ export declare function writeSessionRecord(id: string, rec: SessionRecord): Promise<void>;
30
+ export declare function readSessionRecord(event: H3Event): Promise<{
31
+ id: string;
32
+ rec: SessionRecord;
33
+ } | null>;
34
+ export declare function setSessionCookie(event: H3Event, id: string): Promise<void>;
35
+ export declare function destroySession(event: H3Event): Promise<SessionRecord | null>;
36
+ export declare function readSessionRecordById(id: string): Promise<SessionRecord | null>;
37
+ /** Server-safe session for domain routes — no tokens, no internal storage fields. */
38
+ export declare function getServerAuthSession(event: H3Event): Promise<{
39
+ sub: string;
40
+ email: string;
41
+ name: string | null;
42
+ picture: string | null;
43
+ systemRole: string | null;
44
+ abilities: {
45
+ action: string;
46
+ subject: string;
47
+ conditions?: Record<string, string> | undefined;
48
+ }[];
49
+ organizations: {
50
+ id: string;
51
+ slug: string;
52
+ name: string;
53
+ role: string;
54
+ personal: boolean;
55
+ }[];
56
+ activeOrg: string | null;
57
+ entitlement: {
58
+ product: string;
59
+ plan: string;
60
+ status: string;
61
+ active: boolean;
62
+ currentPeriodEnd: number | null;
63
+ } | null;
64
+ isImpersonation: boolean;
65
+ impersonator: {
66
+ sub: string;
67
+ email: string;
68
+ name: string | null;
69
+ picture: string | null;
70
+ } | null;
71
+ } | null>;
72
+ export type ServerAuthSession = NonNullable<Awaited<ReturnType<typeof getServerAuthSession>>>;
73
+ /** Browser-safe projection — tokens are excluded. */
74
+ export declare function toPublicSession(rec: SessionRecord): PublicSession;
@@ -0,0 +1,79 @@
1
+ import { deleteCookie, getCookie, setCookie } from "h3";
2
+ import { useStorage } from "nitropack/runtime";
3
+ import { resolveAuthConfig } from "./oidc.js";
4
+ function store(base) {
5
+ return useStorage(base);
6
+ }
7
+ function key(id) {
8
+ return `session:${id}`;
9
+ }
10
+ export function newSessionId() {
11
+ const a = new Uint8Array(32);
12
+ crypto.getRandomValues(a);
13
+ return [...a].map((b) => b.toString(16).padStart(2, "0")).join("");
14
+ }
15
+ export async function writeSessionRecord(id, rec) {
16
+ const cfg = resolveAuthConfig();
17
+ await store(cfg.storageBase).setItem(key(id), rec);
18
+ }
19
+ export async function readSessionRecord(event) {
20
+ const cfg = resolveAuthConfig();
21
+ const id = getCookie(event, cfg.cookieName);
22
+ if (!id)
23
+ return null;
24
+ const rec = await store(cfg.storageBase).getItem(key(id));
25
+ return rec ? { id, rec } : null;
26
+ }
27
+ export async function setSessionCookie(event, id) {
28
+ const cfg = resolveAuthConfig();
29
+ setCookie(event, cfg.cookieName, id, {
30
+ httpOnly: true,
31
+ secure: !import.meta.dev,
32
+ sameSite: "lax",
33
+ path: "/",
34
+ maxAge: 60 * 60 * 24 * 7
35
+ });
36
+ }
37
+ export async function destroySession(event) {
38
+ const cfg = resolveAuthConfig();
39
+ const id = getCookie(event, cfg.cookieName);
40
+ if (!id)
41
+ return null;
42
+ const rec = await store(cfg.storageBase).getItem(key(id));
43
+ await store(cfg.storageBase).removeItem(key(id));
44
+ deleteCookie(event, cfg.cookieName, { path: "/" });
45
+ return rec;
46
+ }
47
+ export async function readSessionRecordById(id) {
48
+ const cfg = resolveAuthConfig();
49
+ return store(cfg.storageBase).getItem(key(id));
50
+ }
51
+ export async function getServerAuthSession(event) {
52
+ const s = await readSessionRecord(event);
53
+ if (!s)
54
+ return null;
55
+ return {
56
+ sub: s.rec.sub,
57
+ email: s.rec.user.email,
58
+ name: s.rec.user.name,
59
+ picture: s.rec.user.picture,
60
+ systemRole: s.rec.systemRole,
61
+ abilities: s.rec.abilities,
62
+ organizations: s.rec.organizations,
63
+ activeOrg: s.rec.activeOrg,
64
+ entitlement: s.rec.entitlement,
65
+ isImpersonation: s.rec.isImpersonation,
66
+ impersonator: s.rec.impersonator
67
+ };
68
+ }
69
+ export function toPublicSession(rec) {
70
+ return {
71
+ user: rec.user,
72
+ abilities: rec.abilities,
73
+ systemRole: rec.systemRole,
74
+ organizations: rec.organizations,
75
+ activeOrg: rec.activeOrg,
76
+ entitlement: rec.entitlement,
77
+ impersonator: rec.impersonator
78
+ };
79
+ }
@@ -0,0 +1,17 @@
1
+ declare module '#app' {
2
+ interface PageMeta {
3
+ public?: boolean
4
+ unauthenticatedOnly?: boolean
5
+ can?: string[]
6
+ }
7
+ }
8
+
9
+ declare module 'vue-router' {
10
+ interface RouteMeta {
11
+ public?: boolean
12
+ unauthenticatedOnly?: boolean
13
+ can?: string[]
14
+ }
15
+ }
16
+
17
+ export {}
@@ -0,0 +1,3 @@
1
+ export { default } from './module.mjs'
2
+
3
+ export { type AuthRoutes, type ModuleOptions } from './module.mjs'
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@thecodeorigin/auth",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "private": false,
6
+ "description": "THECODEORIGIN Authentication Portal client module",
7
+ "repository": "https://github.com/thecodeorigin/auth",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/types.d.mts",
11
+ "import": "./dist/module.mjs"
12
+ },
13
+ "./contract": {
14
+ "types": "./dist/contract/index.d.mts",
15
+ "import": "./dist/contract/index.mjs"
16
+ },
17
+ "./server": {
18
+ "types": "./dist/runtime/server/index.d.ts",
19
+ "import": "./dist/runtime/server/index.js"
20
+ }
21
+ },
22
+ "main": "./dist/module.mjs",
23
+ "typesVersions": {
24
+ "*": {
25
+ ".": [
26
+ "./dist/types.d.mts"
27
+ ],
28
+ "contract": [
29
+ "./dist/contract/index.d.mts"
30
+ ],
31
+ "server": [
32
+ "./dist/runtime/server/index.d.ts"
33
+ ]
34
+ }
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "workspaces": [
40
+ "playground"
41
+ ],
42
+ "dependencies": {
43
+ "@casl/ability": "^6.8.1",
44
+ "@casl/vue": "^2.2.6",
45
+ "@nuxt/kit": "^4.4.8",
46
+ "defu": "^6.1.4",
47
+ "h3": "^1.14.0",
48
+ "ofetch": "^1.4.1",
49
+ "ufo": "^1.5.4",
50
+ "zod": "^4.3.6"
51
+ },
52
+ "devDependencies": {
53
+ "@nuxt/devtools": "^3.2.4",
54
+ "@nuxt/eslint-config": "^1.15.2",
55
+ "@nuxt/module-builder": "^1.0.2",
56
+ "@nuxt/schema": "^4.4.8",
57
+ "@nuxt/test-utils": "^4.0.3",
58
+ "@types/node": "latest",
59
+ "changelogen": "^0.6.2",
60
+ "eslint": "^10.4.1",
61
+ "nuxt": "^4.4.8",
62
+ "typescript": "~6.0.3",
63
+ "unbuild": "^3.6.1",
64
+ "vitest": "^4.1.8",
65
+ "vue-tsc": "^3.3.3"
66
+ },
67
+ "scripts": {
68
+ "dev": "npm run dev:prepare && nuxt dev playground",
69
+ "dev:build": "nuxt build playground",
70
+ "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt prepare playground",
71
+ "release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
72
+ "lint": "eslint .",
73
+ "test": "vitest run",
74
+ "test:watch": "vitest watch",
75
+ "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
76
+ }
77
+ }