Is it possible to set TokenEndpoint dynamically based on IncomingRequest? #411
-
|
Hey, good ? I'm trying to dynamically set the TokenEndpoint based on a downstream api, is that possible ? I was looking out ITokenRequestCustomizer and I was reading the internals to undarstand the flow Is there an existing recommended approach to override the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
At the moment, overriding the What you can try right now, depending on how much control you have over the token endpoint handler, is to include the tenant ID in the Here's a sample implementation for a public class TenantTokenRequestCustomizer : ITokenRequestCustomizer
{
public Task<TokenRequestParameters> Customize(
HttpRequestMessage httpRequest,
TokenRequestParameters baseParameters,
CancellationToken cancellationToken = default)
{
if (httpRequest.Headers.TryGetValues("X-Tenant-Id", out var tenantIds))
{
var tenantId = tenantIds.FirstOrDefault();
if (!string.IsNullOrEmpty(tenantId))
{
baseParameters.Parameters.Add("tenant_id", tenantId);
}
}
else if (httpRequest.RequestUri?.Query.Contains("tenant_id") ?? false)
{
// Extract tenant_id from query parameters
var queryParams = System.Web.HttpUtility.ParseQueryString(httpRequest.RequestUri.Query);
var tenantId = queryParams["tenant_id"];
if (!string.IsNullOrEmpty(tenantId))
{
baseParameters.Parameters.Add("tenant_id", tenantId);
}
}
else
{
// Fallback tenant ID
baseParameters.Parameters.Add("tenant_id", "default");
}
return Task.FromResult(baseParameters);
}
}
|
Beta Was this translation helpful? Give feedback.
At the moment, overriding the
TokenEndpointURL is not possible using either approach. We are however investigating adding this as a potential new feature in a next release (4.2.0 or later).What you can try right now, depending on how much control you have over the token endpoint handler, is to include the tenant ID in the
TokenRequestParameters.Parametersdictionary. These parameters will eventually be added to the form contents that are posted to the token endpoint.Here's a sample implementation for a
ITokenRequestCustomizerwhich adds atenant_idto the form. If you need to inject services in your customITokenRequestCustomizer, you'll need to wait for the official 4.1.0 release of the