Commit c4532ba
authored
feat(auth): link OAuth signin to existing user by verified email (#3497)
* feat(auth): link OAuth signin to existing user by verified email (#3493)
- checkOAuthUserProfile lookup order: (provider, sub) → additional
ProvidersData[provider][key] → email + provider-verified → create
- Store linked providers under additionalProvidersData, keep
user.provider intact (password reset + local login still work)
- Require emailVerifiedByProvider=true before linking (prevents
takeover via unverified OIDC claims)
- Set emailVerified=true on fresh OAuth create when provider
vouches for the email (closes #3494)
- Pass email_verified through google + apple strategies
* fix(auth): fix layer violation, provider allowlist, token data leak, test accuracy
- Remove direct UserRepository import from auth controller (was layer violation)
- Replace UserRepository.update with UserService.update(brutUser, patch, 'recover') in step 3
- Add ALLOWED_PROVIDERS/ALLOWED_PROVIDER_KEYS allowlists before dynamic key construction
- Add sanitizeAdditionalProvidersData helper to strip accessToken/refreshToken from token endpoint
- Add additionalProvidersData to recover whitelist so service update can persist it
- Fix credentials.password → credentials[0].password (was array, not object)
- Fix unverified-email test to use same email and assert local account untouched
- Strengthen takeover test: assert specific error code + verify local account not modified
- Remove dead UserRepository mocks from unit tests (controller no longer imports it)
* fix(auth): fix takeover test error code assertion (CONTROLLER_ERROR not SERVICE_ERROR)
* fix(auth): atomic provider linking + JSDoc @returns on modified functions
- Replace search+update sequence with atomic findOneAndUpdate via UserRepository.linkProviderByEmail
to eliminate TOCTOU race between concurrent OAuth callbacks (CodeRabbit race condition finding)
- Expose linkProviderByEmail on UserService with removeSensitive wrapping
- Controller step 3 now calls UserService.linkProviderByEmail (single round-trip, no race window)
- Add @returns JSDoc to checkOAuthUserProfile, apple.prepare, google.prepare
* test(auth): add coverage for provider/key allowlist validation and token sanitization
* docs(migrations): document OAuth account linking + Express 5 callback fix1 parent 35f28ff commit c4532ba
9 files changed
Lines changed: 337 additions & 18 deletions
File tree
- modules
- auth
- controllers
- strategies/local
- tests
- users
- config
- models
- repositories
- services
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
7 | 53 | | |
8 | 54 | | |
9 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
237 | 256 | | |
238 | 257 | | |
239 | 258 | | |
| |||
248 | 267 | | |
249 | 268 | | |
250 | 269 | | |
251 | | - | |
| 270 | + | |
252 | 271 | | |
253 | 272 | | |
254 | 273 | | |
| |||
295 | 314 | | |
296 | 315 | | |
297 | 316 | | |
298 | | - | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
299 | 329 | | |
300 | | - | |
301 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
302 | 333 | | |
303 | 334 | | |
304 | | - | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
305 | 343 | | |
306 | 344 | | |
307 | 345 | | |
| |||
311 | 349 | | |
312 | 350 | | |
313 | 351 | | |
314 | | - | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
315 | 374 | | |
316 | 375 | | |
317 | 376 | | |
| |||
320 | 379 | | |
321 | 380 | | |
322 | 381 | | |
| 382 | + | |
323 | 383 | | |
324 | 384 | | |
325 | 385 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| 40 | + | |
38 | 41 | | |
39 | 42 | | |
40 | 43 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
0 commit comments