wcz-layout 8.0.2 → 8.0.3
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.
Potentially problematic release.
This version of wcz-layout might be problematic. Click here for more details.
- package/dist/Approval-BSQHVGsm.js +133 -0
- package/dist/Approval-BSQHVGsm.js.map +1 -0
- package/dist/{DialogsContext-D5A8xncJ.js → DialogsContext-DkePghS6.js} +1 -1
- package/dist/{DialogsContext-D5A8xncJ.js.map → DialogsContext-DkePghS6.js.map} +1 -1
- package/dist/Email-D2SPExbn.js +20 -0
- package/dist/Email-D2SPExbn.js.map +1 -0
- package/dist/FileMeta--27aYkPB.js +19 -0
- package/dist/FileMeta--27aYkPB.js.map +1 -0
- package/dist/{FileMeta-fQnJ6uFX.d.ts → FileMeta-CQz1TMre.d.ts} +1 -1
- package/dist/{NotificationContext-DBTYc5z-.js → NotificationContext-FQRaNZRx.js} +1 -1
- package/dist/{NotificationContext-DBTYc5z-.js.map → NotificationContext-FQRaNZRx.js.map} +1 -1
- package/dist/{RouterListItemButton-rcdkgOXQ.js → RouterListItemButton-p9U4Z0SW.js} +1 -1
- package/dist/{RouterListItemButton-rcdkgOXQ.js.map → RouterListItemButton-p9U4Z0SW.js.map} +1 -1
- package/dist/{User-C21DoZOa.d.ts → User-B92WOpE2.d.ts} +1 -1
- package/dist/components.d.ts +12 -11
- package/dist/components.js +338 -299
- package/dist/components.js.map +1 -1
- package/dist/data/client.d.ts +3081 -0
- package/dist/data/client.js +188 -0
- package/dist/data/client.js.map +1 -0
- package/dist/data/server.d.ts +20 -0
- package/dist/data/server.js +22 -0
- package/dist/data/server.js.map +1 -0
- package/dist/env-szOeLEuD.js +31 -0
- package/dist/env-szOeLEuD.js.map +1 -0
- package/dist/file-B2VFf-iX.js +100 -0
- package/dist/file-B2VFf-iX.js.map +1 -0
- package/dist/file-DSohGVd1.js +149 -0
- package/dist/file-DSohGVd1.js.map +1 -0
- package/dist/hooks.d.ts +1 -1
- package/dist/hooks.js +3 -3
- package/dist/hooks.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +9 -8
- package/dist/index.js.map +1 -1
- package/dist/middleware.d.ts +1 -1
- package/dist/middleware.js +2 -1
- package/dist/middleware.js.map +1 -1
- package/dist/models.d.ts +35 -6
- package/dist/models.js +4 -118
- package/dist/models.js.map +1 -1
- package/dist/{msalClient-MNN6BwL6.d.ts → msalClient-DZjYT_QJ.d.ts} +2 -2
- package/dist/msalServer-DwpEEthz.js +47 -0
- package/dist/msalServer-DwpEEthz.js.map +1 -0
- package/dist/peoplesoft-COFvPG-L.js +170 -0
- package/dist/peoplesoft-COFvPG-L.js.map +1 -0
- package/dist/peoplesoft-CzeJCaYK.d.ts +1081 -0
- package/dist/{queryClient-CAJR3qjE.js → queryClient-1OJ5Va3i.js} +1 -1
- package/dist/{queryClient-CAJR3qjE.js.map → queryClient-1OJ5Va3i.js.map} +1 -1
- package/dist/{useDialogs-BaxhulU9.js → useDialogs-DBcNu-LV.js} +2 -2
- package/dist/{useDialogs-BaxhulU9.js.map → useDialogs-DBcNu-LV.js.map} +1 -1
- package/dist/{utils-BBQ5S6oM.d.ts → utils-BM8-Efdl.d.ts} +2 -2
- package/dist/{utils-CqQKSaSs.js → utils-CBa4vtgn.js} +8 -37
- package/dist/utils-CBa4vtgn.js.map +1 -0
- package/dist/utils.d.ts +2 -2
- package/dist/utils.js +2 -46
- package/dist/vite.js.map +1 -1
- package/package.json +21 -14
- package/skills/auth/SKILL.md +268 -268
- package/dist/FileMeta-D9HyhrGi.js +0 -42
- package/dist/FileMeta-D9HyhrGi.js.map +0 -1
- package/dist/queries-JPqCc5Tb.js +0 -551
- package/dist/queries-JPqCc5Tb.js.map +0 -1
- package/dist/query.d.ts +0 -3052
- package/dist/query.js +0 -3
- package/dist/utils-CqQKSaSs.js.map +0 -1
- package/dist/utils.js.map +0 -1
package/skills/auth/SKILL.md
CHANGED
|
@@ -1,268 +1,268 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: auth
|
|
3
|
-
description: >
|
|
4
|
-
MSAL/Entra ID authentication and AD-group authorization. Use
|
|
5
|
-
authorizationMiddleware(permissionKey) which includes authentication.
|
|
6
|
-
Configure permissions.ts and scopes.ts for type-safe keys via
|
|
7
|
-
virtual:wcz-layout. Client: getAccessToken(scopeKey). Server:
|
|
8
|
-
getAppToken(scopeKey), getTokenOnBehalfOf(userToken, scopeKey).
|
|
9
|
-
Route guards with requirePermission. Public routes via LayoutOptions.
|
|
10
|
-
serverFnAccessTokenMiddleware in start.ts. Activate when configuring
|
|
11
|
-
authentication, authorization, or token acquisition.
|
|
12
|
-
type: core
|
|
13
|
-
library: wcz-layout
|
|
14
|
-
library_version: "7.6.1"
|
|
15
|
-
sources:
|
|
16
|
-
- "wcz-layout:src/middleware/authMiddleware.ts"
|
|
17
|
-
- "wcz-layout:src/lib/auth/msalClient.ts"
|
|
18
|
-
- "wcz-layout:src/lib/auth/msalServer.ts"
|
|
19
|
-
- "wcz-layout:src/lib/auth/permissions.ts"
|
|
20
|
-
- "wcz-layout:src/lib/auth/scopes.ts"
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
# Authentication & Authorization
|
|
24
|
-
|
|
25
|
-
## Setup
|
|
26
|
-
|
|
27
|
-
Two configuration files define the app's security surface. Both are consumed by the `viteWczLayout()` plugin via `virtual:wcz-layout` to generate TypeScript types:
|
|
28
|
-
|
|
29
|
-
```typescript
|
|
30
|
-
// src/lib/auth/permissions.ts
|
|
31
|
-
export const permissions = {
|
|
32
|
-
admin: ["wcz-developers"],
|
|
33
|
-
all: ["wcz-all-employees", "wscz-all-employees"],
|
|
34
|
-
} as const;
|
|
35
|
-
// keyof typeof permissions → "admin" | "all"
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
// src/lib/auth/scopes.ts
|
|
40
|
-
export const scopes = {
|
|
41
|
-
graph: ["User.Read"],
|
|
42
|
-
api: ["api://wistron.com/your-app/access_as_user"],
|
|
43
|
-
} as const;
|
|
44
|
-
// keyof typeof scopes → "graph" | "api"
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Permission keys map to AD group names. A user has a permission if their JWT `groups` claim contains at least one group from the array.
|
|
48
|
-
|
|
49
|
-
## Core Patterns
|
|
50
|
-
|
|
51
|
-
### API route authorization
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
import { authorizationMiddleware } from "wcz-layout/middleware";
|
|
55
|
-
|
|
56
|
-
export const Route = createFileRoute("/api/todos")({
|
|
57
|
-
server: {
|
|
58
|
-
middleware: [authorizationMiddleware("all")],
|
|
59
|
-
handlers: createHandlers({
|
|
60
|
-
GET: async ({ context }) => {
|
|
61
|
-
// context.user is typed as User with hasPermission()
|
|
62
|
-
return Response.json(await context.db.select().from(todoTable));
|
|
63
|
-
},
|
|
64
|
-
}),
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
`authorizationMiddleware(permissionKey)`:
|
|
70
|
-
|
|
71
|
-
1. Verifies the JWT Bearer token (includes `authenticationMiddleware` internally)
|
|
72
|
-
2. Builds a `User` object with `hasPermission(key)` method
|
|
73
|
-
3. Returns 401 if token is invalid, 403 if user lacks the specified permission
|
|
74
|
-
4. Injects `context.user: User`
|
|
75
|
-
|
|
76
|
-
### Public (authentication-only) route
|
|
77
|
-
|
|
78
|
-
For routes that need authentication but no specific permission:
|
|
79
|
-
|
|
80
|
-
```typescript
|
|
81
|
-
import { authenticationMiddleware } from "wcz-layout/middleware";
|
|
82
|
-
|
|
83
|
-
export const Route = createFileRoute("/api/profile")({
|
|
84
|
-
server: {
|
|
85
|
-
middleware: [authenticationMiddleware()],
|
|
86
|
-
handlers: createHandlers({
|
|
87
|
-
GET: async ({ context }) => {
|
|
88
|
-
// context.user — any authenticated user
|
|
89
|
-
return Response.json(context.user);
|
|
90
|
-
},
|
|
91
|
-
}),
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
For optional authentication (public endpoints that may have user context):
|
|
97
|
-
|
|
98
|
-
```typescript
|
|
99
|
-
middleware: [authenticationMiddleware({ optional: true })],
|
|
100
|
-
// context.user is User | null
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### Route-level permission guard
|
|
104
|
-
|
|
105
|
-
```typescript
|
|
106
|
-
import { requirePermission } from "wcz-layout/utils";
|
|
107
|
-
|
|
108
|
-
export const Route = createFileRoute("/admin/")({
|
|
109
|
-
beforeLoad: requirePermission("admin"),
|
|
110
|
-
component: AdminPage,
|
|
111
|
-
});
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
`requirePermission` runs in `beforeLoad` and redirects unauthorized users.
|
|
115
|
-
|
|
116
|
-
### Client-side token acquisition
|
|
117
|
-
|
|
118
|
-
```typescript
|
|
119
|
-
import { getAccessToken } from "wcz-layout/utils";
|
|
120
|
-
|
|
121
|
-
// In an axios interceptor or client-side code
|
|
122
|
-
const token = await getAccessToken("api");
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
`getAccessToken` is a client-only function (uses MSAL `PublicClientApplication`). It acquires tokens silently and triggers interaction listeners on `InteractionRequiredAuthError`.
|
|
126
|
-
|
|
127
|
-
### Server-side token acquisition
|
|
128
|
-
|
|
129
|
-
```typescript
|
|
130
|
-
import { getAppToken, getTokenOnBehalfOf } from "wcz-layout/utils";
|
|
131
|
-
|
|
132
|
-
// App-only token (client credentials flow — no user context)
|
|
133
|
-
const appToken = await getAppToken("graph");
|
|
134
|
-
|
|
135
|
-
// On-behalf-of flow (exchange user token for downstream API token)
|
|
136
|
-
const oboToken = await getTokenOnBehalfOf(userBearerToken, "graph");
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
Both are server-only functions using MSAL `ConfidentialClientApplication`.
|
|
140
|
-
|
|
141
|
-
### Global server function middleware
|
|
142
|
-
|
|
143
|
-
```typescript
|
|
144
|
-
// src/start.ts
|
|
145
|
-
import { serverFnAccessTokenMiddleware } from "wcz-layout/middleware";
|
|
146
|
-
|
|
147
|
-
export const startInstance = createStart(() => ({
|
|
148
|
-
defaultSsr: false,
|
|
149
|
-
functionMiddleware: [serverFnAccessTokenMiddleware("api")],
|
|
150
|
-
}));
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
`serverFnAccessTokenMiddleware` attaches the Bearer token to all server function calls globally. Without it, server functions won't include authorization headers.
|
|
154
|
-
|
|
155
|
-
### Getting the current user
|
|
156
|
-
|
|
157
|
-
```typescript
|
|
158
|
-
import { getUser } from "wcz-layout/utils";
|
|
159
|
-
|
|
160
|
-
// Isomorphic — works on client (returns User | null) and server (returns null)
|
|
161
|
-
const user = getUser();
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
On the client, `getUser` reads the active MSAL account's ID token claims.
|
|
165
|
-
|
|
166
|
-
## Common Mistakes
|
|
167
|
-
|
|
168
|
-
### HIGH Chaining authenticationMiddleware before authorizationMiddleware
|
|
169
|
-
|
|
170
|
-
Wrong:
|
|
171
|
-
|
|
172
|
-
```typescript
|
|
173
|
-
middleware: [authenticationMiddleware(), authorizationMiddleware("admin")],
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
Correct:
|
|
177
|
-
|
|
178
|
-
```typescript
|
|
179
|
-
middleware: [authorizationMiddleware("admin")],
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
`authorizationMiddleware` already composes `authenticationMiddleware` internally. Chaining both duplicates JWT verification.
|
|
183
|
-
|
|
184
|
-
Source: maintainer interview
|
|
185
|
-
|
|
186
|
-
Cross-skill: See also skills/api-routes/SKILL.md § Common Mistakes
|
|
187
|
-
|
|
188
|
-
### CRITICAL Calling getAccessToken on the server
|
|
189
|
-
|
|
190
|
-
Wrong:
|
|
191
|
-
|
|
192
|
-
```typescript
|
|
193
|
-
// In a server handler or serverFn
|
|
194
|
-
const token = await getAccessToken("api");
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
Correct:
|
|
198
|
-
|
|
199
|
-
```typescript
|
|
200
|
-
// Server-side — use app token or OBO
|
|
201
|
-
const token = await getAppToken("api");
|
|
202
|
-
// or
|
|
203
|
-
const token = await getTokenOnBehalfOf(userToken, "api");
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
`getAccessToken` is `createClientOnlyFn` — it uses MSAL `PublicClientApplication` which only exists in the browser. Calling it on the server throws.
|
|
207
|
-
|
|
208
|
-
Source: wcz-layout:src/lib/auth/msalClient.ts
|
|
209
|
-
|
|
210
|
-
### HIGH Forgetting serverFnAccessTokenMiddleware in start.ts
|
|
211
|
-
|
|
212
|
-
Wrong:
|
|
213
|
-
|
|
214
|
-
```typescript
|
|
215
|
-
// src/start.ts
|
|
216
|
-
export const startInstance = createStart(() => ({
|
|
217
|
-
defaultSsr: false,
|
|
218
|
-
// Missing functionMiddleware
|
|
219
|
-
}));
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
Correct:
|
|
223
|
-
|
|
224
|
-
```typescript
|
|
225
|
-
// src/start.ts
|
|
226
|
-
export const startInstance = createStart(() => ({
|
|
227
|
-
defaultSsr: false,
|
|
228
|
-
functionMiddleware: [serverFnAccessTokenMiddleware("api")],
|
|
229
|
-
}));
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
Without `serverFnAccessTokenMiddleware`, server function calls won't include the Authorization header, causing 401 errors.
|
|
233
|
-
|
|
234
|
-
Source: wcz-layout:src/start.ts
|
|
235
|
-
|
|
236
|
-
### MEDIUM Using string literals for permission/scope keys
|
|
237
|
-
|
|
238
|
-
Wrong:
|
|
239
|
-
|
|
240
|
-
```typescript
|
|
241
|
-
authorizationMiddleware("superadmin"); // not in permissions.ts → compile error
|
|
242
|
-
getAccessToken("custom-api"); // not in scopes.ts → compile error
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
Correct:
|
|
246
|
-
|
|
247
|
-
```typescript
|
|
248
|
-
// First add to permissions.ts / scopes.ts, then use the typed key
|
|
249
|
-
authorizationMiddleware("admin");
|
|
250
|
-
getAccessToken("api");
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
Permission and scope keys are typed from the `as const` exports via `virtual:wcz-layout`. Using arbitrary strings causes TypeScript compile errors.
|
|
254
|
-
|
|
255
|
-
Source: wcz-layout:src/types/wcz-layout.d.ts
|
|
256
|
-
|
|
257
|
-
### HIGH Missing Entra redirect URI configuration
|
|
258
|
-
|
|
259
|
-
The MSAL `redirectUri` defaults to `"/"`. The Entra app registration must include this exact redirect URI, or the authentication flow fails silently with a redirect error.
|
|
260
|
-
|
|
261
|
-
Source: wcz-layout:src/routes/runbook/entra-id.tsx
|
|
262
|
-
|
|
263
|
-
---
|
|
264
|
-
|
|
265
|
-
See also:
|
|
266
|
-
|
|
267
|
-
- skills/api-routes/SKILL.md — Every API route uses authorizationMiddleware.
|
|
268
|
-
- skills/layout-navigation/SKILL.md — publicRoutes bypass auth; permission guards need auth context.
|
|
1
|
+
---
|
|
2
|
+
name: auth
|
|
3
|
+
description: >
|
|
4
|
+
MSAL/Entra ID authentication and AD-group authorization. Use
|
|
5
|
+
authorizationMiddleware(permissionKey) which includes authentication.
|
|
6
|
+
Configure permissions.ts and scopes.ts for type-safe keys via
|
|
7
|
+
virtual:wcz-layout. Client: getAccessToken(scopeKey). Server:
|
|
8
|
+
getAppToken(scopeKey), getTokenOnBehalfOf(userToken, scopeKey).
|
|
9
|
+
Route guards with requirePermission. Public routes via LayoutOptions.
|
|
10
|
+
serverFnAccessTokenMiddleware in start.ts. Activate when configuring
|
|
11
|
+
authentication, authorization, or token acquisition.
|
|
12
|
+
type: core
|
|
13
|
+
library: wcz-layout
|
|
14
|
+
library_version: "7.6.1"
|
|
15
|
+
sources:
|
|
16
|
+
- "wcz-layout:src/middleware/authMiddleware.ts"
|
|
17
|
+
- "wcz-layout:src/lib/auth/msalClient.ts"
|
|
18
|
+
- "wcz-layout:src/lib/auth/msalServer.ts"
|
|
19
|
+
- "wcz-layout:src/lib/auth/permissions.ts"
|
|
20
|
+
- "wcz-layout:src/lib/auth/scopes.ts"
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
# Authentication & Authorization
|
|
24
|
+
|
|
25
|
+
## Setup
|
|
26
|
+
|
|
27
|
+
Two configuration files define the app's security surface. Both are consumed by the `viteWczLayout()` plugin via `virtual:wcz-layout` to generate TypeScript types:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// src/lib/auth/permissions.ts
|
|
31
|
+
export const permissions = {
|
|
32
|
+
admin: ["wcz-developers"],
|
|
33
|
+
all: ["wcz-all-employees", "wscz-all-employees"],
|
|
34
|
+
} as const;
|
|
35
|
+
// keyof typeof permissions → "admin" | "all"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
// src/lib/auth/scopes.ts
|
|
40
|
+
export const scopes = {
|
|
41
|
+
graph: ["User.Read"],
|
|
42
|
+
api: ["api://wistron.com/your-app/access_as_user"],
|
|
43
|
+
} as const;
|
|
44
|
+
// keyof typeof scopes → "graph" | "api"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Permission keys map to AD group names. A user has a permission if their JWT `groups` claim contains at least one group from the array.
|
|
48
|
+
|
|
49
|
+
## Core Patterns
|
|
50
|
+
|
|
51
|
+
### API route authorization
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { authorizationMiddleware } from "wcz-layout/middleware";
|
|
55
|
+
|
|
56
|
+
export const Route = createFileRoute("/api/todos")({
|
|
57
|
+
server: {
|
|
58
|
+
middleware: [authorizationMiddleware("all")],
|
|
59
|
+
handlers: createHandlers({
|
|
60
|
+
GET: async ({ context }) => {
|
|
61
|
+
// context.user is typed as User with hasPermission()
|
|
62
|
+
return Response.json(await context.db.select().from(todoTable));
|
|
63
|
+
},
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`authorizationMiddleware(permissionKey)`:
|
|
70
|
+
|
|
71
|
+
1. Verifies the JWT Bearer token (includes `authenticationMiddleware` internally)
|
|
72
|
+
2. Builds a `User` object with `hasPermission(key)` method
|
|
73
|
+
3. Returns 401 if token is invalid, 403 if user lacks the specified permission
|
|
74
|
+
4. Injects `context.user: User`
|
|
75
|
+
|
|
76
|
+
### Public (authentication-only) route
|
|
77
|
+
|
|
78
|
+
For routes that need authentication but no specific permission:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { authenticationMiddleware } from "wcz-layout/middleware";
|
|
82
|
+
|
|
83
|
+
export const Route = createFileRoute("/api/profile")({
|
|
84
|
+
server: {
|
|
85
|
+
middleware: [authenticationMiddleware()],
|
|
86
|
+
handlers: createHandlers({
|
|
87
|
+
GET: async ({ context }) => {
|
|
88
|
+
// context.user — any authenticated user
|
|
89
|
+
return Response.json(context.user);
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
For optional authentication (public endpoints that may have user context):
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
middleware: [authenticationMiddleware({ optional: true })],
|
|
100
|
+
// context.user is User | null
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Route-level permission guard
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { requirePermission } from "wcz-layout/utils";
|
|
107
|
+
|
|
108
|
+
export const Route = createFileRoute("/admin/")({
|
|
109
|
+
beforeLoad: requirePermission("admin"),
|
|
110
|
+
component: AdminPage,
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`requirePermission` runs in `beforeLoad` and redirects unauthorized users.
|
|
115
|
+
|
|
116
|
+
### Client-side token acquisition
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { getAccessToken } from "wcz-layout/utils";
|
|
120
|
+
|
|
121
|
+
// In an axios interceptor or client-side code
|
|
122
|
+
const token = await getAccessToken("api");
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`getAccessToken` is a client-only function (uses MSAL `PublicClientApplication`). It acquires tokens silently and triggers interaction listeners on `InteractionRequiredAuthError`.
|
|
126
|
+
|
|
127
|
+
### Server-side token acquisition
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { getAppToken, getTokenOnBehalfOf } from "wcz-layout/utils";
|
|
131
|
+
|
|
132
|
+
// App-only token (client credentials flow — no user context)
|
|
133
|
+
const appToken = await getAppToken("graph");
|
|
134
|
+
|
|
135
|
+
// On-behalf-of flow (exchange user token for downstream API token)
|
|
136
|
+
const oboToken = await getTokenOnBehalfOf(userBearerToken, "graph");
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Both are server-only functions using MSAL `ConfidentialClientApplication`.
|
|
140
|
+
|
|
141
|
+
### Global server function middleware
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
// src/start.ts
|
|
145
|
+
import { serverFnAccessTokenMiddleware } from "wcz-layout/middleware";
|
|
146
|
+
|
|
147
|
+
export const startInstance = createStart(() => ({
|
|
148
|
+
defaultSsr: false,
|
|
149
|
+
functionMiddleware: [serverFnAccessTokenMiddleware("api")],
|
|
150
|
+
}));
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`serverFnAccessTokenMiddleware` attaches the Bearer token to all server function calls globally. Without it, server functions won't include authorization headers.
|
|
154
|
+
|
|
155
|
+
### Getting the current user
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { getUser } from "wcz-layout/utils";
|
|
159
|
+
|
|
160
|
+
// Isomorphic — works on client (returns User | null) and server (returns null)
|
|
161
|
+
const user = getUser();
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
On the client, `getUser` reads the active MSAL account's ID token claims.
|
|
165
|
+
|
|
166
|
+
## Common Mistakes
|
|
167
|
+
|
|
168
|
+
### HIGH Chaining authenticationMiddleware before authorizationMiddleware
|
|
169
|
+
|
|
170
|
+
Wrong:
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
middleware: [authenticationMiddleware(), authorizationMiddleware("admin")],
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Correct:
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
middleware: [authorizationMiddleware("admin")],
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
`authorizationMiddleware` already composes `authenticationMiddleware` internally. Chaining both duplicates JWT verification.
|
|
183
|
+
|
|
184
|
+
Source: maintainer interview
|
|
185
|
+
|
|
186
|
+
Cross-skill: See also skills/api-routes/SKILL.md § Common Mistakes
|
|
187
|
+
|
|
188
|
+
### CRITICAL Calling getAccessToken on the server
|
|
189
|
+
|
|
190
|
+
Wrong:
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
// In a server handler or serverFn
|
|
194
|
+
const token = await getAccessToken("api");
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Correct:
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
// Server-side — use app token or OBO
|
|
201
|
+
const token = await getAppToken("api");
|
|
202
|
+
// or
|
|
203
|
+
const token = await getTokenOnBehalfOf(userToken, "api");
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`getAccessToken` is `createClientOnlyFn` — it uses MSAL `PublicClientApplication` which only exists in the browser. Calling it on the server throws.
|
|
207
|
+
|
|
208
|
+
Source: wcz-layout:src/lib/auth/msalClient.ts
|
|
209
|
+
|
|
210
|
+
### HIGH Forgetting serverFnAccessTokenMiddleware in start.ts
|
|
211
|
+
|
|
212
|
+
Wrong:
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
// src/start.ts
|
|
216
|
+
export const startInstance = createStart(() => ({
|
|
217
|
+
defaultSsr: false,
|
|
218
|
+
// Missing functionMiddleware
|
|
219
|
+
}));
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Correct:
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
// src/start.ts
|
|
226
|
+
export const startInstance = createStart(() => ({
|
|
227
|
+
defaultSsr: false,
|
|
228
|
+
functionMiddleware: [serverFnAccessTokenMiddleware("api")],
|
|
229
|
+
}));
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Without `serverFnAccessTokenMiddleware`, server function calls won't include the Authorization header, causing 401 errors.
|
|
233
|
+
|
|
234
|
+
Source: wcz-layout:src/start.ts
|
|
235
|
+
|
|
236
|
+
### MEDIUM Using string literals for permission/scope keys
|
|
237
|
+
|
|
238
|
+
Wrong:
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
authorizationMiddleware("superadmin"); // not in permissions.ts → compile error
|
|
242
|
+
getAccessToken("custom-api"); // not in scopes.ts → compile error
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Correct:
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
// First add to permissions.ts / scopes.ts, then use the typed key
|
|
249
|
+
authorizationMiddleware("admin");
|
|
250
|
+
getAccessToken("api");
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Permission and scope keys are typed from the `as const` exports via `virtual:wcz-layout`. Using arbitrary strings causes TypeScript compile errors.
|
|
254
|
+
|
|
255
|
+
Source: wcz-layout:src/types/wcz-layout.d.ts
|
|
256
|
+
|
|
257
|
+
### HIGH Missing Entra redirect URI configuration
|
|
258
|
+
|
|
259
|
+
The MSAL `redirectUri` defaults to `"/"`. The Entra app registration must include this exact redirect URI, or the authentication flow fails silently with a redirect error.
|
|
260
|
+
|
|
261
|
+
Source: wcz-layout:src/routes/runbook/entra-id.tsx
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
See also:
|
|
266
|
+
|
|
267
|
+
- skills/api-routes/SKILL.md — Every API route uses authorizationMiddleware.
|
|
268
|
+
- skills/layout-navigation/SKILL.md — publicRoutes bypass auth; permission guards need auth context.
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import * as z$2 from "zod";
|
|
2
|
-
import z$1 from "zod";
|
|
3
|
-
//#region src/models/approval/ApprovalStatus.ts
|
|
4
|
-
const ApprovalStatus = z$1.enum([
|
|
5
|
-
"WaitingForApproval",
|
|
6
|
-
"Approved",
|
|
7
|
-
"PartiallyApproved",
|
|
8
|
-
"Rejected",
|
|
9
|
-
"Withdrawn",
|
|
10
|
-
"Cancelled"
|
|
11
|
-
]);
|
|
12
|
-
//#endregion
|
|
13
|
-
//#region src/models/approval/ApprovalStepResult.ts
|
|
14
|
-
const ApprovalStepResult = z$1.enum([
|
|
15
|
-
"NotAvailable",
|
|
16
|
-
"FutureApproval",
|
|
17
|
-
"WaitingForApproval",
|
|
18
|
-
"Approved",
|
|
19
|
-
"Rejected",
|
|
20
|
-
"Skipped",
|
|
21
|
-
"Withdrawn",
|
|
22
|
-
"Cancelled"
|
|
23
|
-
]);
|
|
24
|
-
//#endregion
|
|
25
|
-
//#region src/models/file/FileMeta.ts
|
|
26
|
-
const FileMetaSchema = z$2.object({
|
|
27
|
-
id: z$2.uuid(),
|
|
28
|
-
subId: z$2.uuid(),
|
|
29
|
-
appName: z$2.string().trim().min(1).max(255),
|
|
30
|
-
fileName: z$2.string().trim().min(1).max(255),
|
|
31
|
-
fileExtension: z$2.string().trim().min(1).max(255),
|
|
32
|
-
fileSize: z$2.number().min(0),
|
|
33
|
-
mediaSubType: z$2.string().trim().min(1).max(255),
|
|
34
|
-
mediaType: z$2.string().trim().min(1).max(255),
|
|
35
|
-
mimeType: z$2.string().trim().min(1).max(255),
|
|
36
|
-
createdBy: z$2.string().trim().min(1).max(255),
|
|
37
|
-
createdDate: z$2.date()
|
|
38
|
-
});
|
|
39
|
-
//#endregion
|
|
40
|
-
export { ApprovalStepResult as n, ApprovalStatus as r, FileMetaSchema as t };
|
|
41
|
-
|
|
42
|
-
//# sourceMappingURL=FileMeta-D9HyhrGi.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FileMeta-D9HyhrGi.js","names":["z","ApprovalStatus","enum","z","ApprovalStepResult","enum","z","FileMetaSchema","object","id","uuid","subId","appName","string","trim","min","max","fileName","fileExtension","fileSize","number","mediaSubType","mediaType","mimeType","createdBy","createdDate","date","FileMeta","infer"],"sources":["../src/models/approval/ApprovalStatus.ts","../src/models/approval/ApprovalStepResult.ts","../src/models/file/FileMeta.ts"],"sourcesContent":["import z from \"zod\";\n\nexport const ApprovalStatus = z.enum([\n \"WaitingForApproval\",\n \"Approved\",\n \"PartiallyApproved\",\n \"Rejected\",\n \"Withdrawn\",\n \"Cancelled\",\n]);\n","import z from \"zod\";\n\nexport const ApprovalStepResult = z.enum([\n \"NotAvailable\",\n \"FutureApproval\",\n \"WaitingForApproval\",\n \"Approved\",\n \"Rejected\",\n \"Skipped\",\n \"Withdrawn\",\n \"Cancelled\",\n]);\n","import * as z from \"zod\";\n\nexport const FileMetaSchema = z.object({\n id: z.uuid(),\n subId: z.uuid(),\n appName: z.string().trim().min(1).max(255),\n fileName: z.string().trim().min(1).max(255),\n fileExtension: z.string().trim().min(1).max(255),\n fileSize: z.number().min(0),\n mediaSubType: z.string().trim().min(1).max(255),\n mediaType: z.string().trim().min(1).max(255),\n mimeType: z.string().trim().min(1).max(255),\n createdBy: z.string().trim().min(1).max(255),\n createdDate: z.date(),\n});\n\nexport type FileMeta = z.infer<typeof FileMetaSchema>;\n"],"mappings":";;;AAEA,MAAaC,iBAAiBD,IAAEE,KAAK;CACnC;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;ACPF,MAAaE,qBAAqBD,IAAEE,KAAK;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;;;ACTF,MAAaE,iBAAiBD,IAAEE,OAAO;CACrCC,IAAIH,IAAEI,MAAM;CACZC,OAAOL,IAAEI,MAAM;CACfE,SAASN,IAAEO,QAAQ,CAACC,MAAM,CAACC,IAAI,EAAE,CAACC,IAAI,IAAI;CAC1CC,UAAUX,IAAEO,QAAQ,CAACC,MAAM,CAACC,IAAI,EAAE,CAACC,IAAI,IAAI;CAC3CE,eAAeZ,IAAEO,QAAQ,CAACC,MAAM,CAACC,IAAI,EAAE,CAACC,IAAI,IAAI;CAChDG,UAAUb,IAAEc,QAAQ,CAACL,IAAI,EAAE;CAC3BM,cAAcf,IAAEO,QAAQ,CAACC,MAAM,CAACC,IAAI,EAAE,CAACC,IAAI,IAAI;CAC/CM,WAAWhB,IAAEO,QAAQ,CAACC,MAAM,CAACC,IAAI,EAAE,CAACC,IAAI,IAAI;CAC5CO,UAAUjB,IAAEO,QAAQ,CAACC,MAAM,CAACC,IAAI,EAAE,CAACC,IAAI,IAAI;CAC3CQ,WAAWlB,IAAEO,QAAQ,CAACC,MAAM,CAACC,IAAI,EAAE,CAACC,IAAI,IAAI;CAC5CS,aAAanB,IAAEoB,MAAK;CACrB,CAAC"}
|