hazo_auth 10.26.2 → 10.31.0
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/cli-src/lib/auth/auth_cache.ts +11 -1
- package/cli-src/lib/auth/auth_types.ts +6 -0
- package/cli-src/lib/auth/hazo_get_auth.server.ts +66 -8
- package/cli-src/lib/auth/hazo_get_tenant_auth.server.ts +9 -45
- package/cli-src/lib/auth/scope_header.ts +58 -0
- package/cli-src/lib/email_verification_config.server.ts +3 -0
- package/cli-src/lib/forgot_password_config.server.ts +3 -0
- package/cli-src/lib/register_config.server.ts +3 -0
- package/cli-src/lib/reset_password_config.server.ts +3 -0
- package/cli-src/lib/schema/sqlite_schema.ts +150 -38
- package/cli-src/lib/services/index.ts +1 -0
- package/cli-src/lib/services/registration_service.ts +29 -4
- package/cli-src/lib/services/scope_membership_service.ts +68 -0
- package/cli-src/lib/services/scope_service.ts +21 -0
- package/cli-src/lib/services/user_scope_service.ts +4 -32
- package/dist/components/layouts/email_verification/index.d.ts +3 -1
- package/dist/components/layouts/email_verification/index.d.ts.map +1 -1
- package/dist/components/layouts/email_verification/index.js +4 -4
- package/dist/components/layouts/forgot_password/index.d.ts +3 -1
- package/dist/components/layouts/forgot_password/index.d.ts.map +1 -1
- package/dist/components/layouts/forgot_password/index.js +2 -2
- package/dist/components/layouts/register/hooks/use_register_form.d.ts +6 -1
- package/dist/components/layouts/register/hooks/use_register_form.d.ts.map +1 -1
- package/dist/components/layouts/register/hooks/use_register_form.js +4 -3
- package/dist/components/layouts/register/index.d.ts +7 -1
- package/dist/components/layouts/register/index.d.ts.map +1 -1
- package/dist/components/layouts/register/index.js +3 -2
- package/dist/components/layouts/reset_password/index.d.ts +3 -1
- package/dist/components/layouts/reset_password/index.d.ts.map +1 -1
- package/dist/components/layouts/reset_password/index.js +5 -5
- package/dist/lib/auth/auth_cache.d.ts +6 -2
- package/dist/lib/auth/auth_cache.d.ts.map +1 -1
- package/dist/lib/auth/auth_cache.js +6 -2
- package/dist/lib/auth/auth_types.d.ts +1 -0
- package/dist/lib/auth/auth_types.d.ts.map +1 -1
- package/dist/lib/auth/hazo_get_auth.server.d.ts.map +1 -1
- package/dist/lib/auth/hazo_get_auth.server.js +61 -8
- package/dist/lib/auth/hazo_get_tenant_auth.server.d.ts +2 -18
- package/dist/lib/auth/hazo_get_tenant_auth.server.d.ts.map +1 -1
- package/dist/lib/auth/hazo_get_tenant_auth.server.js +5 -38
- package/dist/lib/auth/scope_header.d.ts +28 -0
- package/dist/lib/auth/scope_header.d.ts.map +1 -0
- package/dist/lib/auth/scope_header.js +34 -0
- package/dist/lib/email_verification_config.server.d.ts +2 -0
- package/dist/lib/email_verification_config.server.d.ts.map +1 -1
- package/dist/lib/email_verification_config.server.js +1 -0
- package/dist/lib/forgot_password_config.server.d.ts +2 -0
- package/dist/lib/forgot_password_config.server.d.ts.map +1 -1
- package/dist/lib/forgot_password_config.server.js +1 -0
- package/dist/lib/register_config.server.d.ts +2 -0
- package/dist/lib/register_config.server.d.ts.map +1 -1
- package/dist/lib/register_config.server.js +1 -0
- package/dist/lib/reset_password_config.server.d.ts +2 -0
- package/dist/lib/reset_password_config.server.d.ts.map +1 -1
- package/dist/lib/reset_password_config.server.js +1 -0
- package/dist/lib/schema/sqlite_schema.d.ts +1 -1
- package/dist/lib/schema/sqlite_schema.d.ts.map +1 -1
- package/dist/lib/schema/sqlite_schema.js +150 -38
- package/dist/lib/services/index.d.ts +1 -0
- package/dist/lib/services/index.d.ts.map +1 -1
- package/dist/lib/services/index.js +1 -0
- package/dist/lib/services/registration_service.d.ts +11 -0
- package/dist/lib/services/registration_service.d.ts.map +1 -1
- package/dist/lib/services/registration_service.js +19 -5
- package/dist/lib/services/scope_membership_service.d.ts +20 -0
- package/dist/lib/services/scope_membership_service.d.ts.map +1 -0
- package/dist/lib/services/scope_membership_service.js +49 -0
- package/dist/lib/services/scope_service.d.ts +6 -0
- package/dist/lib/services/scope_service.d.ts.map +1 -1
- package/dist/lib/services/scope_service.js +12 -0
- package/dist/lib/services/user_scope_service.d.ts.map +1 -1
- package/dist/lib/services/user_scope_service.js +4 -30
- package/dist/server/routes/register.d.ts.map +1 -1
- package/dist/server/routes/register.js +7 -2
- package/dist/server/routes/user_management_users.d.ts +1 -1
- package/dist/server_pages/forgot_password.d.ts.map +1 -1
- package/dist/server_pages/forgot_password.js +1 -1
- package/dist/server_pages/forgot_password_client_wrapper.d.ts +1 -1
- package/dist/server_pages/forgot_password_client_wrapper.d.ts.map +1 -1
- package/dist/server_pages/forgot_password_client_wrapper.js +2 -2
- package/dist/server_pages/register.d.ts +19 -1
- package/dist/server_pages/register.d.ts.map +1 -1
- package/dist/server_pages/register.js +4 -4
- package/dist/server_pages/register_client_wrapper.d.ts +3 -1
- package/dist/server_pages/register_client_wrapper.d.ts.map +1 -1
- package/dist/server_pages/register_client_wrapper.js +2 -2
- package/dist/server_pages/reset_password.d.ts.map +1 -1
- package/dist/server_pages/reset_password.js +1 -1
- package/dist/server_pages/reset_password_client_wrapper.d.ts +1 -1
- package/dist/server_pages/reset_password_client_wrapper.d.ts.map +1 -1
- package/dist/server_pages/reset_password_client_wrapper.js +2 -2
- package/dist/server_pages/verify_email.d.ts.map +1 -1
- package/dist/server_pages/verify_email.js +1 -1
- package/dist/server_pages/verify_email_client_wrapper.d.ts +1 -1
- package/dist/server_pages/verify_email_client_wrapper.d.ts.map +1 -1
- package/dist/server_pages/verify_email_client_wrapper.js +2 -2
- package/package.json +13 -12
|
@@ -14,6 +14,11 @@ type CacheEntry = {
|
|
|
14
14
|
permissions: string[];
|
|
15
15
|
role_ids: string[];
|
|
16
16
|
scopes: ScopeDetails[]; // All user's scope assignments with full details
|
|
17
|
+
// v10.29: per-role permission breakdown (role_id -> permission names), so
|
|
18
|
+
// scope-aware resolution (hazo_get_auth) can filter down to a single active
|
|
19
|
+
// scope's role without a second DB round trip. `permissions` above stays the
|
|
20
|
+
// union across all roles — used as the HRBAC-disabled/no-active-scope fallback.
|
|
21
|
+
permissions_by_role: Record<string, string[]>;
|
|
17
22
|
timestamp: number; // Unix timestamp in milliseconds
|
|
18
23
|
cache_version: number; // Version number for smart invalidation
|
|
19
24
|
};
|
|
@@ -83,9 +88,12 @@ class AuthCache {
|
|
|
83
88
|
* Evicts least recently used entries if cache is full
|
|
84
89
|
* @param user_id - User ID
|
|
85
90
|
* @param user - User data
|
|
86
|
-
* @param permissions - User permissions
|
|
91
|
+
* @param permissions - User permissions (union across all scope roles)
|
|
87
92
|
* @param role_ids - User role IDs (v5.x: string UUIDs)
|
|
88
93
|
* @param scopes - User scope details with full information (v5.2+)
|
|
94
|
+
* @param permissions_by_role - Per-role permission breakdown (v10.29+), used
|
|
95
|
+
* for HRBAC scope-aware filtering. Defaults to {} for callers that haven't
|
|
96
|
+
* been updated yet (scope-aware resolution simply won't have data to filter).
|
|
89
97
|
*/
|
|
90
98
|
set(
|
|
91
99
|
user_id: string,
|
|
@@ -93,6 +101,7 @@ class AuthCache {
|
|
|
93
101
|
permissions: string[],
|
|
94
102
|
role_ids: string[],
|
|
95
103
|
scopes: ScopeDetails[] = [],
|
|
104
|
+
permissions_by_role: Record<string, string[]> = {},
|
|
96
105
|
): void {
|
|
97
106
|
// Evict LRU entries if cache is full
|
|
98
107
|
while (this.cache.size >= this.max_size) {
|
|
@@ -112,6 +121,7 @@ class AuthCache {
|
|
|
112
121
|
permissions,
|
|
113
122
|
role_ids,
|
|
114
123
|
scopes,
|
|
124
|
+
permissions_by_role,
|
|
115
125
|
timestamp: Date.now(),
|
|
116
126
|
cache_version,
|
|
117
127
|
};
|
|
@@ -25,6 +25,12 @@ export type HazoAuthUser = {
|
|
|
25
25
|
country: string | null;
|
|
26
26
|
state_province: string | null;
|
|
27
27
|
suburb: string | null;
|
|
28
|
+
// The type key from [hazo_auth__user_types] (e.g. "parent", "teacher"), or
|
|
29
|
+
// null when the feature is disabled, unset for this user, or the user
|
|
30
|
+
// predates activation. See user_types_config.server.ts for the config
|
|
31
|
+
// shape and get_user_type_by_key() for resolving this to a display
|
|
32
|
+
// label/badge color.
|
|
33
|
+
user_type: string | null;
|
|
28
34
|
};
|
|
29
35
|
|
|
30
36
|
/**
|
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
import { get_cookie_name, BASE_COOKIE_NAMES } from "../cookies_config.server.js";
|
|
32
32
|
import { get_app_permission_descriptions } from "../app_permissions_config.server.js";
|
|
33
33
|
import { GLOBAL_ADMIN_PERMISSION } from "../constants.js";
|
|
34
|
+
import { extract_scope_id_from_request } from "./scope_header.js";
|
|
34
35
|
|
|
35
36
|
// section: helpers
|
|
36
37
|
|
|
@@ -162,6 +163,7 @@ async function fetch_user_data_from_db(user_id: string): Promise<{
|
|
|
162
163
|
permissions: string[];
|
|
163
164
|
role_ids: string[];
|
|
164
165
|
scopes: ScopeDetails[];
|
|
166
|
+
permissions_by_role: Record<string, string[]>;
|
|
165
167
|
}> {
|
|
166
168
|
const hazoConnect = get_hazo_connect_instance();
|
|
167
169
|
const users_service = createCrudService(hazoConnect, "hazo_users");
|
|
@@ -210,6 +212,7 @@ async function fetch_user_data_from_db(user_id: string): Promise<{
|
|
|
210
212
|
country: (user_db.country as string | null | undefined) || null,
|
|
211
213
|
state_province: (user_db.state_province as string | null | undefined) || null,
|
|
212
214
|
suburb: (user_db.suburb as string | null | undefined) || null,
|
|
215
|
+
user_type: (user_db.user_type as string | null | undefined) || null,
|
|
213
216
|
};
|
|
214
217
|
|
|
215
218
|
// v5.x: Fetch user's roles from hazo_user_scopes (scope-based role assignments)
|
|
@@ -226,8 +229,12 @@ async function fetch_user_data_from_db(user_id: string): Promise<{
|
|
|
226
229
|
}
|
|
227
230
|
const role_ids = Array.from(role_ids_set);
|
|
228
231
|
|
|
229
|
-
// Fetch role permissions
|
|
232
|
+
// Fetch role permissions. Builds both the flattened union (`permissions`,
|
|
233
|
+
// used when HRBAC is disabled or no active scope is set) and a per-role
|
|
234
|
+
// breakdown (`permissions_by_role`, used by hazo_get_auth to filter down to
|
|
235
|
+
// just the active scope's role — see "section: scope_aware_permissions" below).
|
|
230
236
|
const permissions_set = new Set<string>();
|
|
237
|
+
const permissions_by_role: Record<string, string[]> = {};
|
|
231
238
|
if (role_ids.length > 0) {
|
|
232
239
|
const role_permissions = await role_permissions_service.findBy({});
|
|
233
240
|
if (Array.isArray(role_permissions)) {
|
|
@@ -245,7 +252,9 @@ async function fetch_user_data_from_db(user_id: string): Promise<{
|
|
|
245
252
|
}
|
|
246
253
|
}
|
|
247
254
|
|
|
248
|
-
// Fetch permission names
|
|
255
|
+
// Fetch permission names, keyed by permission_id, so we can resolve
|
|
256
|
+
// both the flattened union and each role's own subset from one query.
|
|
257
|
+
const permission_names_by_id = new Map<string, string>();
|
|
249
258
|
if (permission_ids.size > 0) {
|
|
250
259
|
const permissions = await permissions_service.findBy({});
|
|
251
260
|
if (Array.isArray(permissions)) {
|
|
@@ -254,12 +263,28 @@ async function fetch_user_data_from_db(user_id: string): Promise<{
|
|
|
254
263
|
if (perm_id !== undefined && permission_ids.has(String(perm_id))) {
|
|
255
264
|
const perm_name = perm.permission_name as string | undefined;
|
|
256
265
|
if (perm_name) {
|
|
257
|
-
|
|
266
|
+
permission_names_by_id.set(String(perm_id), perm_name);
|
|
258
267
|
}
|
|
259
268
|
}
|
|
260
269
|
}
|
|
261
270
|
}
|
|
262
271
|
}
|
|
272
|
+
|
|
273
|
+
for (const role_id of role_ids) {
|
|
274
|
+
permissions_by_role[role_id] = [];
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
for (const rp of user_role_permissions) {
|
|
278
|
+
const perm_id = rp.permission_id as string | number | undefined;
|
|
279
|
+
if (perm_id === undefined) continue;
|
|
280
|
+
const perm_name = permission_names_by_id.get(String(perm_id));
|
|
281
|
+
if (!perm_name) continue;
|
|
282
|
+
|
|
283
|
+
permissions_set.add(perm_name);
|
|
284
|
+
|
|
285
|
+
const role_id = String(rp.role_id);
|
|
286
|
+
(permissions_by_role[role_id] ??= []).push(perm_name);
|
|
287
|
+
}
|
|
263
288
|
}
|
|
264
289
|
}
|
|
265
290
|
|
|
@@ -268,7 +293,7 @@ async function fetch_user_data_from_db(user_id: string): Promise<{
|
|
|
268
293
|
// v5.2: Fetch full scope details for caching
|
|
269
294
|
const scopes = await fetch_user_scope_details(user_id);
|
|
270
295
|
|
|
271
|
-
return { user, permissions, role_ids, scopes };
|
|
296
|
+
return { user, permissions, role_ids, scopes, permissions_by_role };
|
|
272
297
|
}
|
|
273
298
|
|
|
274
299
|
/**
|
|
@@ -500,12 +525,16 @@ export async function hazo_get_auth(
|
|
|
500
525
|
let user: HazoAuthUser;
|
|
501
526
|
let permissions: string[];
|
|
502
527
|
let role_ids: string[]; // v5.x: role_ids are now string UUIDs
|
|
528
|
+
let scopes: ScopeDetails[];
|
|
529
|
+
let permissions_by_role: Record<string, string[]>;
|
|
503
530
|
|
|
504
531
|
if (cached_entry) {
|
|
505
532
|
// Cache hit
|
|
506
533
|
user = cached_entry.user;
|
|
507
534
|
permissions = cached_entry.permissions;
|
|
508
535
|
role_ids = cached_entry.role_ids;
|
|
536
|
+
scopes = cached_entry.scopes;
|
|
537
|
+
permissions_by_role = cached_entry.permissions_by_role;
|
|
509
538
|
} else {
|
|
510
539
|
// Cache miss - fetch from database
|
|
511
540
|
try {
|
|
@@ -513,9 +542,11 @@ export async function hazo_get_auth(
|
|
|
513
542
|
user = user_data.user;
|
|
514
543
|
permissions = user_data.permissions;
|
|
515
544
|
role_ids = user_data.role_ids;
|
|
545
|
+
scopes = user_data.scopes;
|
|
546
|
+
permissions_by_role = user_data.permissions_by_role;
|
|
516
547
|
|
|
517
|
-
// Update cache (v5.2: includes scope details for tenant auth)
|
|
518
|
-
cache.set(user_id, user, permissions, role_ids,
|
|
548
|
+
// Update cache (v5.2: includes scope details for tenant auth; v10.29: per-role breakdown)
|
|
549
|
+
cache.set(user_id, user, permissions, role_ids, scopes, permissions_by_role);
|
|
519
550
|
} catch (error) {
|
|
520
551
|
const error_message =
|
|
521
552
|
error instanceof Error ? error.message : "Unknown error";
|
|
@@ -536,6 +567,35 @@ export async function hazo_get_auth(
|
|
|
536
567
|
}
|
|
537
568
|
}
|
|
538
569
|
|
|
570
|
+
// section: scope_aware_permissions (ticket 12 — HRBAC scope-aware permissions)
|
|
571
|
+
//
|
|
572
|
+
// `permissions` above is the union across every scope the user belongs to — the
|
|
573
|
+
// pre-existing behavior, which this block preserves as the fallback whenever:
|
|
574
|
+
// - HRBAC is disabled, or
|
|
575
|
+
// - no active scope is set (no X-Hazo-Scope-Id header/cookie, and the caller
|
|
576
|
+
// didn't pass options.scope_id), or
|
|
577
|
+
// - the user holds the global-admin permission, which transcends scope by
|
|
578
|
+
// design (identical bypass to the scope-ACCESS check further below).
|
|
579
|
+
//
|
|
580
|
+
// Otherwise, permissions are narrowed to ONLY the role assigned in the active
|
|
581
|
+
// scope — fixing the gap where a role in scope A incorrectly granted its
|
|
582
|
+
// permissions while the user was operating in unrelated scope B. A user with no
|
|
583
|
+
// hazo_user_scopes row for the active scope gets zero permissions there (not the
|
|
584
|
+
// union) — they are simply not a member of that scope.
|
|
585
|
+
const hrbac_enabled = is_hrbac_enabled();
|
|
586
|
+
const active_scope_id = options?.scope_id ?? extract_scope_id_from_request(request);
|
|
587
|
+
|
|
588
|
+
if (
|
|
589
|
+
hrbac_enabled &&
|
|
590
|
+
active_scope_id &&
|
|
591
|
+
!permissions.includes(GLOBAL_ADMIN_PERMISSION)
|
|
592
|
+
) {
|
|
593
|
+
const active_scope = scopes.find((s) => s.id === active_scope_id);
|
|
594
|
+
permissions = active_scope
|
|
595
|
+
? permissions_by_role[active_scope.role_id] ?? []
|
|
596
|
+
: [];
|
|
597
|
+
}
|
|
598
|
+
|
|
539
599
|
// Check permissions if required
|
|
540
600
|
let permission_ok = true;
|
|
541
601
|
let missing_permissions: string[] | undefined;
|
|
@@ -588,8 +648,6 @@ export async function hazo_get_auth(
|
|
|
588
648
|
let scope_ok: boolean | undefined;
|
|
589
649
|
let scope_access_via: ScopeAccessInfo | undefined;
|
|
590
650
|
|
|
591
|
-
const hrbac_enabled = is_hrbac_enabled();
|
|
592
|
-
|
|
593
651
|
if (hrbac_enabled && options?.scope_id) {
|
|
594
652
|
// Global admin permission grants access to all scopes
|
|
595
653
|
const has_global_admin = permissions.includes(GLOBAL_ADMIN_PERMISSION);
|
|
@@ -9,8 +9,12 @@ import { get_auth_cache } from "./auth_cache.js";
|
|
|
9
9
|
import { get_scope_by_id } from "../services/scope_service.js";
|
|
10
10
|
import { GLOBAL_ADMIN_PERMISSION } from "../constants.js";
|
|
11
11
|
import { get_hazo_connect_instance } from "../hazo_connect_instance.server.js";
|
|
12
|
-
import { get_cookie_name } from "../cookies_config.server.js";
|
|
13
12
|
import { get_auth_utility_config } from "../auth_utility_config.server.js";
|
|
13
|
+
import {
|
|
14
|
+
extract_scope_id_from_request,
|
|
15
|
+
SCOPE_COOKIE_BASE_NAME,
|
|
16
|
+
get_scope_cookie_name,
|
|
17
|
+
} from "./scope_header.js";
|
|
14
18
|
import type {
|
|
15
19
|
TenantAuthOptions,
|
|
16
20
|
TenantAuthResult,
|
|
@@ -24,53 +28,13 @@ import {
|
|
|
24
28
|
TenantAccessDeniedError,
|
|
25
29
|
} from "./auth_types.js";
|
|
26
30
|
|
|
27
|
-
// section:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*/
|
|
32
|
-
const DEFAULT_SCOPE_HEADER = "X-Hazo-Scope-Id";
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Base cookie name for scope ID (will have prefix applied).
|
|
36
|
-
*
|
|
37
|
-
* Exported so a consuming app can SET the same cookie this module READS —
|
|
38
|
-
* without it, an app implementing a scope switcher has to duplicate the
|
|
39
|
-
* configured prefix and drift the moment it changes.
|
|
40
|
-
*/
|
|
41
|
-
export const SCOPE_COOKIE_BASE_NAME = "hazo_auth_scope_id";
|
|
42
|
-
|
|
43
|
-
/** The live, prefixed scope cookie name for this deployment. */
|
|
44
|
-
export function get_scope_cookie_name(): string {
|
|
45
|
-
return get_cookie_name(SCOPE_COOKIE_BASE_NAME);
|
|
46
|
-
}
|
|
31
|
+
// section: re_exports
|
|
32
|
+
// Moved to scope_header.ts (shared with hazo_get_auth's scope-aware permission
|
|
33
|
+
// resolution) — re-exported here so existing consumers of this module keep working.
|
|
34
|
+
export { extract_scope_id_from_request, SCOPE_COOKIE_BASE_NAME, get_scope_cookie_name };
|
|
47
35
|
|
|
48
36
|
// section: helpers
|
|
49
37
|
|
|
50
|
-
/**
|
|
51
|
-
* Extracts scope ID from request headers or cookies
|
|
52
|
-
* Priority: Header > Cookie
|
|
53
|
-
* @param request - NextRequest object
|
|
54
|
-
* @param options - TenantAuthOptions for customization
|
|
55
|
-
* @returns Scope ID if found, undefined otherwise
|
|
56
|
-
*/
|
|
57
|
-
export function extract_scope_id_from_request(
|
|
58
|
-
request: NextRequest,
|
|
59
|
-
options: TenantAuthOptions,
|
|
60
|
-
): string | undefined {
|
|
61
|
-
// Check header first
|
|
62
|
-
const header_name = options.scope_header_name || DEFAULT_SCOPE_HEADER;
|
|
63
|
-
const header_value = request.headers.get(header_name);
|
|
64
|
-
if (header_value) {
|
|
65
|
-
return header_value;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Check cookie (with prefix)
|
|
69
|
-
const cookie_name = options.scope_cookie_name || get_cookie_name(SCOPE_COOKIE_BASE_NAME);
|
|
70
|
-
const cookie_value = request.cookies.get(cookie_name)?.value;
|
|
71
|
-
return cookie_value;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
38
|
/**
|
|
75
39
|
* Builds TenantOrganization from scope details
|
|
76
40
|
* @param scope_details - Full scope details from cache
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// file_description: shared request-level active-scope extraction (header/cookie).
|
|
2
|
+
// Single source of truth for hazo_get_auth (scope-aware permission resolution) and
|
|
3
|
+
// hazo_get_tenant_auth (scope access + organization resolution) — both need the exact
|
|
4
|
+
// same header name, cookie name, and extraction priority (header > cookie), and must
|
|
5
|
+
// never drift from each other.
|
|
6
|
+
import { NextRequest } from "next/server";
|
|
7
|
+
import { get_cookie_name } from "../cookies_config.server.js";
|
|
8
|
+
|
|
9
|
+
// section: constants
|
|
10
|
+
|
|
11
|
+
/** Default header name for the active scope ID. */
|
|
12
|
+
export const DEFAULT_SCOPE_HEADER = "X-Hazo-Scope-Id";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Base cookie name for scope ID (will have the configured prefix applied).
|
|
16
|
+
*
|
|
17
|
+
* Exported so a consuming app can SET the same cookie this module READS — without it,
|
|
18
|
+
* an app implementing a scope switcher has to duplicate the configured prefix and drift
|
|
19
|
+
* the moment it changes.
|
|
20
|
+
*/
|
|
21
|
+
export const SCOPE_COOKIE_BASE_NAME = "hazo_auth_scope_id";
|
|
22
|
+
|
|
23
|
+
/** The live, prefixed scope cookie name for this deployment. */
|
|
24
|
+
export function get_scope_cookie_name(): string {
|
|
25
|
+
return get_cookie_name(SCOPE_COOKIE_BASE_NAME);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// section: types
|
|
29
|
+
|
|
30
|
+
export type ScopeHeaderOverrides = {
|
|
31
|
+
/** Header name to check for scope ID (default: "X-Hazo-Scope-Id") */
|
|
32
|
+
scope_header_name?: string;
|
|
33
|
+
/** Cookie name to check for scope ID (uses cookie prefix if not specified) */
|
|
34
|
+
scope_cookie_name?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// section: helpers
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Extracts the active scope ID from request headers or cookies.
|
|
41
|
+
* Priority: header > cookie.
|
|
42
|
+
* @param request - NextRequest object
|
|
43
|
+
* @param overrides - optional header/cookie name overrides
|
|
44
|
+
* @returns Scope ID if found, undefined otherwise
|
|
45
|
+
*/
|
|
46
|
+
export function extract_scope_id_from_request(
|
|
47
|
+
request: NextRequest,
|
|
48
|
+
overrides: ScopeHeaderOverrides = {},
|
|
49
|
+
): string | undefined {
|
|
50
|
+
const header_name = overrides.scope_header_name || DEFAULT_SCOPE_HEADER;
|
|
51
|
+
const header_value = request.headers.get(header_name);
|
|
52
|
+
if (header_value) {
|
|
53
|
+
return header_value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const cookie_name = overrides.scope_cookie_name || get_scope_cookie_name();
|
|
57
|
+
return request.cookies.get(cookie_name)?.value;
|
|
58
|
+
}
|
|
@@ -18,6 +18,8 @@ export type EmailVerificationConfig = {
|
|
|
18
18
|
showReturnHomeButton: boolean;
|
|
19
19
|
returnHomeButtonLabel: string | undefined;
|
|
20
20
|
returnHomePath: string;
|
|
21
|
+
/** See `AlreadyLoggedInConfig.redirectPath` — undefined unless `[hazo_auth__already_logged_in] redirect_path` is set. */
|
|
22
|
+
alreadyLoggedInRedirectPath: string | undefined;
|
|
21
23
|
imageSrc: string;
|
|
22
24
|
imageAlt: string;
|
|
23
25
|
imageBackgroundColor: string;
|
|
@@ -61,6 +63,7 @@ export function get_email_verification_config(): EmailVerificationConfig {
|
|
|
61
63
|
showReturnHomeButton: alreadyLoggedInConfig.showReturnHomeButton,
|
|
62
64
|
returnHomeButtonLabel: alreadyLoggedInConfig.returnHomeButtonLabel,
|
|
63
65
|
returnHomePath: alreadyLoggedInConfig.returnHomePath,
|
|
66
|
+
alreadyLoggedInRedirectPath: alreadyLoggedInConfig.redirectPath,
|
|
64
67
|
imageSrc,
|
|
65
68
|
imageAlt,
|
|
66
69
|
imageBackgroundColor,
|
|
@@ -18,6 +18,8 @@ export type ForgotPasswordConfig = {
|
|
|
18
18
|
showReturnHomeButton: boolean;
|
|
19
19
|
returnHomeButtonLabel: string | undefined;
|
|
20
20
|
returnHomePath: string;
|
|
21
|
+
/** See `AlreadyLoggedInConfig.redirectPath` — undefined unless `[hazo_auth__already_logged_in] redirect_path` is set. */
|
|
22
|
+
alreadyLoggedInRedirectPath: string | undefined;
|
|
21
23
|
imageSrc: string;
|
|
22
24
|
imageAlt: string;
|
|
23
25
|
imageBackgroundColor: string;
|
|
@@ -61,6 +63,7 @@ export function get_forgot_password_config(): ForgotPasswordConfig {
|
|
|
61
63
|
showReturnHomeButton: alreadyLoggedInConfig.showReturnHomeButton,
|
|
62
64
|
returnHomeButtonLabel: alreadyLoggedInConfig.returnHomeButtonLabel,
|
|
63
65
|
returnHomePath: alreadyLoggedInConfig.returnHomePath,
|
|
66
|
+
alreadyLoggedInRedirectPath: alreadyLoggedInConfig.redirectPath,
|
|
64
67
|
imageSrc,
|
|
65
68
|
imageAlt,
|
|
66
69
|
imageBackgroundColor,
|
|
@@ -36,6 +36,8 @@ export type RegisterConfig = {
|
|
|
36
36
|
showReturnHomeButton: boolean;
|
|
37
37
|
returnHomeButtonLabel: string | undefined;
|
|
38
38
|
returnHomePath: string;
|
|
39
|
+
/** See `AlreadyLoggedInConfig.redirectPath` — undefined unless `[hazo_auth__already_logged_in] redirect_path` is set. */
|
|
40
|
+
alreadyLoggedInRedirectPath: string | undefined;
|
|
39
41
|
signInPath: string;
|
|
40
42
|
/** Undefined when not configured — RegisterLayout falls back to `t("shared.sign_in_default")`. */
|
|
41
43
|
signInLabel: string | undefined;
|
|
@@ -136,6 +138,7 @@ export function get_register_config(): RegisterConfig {
|
|
|
136
138
|
showReturnHomeButton: alreadyLoggedInConfig.showReturnHomeButton,
|
|
137
139
|
returnHomeButtonLabel: alreadyLoggedInConfig.returnHomeButtonLabel,
|
|
138
140
|
returnHomePath: alreadyLoggedInConfig.returnHomePath,
|
|
141
|
+
alreadyLoggedInRedirectPath: alreadyLoggedInConfig.redirectPath,
|
|
139
142
|
signInPath,
|
|
140
143
|
signInLabel,
|
|
141
144
|
imageSrc,
|
|
@@ -25,6 +25,8 @@ export type ResetPasswordConfig = {
|
|
|
25
25
|
showReturnHomeButton: boolean;
|
|
26
26
|
returnHomeButtonLabel: string | undefined;
|
|
27
27
|
returnHomePath: string;
|
|
28
|
+
/** See `AlreadyLoggedInConfig.redirectPath` — undefined unless `[hazo_auth__already_logged_in] redirect_path` is set. */
|
|
29
|
+
alreadyLoggedInRedirectPath: string | undefined;
|
|
28
30
|
passwordRequirements: {
|
|
29
31
|
minimum_length: number;
|
|
30
32
|
require_uppercase: boolean;
|
|
@@ -94,6 +96,7 @@ export function get_reset_password_config(): ResetPasswordConfig {
|
|
|
94
96
|
showReturnHomeButton: alreadyLoggedInConfig.showReturnHomeButton,
|
|
95
97
|
returnHomeButtonLabel: alreadyLoggedInConfig.returnHomeButtonLabel,
|
|
96
98
|
returnHomePath: alreadyLoggedInConfig.returnHomePath,
|
|
99
|
+
alreadyLoggedInRedirectPath: alreadyLoggedInConfig.redirectPath,
|
|
97
100
|
passwordRequirements,
|
|
98
101
|
imageSrc,
|
|
99
102
|
imageAlt,
|