Commit 36a9cec
committed
msauth: accept IMicrosoftAccount on GetTokenForUserAsync
`IMicrosoftAuthentication.GetTokenForUserAsync` predated the
`IMicrosoftAccount` shape that the rest of the auth surface now
uses for identifying cached accounts. It took a bare `string
userName`, matched by UPN against `app.GetAccountsAsync()`, and
quietly picked the first match. That works fine for single-tenant
single-account users but loses information in two scenarios:
- Guest accounts where the same UPN exists in two tenants
(e.g. `alice@contoso.com` as a guest in fabrikam). Both
accounts share a UPN but have distinct `HomeAccountId`
values; selecting by UPN alone non-deterministically picks one.
- Callers that already carry a stable `HomeAccountId` (e.g. an
upcoming binding manager rewrite) have to translate it back
to a UPN before calling the API, which both wastes the
stable identifier and reintroduces the ambiguity.
Pivot the interface to take an `IMicrosoftAccount`:
Task<IMicrosoftAuthenticationResult> GetTokenForUserAsync(
string authority, string clientId, Uri redirectUri,
string[] scopes, IMicrosoftAccount account, bool msaPt = false);
Internal resolution prefers `HomeAccountId` when present, falls
back to `UserName` otherwise, traces a warning when both are set
and the cached account's UPN differs from the supplied one
(HomeAccountId wins, supplied UPN is informational), and traces a
warning when UPN-only resolution returns multiple matches
(first-match returned, today's behaviour preserved). `null`
account remains "let MSAL pick interactively".
The silent-acquisition helper unifies behind a single
`GetAccessTokenSilentlyAsync(app, scopes, IAccount, msaPt)`:
callers with an explicit cached account pass it through, and the
broker's "default OS account" path resolves to
`PublicClientApplication.OperatingSystemAccount` (an `IAccount`
sentinel) and goes through the same path. The MSA-passthrough
tenant-id workaround switches to a null-safe lookup so it no-ops
cleanly when the sentinel's `HomeAccountId` isn't populated.
Add a new `MicrosoftAuthenticationExtensions` static class to
host an `[Obsolete]` extension that preserves the pre-existing
`(…, string userName, …)` signature. Existing in-tree production
call sites keep building with a deprecation warning as a TODO
list; the extension wraps by constructing
`new MicrosoftAccount(homeAccountId: null, userName)` — the
"UPN-only" shape — so the legacy semantics are preserved exactly.
A follow-on commit migrates the one remaining production caller
(Azure Repos), then a final commit deletes the extension.
`Mock<IMicrosoftAuthentication>` cannot set up an extension
method, so the eight `.Setup` expressions in
`AzureReposHostProviderTests` and the one direct call in
`MicrosoftAuthenticationTests` migrate to the new interface
method in this commit. Each test hoists a local
`var expectedAccount = new MicrosoftAccount(homeAccountId: null,
userName: …)` (or `IMicrosoftAccount expectedAccount = null` for
the unconstrained case) alongside its other expected-mock-inputs
and passes the value directly to `Setup`; the equality contract
introduced alongside `IMicrosoftAccount` itself is what lets Moq
match these by value rather than reference. Production code
stays on the obsolete extension for now.
Assisted-by: Claude Opus 4.7
Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>1 parent 95e3d15 commit 36a9cec
3 files changed
Lines changed: 79 additions & 51 deletions
File tree
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
Lines changed: 61 additions & 41 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
57 | 60 | | |
58 | 61 | | |
59 | 62 | | |
60 | | - | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
| |||
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
155 | 177 | | |
156 | 178 | | |
157 | 179 | | |
| |||
254 | 276 | | |
255 | 277 | | |
256 | 278 | | |
257 | | - | |
| 279 | + | |
258 | 280 | | |
259 | 281 | | |
260 | 282 | | |
| |||
276 | 298 | | |
277 | 299 | | |
278 | 300 | | |
279 | | - | |
280 | | - | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
281 | 317 | | |
282 | 318 | | |
283 | | - | |
| 319 | + | |
284 | 320 | | |
285 | 321 | | |
286 | 322 | | |
| |||
318 | 354 | | |
319 | 355 | | |
320 | 356 | | |
321 | | - | |
| 357 | + | |
322 | 358 | | |
323 | 359 | | |
324 | 360 | | |
| |||
604 | 640 | | |
605 | 641 | | |
606 | 642 | | |
607 | | - | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
608 | 646 | | |
609 | 647 | | |
610 | | - | |
| 648 | + | |
611 | 649 | | |
612 | 650 | | |
613 | 651 | | |
614 | | - | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
615 | 664 | | |
616 | | - | |
617 | | - | |
618 | | - | |
619 | | - | |
620 | | - | |
| 665 | + | |
621 | 666 | | |
622 | | - | |
623 | | - | |
624 | | - | |
625 | 667 | | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | | - | |
630 | | - | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
637 | | - | |
638 | | - | |
639 | | - | |
640 | | - | |
641 | | - | |
642 | | - | |
643 | | - | |
644 | | - | |
645 | | - | |
646 | | - | |
647 | | - | |
648 | | - | |
| 668 | + | |
649 | 669 | | |
650 | 670 | | |
651 | 671 | | |
| |||
Lines changed: 16 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| 161 | + | |
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
| |||
170 | 171 | | |
171 | 172 | | |
172 | 173 | | |
173 | | - | |
| 174 | + | |
174 | 175 | | |
175 | 176 | | |
176 | 177 | | |
| |||
208 | 209 | | |
209 | 210 | | |
210 | 211 | | |
| 212 | + | |
211 | 213 | | |
212 | 214 | | |
213 | 215 | | |
| |||
220 | 222 | | |
221 | 223 | | |
222 | 224 | | |
223 | | - | |
| 225 | + | |
224 | 226 | | |
225 | 227 | | |
226 | 228 | | |
| |||
254 | 256 | | |
255 | 257 | | |
256 | 258 | | |
| 259 | + | |
257 | 260 | | |
258 | 261 | | |
259 | 262 | | |
| |||
267 | 270 | | |
268 | 271 | | |
269 | 272 | | |
270 | | - | |
| 273 | + | |
271 | 274 | | |
272 | 275 | | |
273 | 276 | | |
| |||
303 | 306 | | |
304 | 307 | | |
305 | 308 | | |
| 309 | + | |
306 | 310 | | |
307 | 311 | | |
308 | 312 | | |
| |||
315 | 319 | | |
316 | 320 | | |
317 | 321 | | |
318 | | - | |
| 322 | + | |
319 | 323 | | |
320 | 324 | | |
321 | 325 | | |
| |||
353 | 357 | | |
354 | 358 | | |
355 | 359 | | |
| 360 | + | |
356 | 361 | | |
357 | 362 | | |
358 | 363 | | |
| |||
364 | 369 | | |
365 | 370 | | |
366 | 371 | | |
367 | | - | |
| 372 | + | |
368 | 373 | | |
369 | 374 | | |
370 | 375 | | |
| |||
401 | 406 | | |
402 | 407 | | |
403 | 408 | | |
| 409 | + | |
404 | 410 | | |
405 | 411 | | |
406 | 412 | | |
| |||
414 | 420 | | |
415 | 421 | | |
416 | 422 | | |
417 | | - | |
| 423 | + | |
418 | 424 | | |
419 | 425 | | |
420 | 426 | | |
| |||
450 | 456 | | |
451 | 457 | | |
452 | 458 | | |
| 459 | + | |
453 | 460 | | |
454 | 461 | | |
455 | 462 | | |
| |||
462 | 469 | | |
463 | 470 | | |
464 | 471 | | |
465 | | - | |
| 472 | + | |
466 | 473 | | |
467 | 474 | | |
468 | 475 | | |
| |||
496 | 503 | | |
497 | 504 | | |
498 | 505 | | |
| 506 | + | |
499 | 507 | | |
500 | 508 | | |
501 | 509 | | |
| |||
508 | 516 | | |
509 | 517 | | |
510 | 518 | | |
511 | | - | |
| 519 | + | |
512 | 520 | | |
513 | 521 | | |
514 | 522 | | |
| |||
0 commit comments