canopycms-auth-dev 0.0.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/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ export {
2
+ createDevAuthPlugin,
3
+ DevAuthPlugin,
4
+ DEFAULT_USERS,
5
+ DEFAULT_GROUPS,
6
+ DEV_ADMIN_USER_ID,
7
+ } from './dev-plugin'
8
+ export type { DevAuthConfig, DevUser, DevGroup } from './dev-plugin'
9
+ export { createDevTokenVerifier } from './jwt-verifier'
10
+ export { refreshDevCache } from './cache-writer'
11
+ export type { RefreshDevCacheOptions } from './cache-writer'
@@ -0,0 +1,46 @@
1
+ import { extractHeaders } from 'canopycms/auth'
2
+ import type { TokenVerifier } from 'canopycms/auth'
3
+ import { getDevUserCookieFromHeaders, DEFAULT_USER_ID } from './cookie-utils'
4
+ import { DEV_ADMIN_USER_ID } from './dev-plugin'
5
+
6
+ /**
7
+ * Test user key → dev user ID mapping.
8
+ * Matches the mapping in DevAuthPlugin.mapTestUserKey().
9
+ */
10
+ const TEST_USER_MAP: Record<string, string> = {
11
+ admin: DEV_ADMIN_USER_ID,
12
+ editor: 'dev_user1_2nK8mP4xL9',
13
+ viewer: 'dev_user2_7qR3tY6wN2',
14
+ reviewer: 'dev_reviewer_9aB4cD2eF7',
15
+ }
16
+
17
+ /**
18
+ * Creates a token verifier for dev auth.
19
+ * Extracts userId from X-Test-User header, x-dev-user-id header,
20
+ * or canopy-dev-user cookie — same logic as DevAuthPlugin.authenticate().
21
+ *
22
+ * Used with CachingAuthPlugin in prod-sim mode to simulate the prod
23
+ * code path (token verification + cached metadata lookup) using dev users.
24
+ */
25
+ export function createDevTokenVerifier(options?: { defaultUserId?: string }): TokenVerifier {
26
+ const defaultUserId = options?.defaultUserId ?? DEFAULT_USER_ID
27
+
28
+ return async (context: unknown) => {
29
+ const headers = extractHeaders(context)
30
+ if (!headers) return null
31
+
32
+ // Same extraction logic as DevAuthPlugin.authenticate()
33
+ let userId = headers.get('X-Test-User')
34
+ if (!userId) {
35
+ userId = headers.get('x-dev-user-id') ?? getDevUserCookieFromHeaders(headers)
36
+ }
37
+ if (!userId) {
38
+ userId = defaultUserId
39
+ }
40
+
41
+ // Map test user keys to dev user IDs
42
+ const mapped = TEST_USER_MAP[userId] ?? userId
43
+
44
+ return { userId: mapped }
45
+ }
46
+ }