@reflagged/shell 1.2.0 → 1.3.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/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@reflagged/shell",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Shared app shell for Reflagged services — OIDC auth, SSO, app switcher",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "files": [
8
8
  "src",
9
+ "!src/**/*.test.ts",
9
10
  "tsconfig.json",
10
11
  "README.md"
11
12
  ],
@@ -29,28 +30,30 @@
29
30
  "./components/ServiceIcon": "./src/components/ServiceIcon.tsx"
30
31
  },
31
32
  "scripts": {
32
- "typecheck": "tsc --noEmit"
33
+ "typecheck": "tsc --noEmit",
34
+ "test": "vitest run"
33
35
  },
34
36
  "dependencies": {
35
37
  "jose": "^5.10.0",
36
38
  "oauth4webapi": "^3.6.0"
37
39
  },
38
40
  "peerDependencies": {
41
+ "lucide-react": ">=0.400.0",
39
42
  "next": "^15.4.0",
40
43
  "payload": "^3.79.0",
41
44
  "react": "^19.2.0",
42
- "react-dom": "^19.2.0",
43
- "lucide-react": ">=0.400.0"
45
+ "react-dom": "^19.2.0"
44
46
  },
45
47
  "devDependencies": {
46
48
  "@types/react": "^19.2.0",
47
49
  "@types/react-dom": "^19.2.0",
50
+ "lucide-react": "^0.500.0",
48
51
  "next": "^15.4.0",
49
52
  "payload": "^3.79.0",
50
53
  "react": "^19.2.0",
51
54
  "react-dom": "^19.2.0",
52
55
  "typescript": "^5.7.0",
53
- "lucide-react": "^0.500.0"
56
+ "vitest": "^3.2.0"
54
57
  },
55
58
  "peerDependenciesMeta": {
56
59
  "lucide-react": {
@@ -5,46 +5,112 @@ import { loadAppConfig } from '../../config'
5
5
  import { OIDC_COOKIE } from './oidc-config'
6
6
  import { verifySessionCookie } from './oidc-cookie'
7
7
 
8
- export const nextauthStrategy: AuthStrategy = {
9
- name: 'rflgd-oidc',
10
- authenticate: async ({ payload, headers }) => {
11
- const secret = process.env.AUTH_SECRET
12
- if (!secret) return { user: null }
13
-
14
- const cookieHeader = headers.get('cookie') ?? ''
15
- const cookieStart = `${OIDC_COOKIE}=`
16
- const segment = cookieHeader
17
- .split(';')
18
- .map((c) => c.trim())
19
- .find((c) => c.startsWith(cookieStart))
20
- if (!segment) return { user: null }
21
-
22
- const token = segment.slice(cookieStart.length)
23
- const session = await verifySessionCookie(secret, token)
24
- if (!session?.email) return { user: null }
25
-
26
- const existing = await payload.find({
27
- collection: 'users',
28
- where: { email: { equals: session.email } },
29
- limit: 1,
30
- overrideAccess: true,
31
- })
32
-
33
- let user = existing.docs[0]
34
- if (!user) {
35
- const config = loadAppConfig()
36
- const isFirst = (await payload.count({ collection: 'users', overrideAccess: true })).totalDocs === 0
37
- user = await payload.create({
8
+ /** What the platform knows about a person signing in for the first time. */
9
+ export type NewUserContext = {
10
+ /** Role in the current workspace ('admin' | 'member'), null without claims. */
11
+ orgRole: string | null
12
+ /** Role on the platform itself, null without claims. */
13
+ platformRole: string | null
14
+ email: string
15
+ /** True when this account is the first in the instance. */
16
+ isFirstUser: boolean
17
+ }
18
+
19
+ /** Maps the platform's view of a person onto one of this app's own roles. */
20
+ export type RoleForNewUser = (ctx: NewUserContext) => string
21
+
22
+ /**
23
+ * The role a newly created account receives.
24
+ *
25
+ * Without a callback this is exactly what it always was: the first account
26
+ * administers, everyone after it gets RFLGD_DEFAULT_USER_ROLE. With a
27
+ * callback the decision belongs entirely to the app, `isFirstUser` included —
28
+ * which is why it is in the context. An app that forgets that branch can end
29
+ * up with an instance that has no administrator.
30
+ *
31
+ * The return value is not checked. Which roles exist is the app's knowledge;
32
+ * an unknown value is rejected by Payload against the field configuration and
33
+ * the account is not created. That is the same path on which this package's
34
+ * own default 'user' once locked instances out.
35
+ */
36
+ export function resolveNewUserRole(
37
+ ctx: NewUserContext,
38
+ roleForNewUser?: RoleForNewUser,
39
+ ): string {
40
+ if (roleForNewUser) return roleForNewUser(ctx)
41
+ return ctx.isFirstUser ? 'admin' : loadAppConfig().defaultRole
42
+ }
43
+
44
+ /**
45
+ * Payload auth strategy for the platform's single sign-on.
46
+ *
47
+ * Creates an account on first sign-in when none exists for the address. The
48
+ * role it gets is `resolveNewUserRole`'s decision — see there.
49
+ */
50
+ export function createOidcStrategy(options?: {
51
+ roleForNewUser?: RoleForNewUser
52
+ }): AuthStrategy {
53
+ return {
54
+ name: 'rflgd-oidc',
55
+ authenticate: async ({ payload, headers }) => {
56
+ const secret = process.env.AUTH_SECRET
57
+ if (!secret) return { user: null }
58
+
59
+ const cookieHeader = headers.get('cookie') ?? ''
60
+ const cookieStart = `${OIDC_COOKIE}=`
61
+ const segment = cookieHeader
62
+ .split(';')
63
+ .map((c) => c.trim())
64
+ .find((c) => c.startsWith(cookieStart))
65
+ if (!segment) return { user: null }
66
+
67
+ const token = segment.slice(cookieStart.length)
68
+ const session = await verifySessionCookie(secret, token)
69
+ if (!session?.email) return { user: null }
70
+
71
+ const existing = await payload.find({
38
72
  collection: 'users',
39
- data: {
40
- email: session.email,
41
- password: randomBytes(32).toString('hex'),
42
- role: isFirst ? 'admin' : config.defaultRole,
43
- },
73
+ where: { email: { equals: session.email } },
74
+ limit: 1,
44
75
  overrideAccess: true,
45
76
  })
46
- }
47
77
 
48
- return { user: { ...user, collection: 'users' } }
49
- },
78
+ let user = existing.docs[0]
79
+ if (!user) {
80
+ const isFirstUser =
81
+ (await payload.count({ collection: 'users', overrideAccess: true })).totalDocs === 0
82
+ const role = resolveNewUserRole(
83
+ {
84
+ orgRole: session.orgRole ?? null,
85
+ platformRole: session.platformRole ?? null,
86
+ email: session.email,
87
+ isFirstUser,
88
+ },
89
+ options?.roleForNewUser,
90
+ )
91
+ user = await payload.create({
92
+ collection: 'users',
93
+ data: {
94
+ email: session.email,
95
+ password: randomBytes(32).toString('hex'),
96
+ // `role` comes from the environment or the app and is therefore a
97
+ // `string`. The host application narrows `users.role` to the
98
+ // options of its own collection — this package cannot know that
99
+ // type. The value is checked by Payload against the field
100
+ // configuration on write; an unknown one is rejected there.
101
+ role: role as never,
102
+ },
103
+ overrideAccess: true,
104
+ })
105
+ }
106
+
107
+ return { user: { ...user, collection: 'users' } }
108
+ },
109
+ }
50
110
  }
111
+
112
+ /**
113
+ * The strategy without options — the export every consumer used before this
114
+ * package learned about role translation. Kept so nothing has to change.
115
+ */
116
+ export const nextauthStrategy: AuthStrategy = createOidcStrategy()