-
Notifications
You must be signed in to change notification settings - Fork 183
mgmt: Adds Auth0-Custom-Header on allow-listed Management API endpoints #985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Auth0.ManagementApi.Core; | ||
|
|
||
| /// <summary> | ||
| /// A <see cref="DelegatingHandler"/> that enforces the <c>Auth0-Custom-Domain</c> header whitelist. | ||
| /// | ||
| /// <para> | ||
| /// The <c>Auth0-Custom-Domain</c> header is only meaningful for specific Management API endpoints | ||
| /// that generate user-facing links (email verification, password reset, invitations, etc.). | ||
| /// This handler strips the header from requests to any path not on the whitelist, preventing | ||
| /// it from leaking to unrelated endpoints. | ||
| /// </para> | ||
| /// | ||
| /// <para>Whitelisted paths:</para> | ||
| /// <list type="bullet"> | ||
| /// <item><c>POST /tickets/email-verification</c></item> | ||
| /// <item><c>POST /tickets/password-change</c></item> | ||
| /// <item><c>POST /organizations/{id}/invitations</c></item> | ||
| /// <item><c>POST /guardian/enrollments/ticket</c></item> | ||
| /// <item><c>POST /jobs/verification-email</c></item> | ||
| /// <item><c>POST /jobs/users-imports</c></item> | ||
| /// <item><c>POST /users</c> and <c>PATCH /users/{id}</c></item> | ||
| /// <item><c>POST /self-service-profiles/{id}/sso-ticket</c></item> | ||
| /// </list> | ||
| /// </summary> | ||
| public class CustomDomainInterceptor : DelegatingHandler | ||
| { | ||
| /// <summary> | ||
| /// The name of the custom domain header. | ||
| /// </summary> | ||
| public const string HeaderName = "Auth0-Custom-Domain"; | ||
|
|
||
| private static readonly Regex[] WhitelistedPaths = | ||
| { | ||
| new Regex(@".*/tickets/email-verification$", RegexOptions.Compiled), | ||
| new Regex(@".*/tickets/password-change$", RegexOptions.Compiled), | ||
| new Regex(@".*/organizations/[^/]+/invitations(/[^/]+)?$", RegexOptions.Compiled), | ||
| new Regex(@".*/guardian/enrollments/ticket$", RegexOptions.Compiled), | ||
| new Regex(@".*/jobs/verification-email$", RegexOptions.Compiled), | ||
| new Regex(@".*/jobs/users-imports$", RegexOptions.Compiled), | ||
| new Regex(@".*/users(/[^/]+)?$", RegexOptions.Compiled), | ||
| new Regex(@".*/self-service-profiles/[^/]+/sso-ticket(/[^/]+/revoke)?$", RegexOptions.Compiled), | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="CustomDomainInterceptor"/> with a default | ||
| /// <see cref="HttpClientHandler"/> as the inner handler. | ||
| /// </summary> | ||
| public CustomDomainInterceptor() | ||
| : base(new HttpClientHandler()) { } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="CustomDomainInterceptor"/> wrapping the | ||
| /// specified inner handler. | ||
| /// </summary> | ||
| /// <param name="innerHandler">The inner <see cref="HttpMessageHandler"/> to delegate to.</param> | ||
| /// <exception cref="ArgumentNullException">Thrown when <paramref name="innerHandler"/> is null.</exception> | ||
| public CustomDomainInterceptor(HttpMessageHandler innerHandler) | ||
| : base(innerHandler ?? throw new ArgumentNullException(nameof(innerHandler))) { } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override Task<HttpResponseMessage> SendAsync( | ||
| HttpRequestMessage request, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| if (request.Headers.Contains(HeaderName) && | ||
| !IsWhitelisted(request.RequestUri?.AbsolutePath)) | ||
| { | ||
| request.Headers.Remove(HeaderName); | ||
| } | ||
|
|
||
| return base.SendAsync(request, cancellationToken); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <c>true</c> if <paramref name="path"/> matches one of the whitelisted endpoint patterns. | ||
| /// Used internally by <see cref="SendAsync"/> and exposed for unit testing via <c>InternalsVisibleTo</c>. | ||
| /// </summary> | ||
| /// <param name="path">The URL absolute path to test (e.g. <c>/api/v2/tickets/email-verification</c>).</param> | ||
| internal static bool IsWhitelisted(string? path) | ||
| { | ||
| if (string.IsNullOrEmpty(path)) | ||
| return false; | ||
|
|
||
| return WhitelistedPaths.Any(p => p.IsMatch(path)); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/Auth0.ManagementApi/Core/Public/ClientOptions.Custom.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using Auth0.ManagementApi.Core; | ||
|
|
||
| namespace Auth0.ManagementApi; | ||
|
|
||
| /// <summary> | ||
| /// Convenience helper for creating per-request <c>Auth0-Custom-Domain</c> overrides. | ||
| /// | ||
| /// <para> | ||
| /// Use this when you need to specify a custom domain for a single API call without | ||
| /// configuring it globally on the client. The header is forwarded only to whitelisted | ||
| /// endpoints — if you have a <see cref="CustomDomainInterceptor"/> configured on your | ||
| /// <see cref="System.Net.Http.HttpClient"/>, it is stripped automatically from any | ||
| /// non-whitelisted path. | ||
| /// </para> | ||
| /// | ||
| /// <para> | ||
| /// When you need the custom domain header <em>and</em> other <see cref="RequestOptions"/> | ||
| /// settings (extra headers, timeout, retries) in the same call, construct | ||
| /// <see cref="RequestOptions"/> directly instead of using this helper: | ||
| /// <code> | ||
| /// await client.Tickets.VerifyEmailAsync(request, new RequestOptions | ||
| /// { | ||
| /// AdditionalHeaders = new[] | ||
| /// { | ||
| /// new KeyValuePair<string, string?>(CustomDomainInterceptor.HeaderName, "login.mycompany.com"), | ||
| /// new KeyValuePair<string, string?>("X-Correlation-Id", "abc-123"), | ||
| /// }, | ||
| /// MaxRetries = 1, | ||
| /// }); | ||
| /// </code> | ||
| /// </para> | ||
| /// | ||
| /// <example> | ||
| /// <code> | ||
| /// // Override for a specific request | ||
| /// await client.Tickets.VerifyEmailAsync(request, CustomDomainHeader.For("login.mycompany.com")); | ||
| /// | ||
| /// // Override for organization invitations | ||
| /// await client.Organizations.Invitations.CreateAsync(orgId, invitation, CustomDomainHeader.For("login.mycompany.com")); | ||
| /// </code> | ||
| /// </example> | ||
| /// </summary> | ||
| public static class CustomDomainHeader | ||
| { | ||
| /// <summary> | ||
| /// Creates a <see cref="RequestOptions"/> with the <c>Auth0-Custom-Domain</c> header set | ||
| /// to <paramref name="domain"/>. | ||
| /// </summary> | ||
| /// <param name="domain">The custom domain (e.g. <c>"login.mycompany.com"</c>).</param> | ||
| /// <returns>A <see cref="RequestOptions"/> carrying the custom domain header.</returns> | ||
| /// <exception cref="ArgumentException">Thrown when <paramref name="domain"/> is null, empty, or whitespace.</exception> | ||
| public static RequestOptions For(string domain) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(domain)) | ||
| throw new ArgumentException("Domain must not be null or whitespace.", nameof(domain)); | ||
|
|
||
| return new RequestOptions | ||
| { | ||
| AdditionalHeaders = new[] | ||
| { | ||
| new KeyValuePair<string, string?>(CustomDomainInterceptor.HeaderName, domain), | ||
| }, | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indentation, if needed