| 1 | using AgentForge.Plugin.Sdk; |
| 2 | using AgentForge.Data.Entities; |
| 3 | using AgentForge.Services; |
| 4 | |
| 5 | namespace AgentForge.Plugin.View; |
| 6 | |
| 7 | public static class ForgeViewSession |
| 8 | { |
| 9 | internal const string CookieName = ForgePluginHttpAuth.ViewSessionCookieName; |
| 10 | |
| 11 | public static void SetSessionCookie(HttpResponse response, string plainToken) |
| 12 | { |
| 13 | response.Cookies.Append( |
| 14 | CookieName, |
| 15 | plainToken, |
| 16 | new CookieOptions |
| 17 | { |
| 18 | HttpOnly = true, |
| 19 | SameSite = SameSiteMode.Lax, |
| 20 | Path = "/", |
| 21 | IsEssential = true, |
| 22 | MaxAge = TimeSpan.FromDays(30), |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | public static void ClearSessionCookie(HttpResponse response) => |
| 27 | response.Cookies.Delete(CookieName, new CookieOptions { Path = "/" }); |
| 28 | |
| 29 | public static ForgeApiTokenEntity? TryGetToken(HttpContext? http, ForgeAuthService auth) |
| 30 | { |
| 31 | if (http is null) |
| 32 | return null; |
| 33 | |
| 34 | if (http.Request.Cookies.TryGetValue(CookieName, out var cookieToken) |
| 35 | && !string.IsNullOrWhiteSpace(cookieToken)) |
| 36 | { |
| 37 | var fromCookie = auth.ValidateApiToken(cookieToken); |
| 38 | if (fromCookie is not null) |
| 39 | return fromCookie; |
| 40 | } |
| 41 | |
| 42 | return ForgePluginHttpAuth.GetApiToken(http); |
| 43 | } |
| 44 | } |
| 45 | |
View only · write via MCP/CIDE