Skip to content

Commit

Permalink
Fix auth session not being accepted via cookie anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Dec 7, 2024
1 parent 5068921 commit a732e3e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion API/Controller/Account/Logout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task<IActionResult> Logout(
[FromServices] ApiConfig apiConfig)
{
// Remove session if valid
if (HttpContext.TryGetUserSessionCookie(out var sessionCookie))
if (HttpContext.TryGetUserSession(out var sessionCookie))
{
await sessionService.DeleteSessionById(sessionCookie);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public UserSessionAuthentication(

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Context.TryGetUserSessionCookie(out var sessionKey))
if (!Context.TryGetUserSession(out var sessionKey))
{
return AuthenticateResult.Fail(AuthResultError.CookieMissingOrInvalid.Type!);
}
Expand Down
2 changes: 1 addition & 1 deletion Common/Constants/AuthConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public static class AuthConstants
{
public const string UserSessionCookieName = "openShockSession";
public const string SessionHeaderName = "OpenShockSession";
public const string UserSessionHeaderName = "OpenShockSession";
public const string ApiTokenHeaderName = "OpenShockToken";
public const string HubTokenHeaderName = "DeviceToken";
}
2 changes: 1 addition & 1 deletion Common/Hubs/ShareLinkHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override async Task OnConnectedAsync()

GenericIni? user = null;

if (httpContext.TryGetUserSessionCookie(out var sessionCookie))
if (httpContext.TryGetUserSession(out var sessionCookie))
{
user = await SessionAuth(sessionCookie);
if (user == null)
Expand Down
12 changes: 9 additions & 3 deletions Common/Utils/AuthUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ public static void RemoveSessionKeyCookie(this HttpContext context, string domai
});
}

public static bool TryGetUserSessionCookie(this HttpContext context, [NotNullWhen(true)] out string? sessionCookie)
public static bool TryGetUserSession(this HttpContext context, [NotNullWhen(true)] out string? sessionToken)
{
if (context.Request.Cookies.TryGetValue(AuthConstants.UserSessionCookieName, out sessionCookie) && !string.IsNullOrEmpty(sessionCookie))
if (context.Request.Cookies.TryGetValue(AuthConstants.UserSessionCookieName, out sessionToken) && !string.IsNullOrEmpty(sessionToken))
{
return true;
}

if(context.Request.Headers.TryGetValue(AuthConstants.UserSessionHeaderName, out var headerSessionCookie) && !string.IsNullOrEmpty(headerSessionCookie))
{
sessionToken = headerSessionCookie.ToString();
return true;
}

sessionCookie = null;
sessionToken = null;

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Cron/DashboardAdminAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<bool> AuthorizeAsync(DashboardContext context)
var userSessions = redis.RedisCollection<LoginSession>(false);
var db = httpContext.RequestServices.GetRequiredService<OpenShockContext>();

if (httpContext.TryGetUserSessionCookie(out var userSessionCookie))
if (httpContext.TryGetUserSession(out var userSessionCookie))
{
if (await SessionAuthAdmin(userSessionCookie, userSessions, db))
{
Expand Down

0 comments on commit a732e3e

Please sign in to comment.