Skip to content

Add unit tests with HTTP mocking for vanilla dSTS + fix Authority par…#3805

Merged
XiaoxinMS2 merged 7 commits into
masterfrom
zhanli1/dsts-unit-tests-with-http-mocking
May 21, 2026
Merged

Add unit tests with HTTP mocking for vanilla dSTS + fix Authority par…#3805
XiaoxinMS2 merged 7 commits into
masterfrom
zhanli1/dsts-unit-tests-with-http-mocking

Conversation

@XiaoxinMS2
Copy link
Copy Markdown
Contributor

@XiaoxinMS2 XiaoxinMS2 commented May 5, 2026

…sing

Adds 6 HTTP-mocked unit tests for the vanilla dSTS (Dedicated Security Token Service) token-acquisition path in Microsoft.Identity.Web, and fixes MergedOptions.ParseAuthorityIfNecessary so that the natural / documented dSTS configuration form works end-to-end:

options.Authority = "https://{host}/dstsv2/{tenantGuid}";

Without the fix, the AAD-style parser took the literal "dstsv2" as the tenant and dropped the actual tenant GUID, producing an authority MSAL rejected with "The DSTS authority URI should have at least 2 segments...".

Tests cover: token endpoint URI; client_credentials grant body; second-call cache hit; OAuth2 error -> MsalServiceException mapping; SendX5C=true includes x5c JWT header; SendX5C=false omits it. All tests use the existing MockHttpClientFactory infrastructure (no real network / Key Vault / cert) and run in any CI environment.

No public API changes.

Add unit tests with HTTP mocking for vanilla dSTS scenarios + fix Authority-only configuration

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Summary of the changes (Less than 80 chars)

Description

Adds 6 HTTP-mocked unit tests covering the vanilla dSTS (Dedicated Security Token Service) token-acquisition path in Microsoft.Identity.Web, and fixes a parser bug in MergedOptions.ParseAuthorityIfNecessary that prevented the natural / documented dSTS configuration form (options.Authority = "https://{host}/dstsv2/{tenantGuid}") from working.

Both changes ship together because the new tests use the natural configuration form, which is the form the parser fix unblocks.

Vanilla dSTS support in Id.Web previously had zero unit-test coverage — the only assertion lived in integration tests that required a real dSTS deployment + Key Vault certificate, so they couldn't run in CI. Anyone refactoring MergedOptions / TokenAcquirerFactory could silently break dSTS token acquisition without any signal.

Fixes #{bug number} (in this specific format)

@XiaoxinMS2 XiaoxinMS2 requested a review from a team as a code owner May 5, 2026 14:57
Adds 7 HTTP-mocked unit tests for the vanilla dSTS (Dedicated Security Token Service) token-acquisition path in Microsoft.Identity.Web.

Per PR review feedback, dSTS users MUST configure 'Instance' (e.g. "https://{host}/dstsv2") and 'TenantId' separately. The single-string 'Authority' option is reserved for vanilla OIDC / CIAM scenarios and routes through MSAL.WithOidcAuthority(), which is incompatible with dSTS; configuring a dSTS-style URL there now throws an InvalidOperationException with a clear, actionable error message instead of letting MSAL surface its opaque "DSTS authority URI should have at least 2 segments..." error later.

Tests cover (canonical Instance + TenantId shape):

  1. Token endpoint URI lock (POST to https://{host}/dstsv2/{tenant}/oauth2/v2.0/token)

  2. Client_credentials grant body (grant_type, scope, client_id, client_secret)

  3. Second-call cache hit (only one mock handler registered)

  4. OAuth2 token-endpoint error -> MsalServiceException mapping

  5. SendX5C=true -> client_assertion JWT header includes x5c

  6. SendX5C=false -> x5c omitted

Plus 1 negative test:

  7. Configuring dSTS URL via 'Authority' option -> InvalidOperationException with clear guidance to use Instance + TenantId

All tests use the existing MockHttpClientFactory infrastructure (no real network / Key Vault / cert) and run in any CI environment.

Implementation change in MergedOptions.ParseAuthorityIfNecessary: detects dSTS-shaped Authority (path segment "dstsv2") and throws an InvalidOperationException with a message that points users to the canonical Instance+TenantId shape. No public API changes.
@XiaoxinMS2 XiaoxinMS2 force-pushed the zhanli1/dsts-unit-tests-with-http-mocking branch from eedbd53 to c625531 Compare May 5, 2026 21:54
Comment thread src/Microsoft.Identity.Web.TokenAcquisition/MergedOptions.cs Outdated
Copy link
Copy Markdown
Contributor

Copilot AI commented May 21, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • login.microsoftonline.com
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/microsoft-identity-web/microsoft-identity-web/tests/Microsoft.Identity.Web.Test/bin/Release/net8.0/Microsoft.Identity.Web.Test.runtimeconfig.json --depsfile /home/REDACTED/work/microsoft-identity-web/microsoft-identity-web/tests/Microsoft.Identity.Web.Test/bin/Release/net8.0/Microsoft.Identity.Web.Test.deps.json /home/REDACTED/work/microsoft-identity-web/microsoft-identity-web/tests/Microsoft.Identity.Web.Test/bin/Release/net8.0/testhost.dll --port 40265 --endpoint 127.0.0.1:040265 --role client --parentprocessid 5124 --telemetryoptedin false (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@XiaoxinMS2
Copy link
Copy Markdown
Contributor Author

XiaoxinMS2 commented May 21, 2026

@microsoft-github-policy-service agree company="Microsoft"

github-actions Bot pushed a commit to EelcoLos/nx-tinkering that referenced this pull request May 26, 2026
Updated
[Microsoft.Identity.Web](https://github.com/AzureAD/microsoft-identity-web)
from 4.9.0 to 4.10.0.

<details>
<summary>Release notes</summary>

_Sourced from [Microsoft.Identity.Web's
releases](https://github.com/AzureAD/microsoft-identity-web/releases)._

## 4.10.0

### New features
- Add `WithExtraBodyParameters` fluent API for attaching extra body
parameters to token acquisition requests. See
[#​3819](AzureAD/microsoft-identity-web#3819).
- Add `IConfidentialClientApplicationProvider` extensibility interface
and `CachePartitionKey` support for silent token acquisition. See
[#​3822](AzureAD/microsoft-identity-web#3822).

### Bug fixes
- Redirect URI sanitization in authorization scenarios; centralize
redirect URI validation in a shared helper. See
[#​3825](AzureAD/microsoft-identity-web#3825).
- Reject dSTS-shaped `Authority` values with a clearer exception,
steering users to use `Instance` + `TenantId` instead. See
[#​3805](AzureAD/microsoft-identity-web#3805).
- Improve regex handling and adding length/timeout safeguards for
SameSite User Agent. See
[#​3811](AzureAD/microsoft-identity-web#3811).

### Behavior changes
- **B2C OpenID Connect event handler: LRU cache for issuer address.**
Issuer address lookups in the B2C OIDC event handler are now cached with
an LRU cache, improving performance for repeated lookups. See
[#​3821](AzureAD/microsoft-identity-web#3821).

### Dependencies updates
- Update MSAL.NET to 4.84.1. See
[#​3822](AzureAD/microsoft-identity-web#3822).
- Pin `Microsoft.Kiota.Abstractions` to 1.22.0 for GraphServiceClient.
See
[#​3817](AzureAD/microsoft-identity-web#3817).
- Bump `uuid` and `@​azure/msal-node` in SidecarAdapter TypeScript test
app. See
[#​3826](AzureAD/microsoft-identity-web#3826).
- Bump `qs` in SidecarAdapter TypeScript test app. See
[#​3829](AzureAD/microsoft-identity-web#3829).

Commits viewable in [compare
view](AzureAD/microsoft-identity-web@4.9.0...4.10.0).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Microsoft.Identity.Web&package-manager=nuget&previous-version=4.9.0&new-version=4.10.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore <dependency name> major version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's major version (unless you unignore this specific
dependency's major version or upgrade to it yourself)
- `@dependabot ignore <dependency name> minor version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's minor version (unless you unignore this specific
dependency's minor version or upgrade to it yourself)
- `@dependabot ignore <dependency name>` will close this group update PR
and stop Dependabot creating any more for the specific dependency
(unless you unignore this specific dependency or upgrade to it yourself)
- `@dependabot unignore <dependency name>` will remove all of the ignore
conditions of the specified dependency
- `@dependabot unignore <dependency name> <ignore condition>` will
remove the ignore condition of the specified dependency and ignore
conditions


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Eelco Los <5102501+EelcoLos@users.noreply.github.com>
This was referenced May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants