mirror of
https://github.com/DorkSquadRR/DorkNet.git
synced 2026-10-25 23:18:44 -07:00
Match legacy token cookie behavior
This commit is contained in:
@@ -277,7 +277,8 @@ internal static class RecRoomIdentityServerConfig
|
|||||||
public sealed class IdentityServerGameTokenRequestMiddleware(
|
public sealed class IdentityServerGameTokenRequestMiddleware(
|
||||||
RequestDelegate next,
|
RequestDelegate next,
|
||||||
DomainConfig domain,
|
DomainConfig domain,
|
||||||
IConfiguration config)
|
IConfiguration config,
|
||||||
|
ILogger<IdentityServerGameTokenRequestMiddleware> logger)
|
||||||
{
|
{
|
||||||
public async Task InvokeAsync(HttpContext context)
|
public async Task InvokeAsync(HttpContext context)
|
||||||
{
|
{
|
||||||
@@ -380,10 +381,10 @@ public sealed class IdentityServerGameTokenRequestMiddleware(
|
|||||||
var options = new CookieOptions
|
var options = new CookieOptions
|
||||||
{
|
{
|
||||||
HttpOnly = true,
|
HttpOnly = true,
|
||||||
Secure = context.Request.IsHttps || domain.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase),
|
Secure = string.Equals(domain.Scheme, "https", StringComparison.OrdinalIgnoreCase),
|
||||||
SameSite = SameSiteMode.Lax,
|
SameSite = SameSiteMode.None,
|
||||||
Path = "/",
|
|
||||||
Expires = expires,
|
Expires = expires,
|
||||||
|
IsEssential = true,
|
||||||
};
|
};
|
||||||
|
|
||||||
var cookieDomain = ResolveCookieDomain(context.Request.Host.Host);
|
var cookieDomain = ResolveCookieDomain(context.Request.Host.Host);
|
||||||
@@ -391,6 +392,13 @@ public sealed class IdentityServerGameTokenRequestMiddleware(
|
|||||||
options.Domain = cookieDomain;
|
options.Domain = cookieDomain;
|
||||||
|
|
||||||
context.Response.Cookies.Append(AuthService.AccessCookieName, accessToken, options);
|
context.Response.Cookies.Append(AuthService.AccessCookieName, accessToken, options);
|
||||||
|
logger.LogInformation(
|
||||||
|
"[auth-cookie] issued access cookie host={Host} domain={Domain} secure={Secure} sameSite={SameSite} essential={IsEssential}",
|
||||||
|
context.Request.Host.Host,
|
||||||
|
cookieDomain ?? "<host-only>",
|
||||||
|
options.Secure,
|
||||||
|
options.SameSite,
|
||||||
|
options.IsEssential);
|
||||||
}
|
}
|
||||||
catch (JsonException)
|
catch (JsonException)
|
||||||
{
|
{
|
||||||
@@ -422,9 +430,33 @@ public sealed class IdentityServerGameTokenRequestMiddleware(
|
|||||||
return "." + apex;
|
return "." + apex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (domain.UsesHyphenSubdomains)
|
||||||
|
{
|
||||||
|
var parent = HyphenCookieParent(apex, host);
|
||||||
|
if (parent is not null)
|
||||||
|
return "." + parent;
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string? HyphenCookieParent(string apex, string host)
|
||||||
|
{
|
||||||
|
var parts = apex.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
|
if (parts.Length < 3)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var environmentLabel = parts[0];
|
||||||
|
var parent = string.Join('.', parts.Skip(1));
|
||||||
|
if (!host.EndsWith("." + parent, StringComparison.OrdinalIgnoreCase))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var hostLabel = host[..^(parent.Length + 1)];
|
||||||
|
return hostLabel.EndsWith("-" + environmentLabel, StringComparison.OrdinalIgnoreCase)
|
||||||
|
? parent
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
private static bool IsTokenRequest(HttpContext context) =>
|
private static bool IsTokenRequest(HttpContext context) =>
|
||||||
HttpMethods.IsPost(context.Request.Method)
|
HttpMethods.IsPost(context.Request.Method)
|
||||||
&& context.Request.Path.Equals("/connect/token", StringComparison.OrdinalIgnoreCase)
|
&& context.Request.Path.Equals("/connect/token", StringComparison.OrdinalIgnoreCase)
|
||||||
|
|||||||
Reference in New Issue
Block a user