Skip to content

Commit

Permalink
Added feature: History encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
mergehez committed Oct 10, 2024
1 parent 2f606cc commit 796c96c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions InertiaNetCore/Inertia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static class Inertia
public static void Share(InertiaProps data) => _factory.Share(data);

public static void Flash(string key, string? value) => _factory.Flash(key, value);

public static void EnableEncryptHistory(bool enable = true) => _factory.EnableEncryptHistory(enable);
public static void ClearHistory() => _factory.ClearHistory();

public static LazyProp<T> Lazy<T>(Func<T?> callback) => _factory.Lazy(callback);
public static LazyProp<T> Lazy<T>(Func<Task<T?>> callback) => _factory.Lazy(callback);
Expand Down
2 changes: 2 additions & 0 deletions InertiaNetCore/Models/InertiaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class InertiaOptions

public Action<SessionOptions> ConfigureSession { get; set; } = _ => { };

public bool EncryptHistory { get; set; }

private static JsonSerializerOptions DefaultJsonSerializerOptions { get; } = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Expand Down
2 changes: 2 additions & 0 deletions InertiaNetCore/Models/InertiaPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public readonly record struct InertiaPage
public required string Component { get; init; }
public required string? Version { get; init; }
public required string Url { get; init; }
public required bool EncryptHistory { get; init; }
public required bool ClearHistory { get; init; }
}
15 changes: 9 additions & 6 deletions InertiaNetCore/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

namespace InertiaNetCore;

public class Response(string component, InertiaProps props, string? version, InertiaOptions options)
public class Response(string component, InertiaProps props, string? version, InertiaOptions options, bool? encryptHistory, bool clearHistory)
: IActionResult
{
private IDictionary<string, object>? _viewData;
private readonly InertiaOptions _options = options;

public async Task ExecuteResultAsync(ActionContext context)
{
Expand All @@ -23,7 +24,9 @@ public async Task ExecuteResultAsync(ActionContext context)
Url = context.HttpContext.RequestedUri(),
Props = await GetFinalProps(context),
DeferredProps = GetDeferredProps(context),
MergeProps = GetMergeProps(context)
MergeProps = GetMergeProps(context),
ClearHistory = clearHistory,
EncryptHistory = encryptHistory ?? options.EncryptHistory
};

if (!context.HttpContext.IsInertiaRequest())
Expand All @@ -39,15 +42,15 @@ public async Task ExecuteResultAsync(ActionContext context)
viewData[key] = value;
}

await new ViewResult { ViewName = options.RootView, ViewData = viewData }.ExecuteResultAsync(context);
await new ViewResult { ViewName = _options.RootView, ViewData = viewData }.ExecuteResultAsync(context);
}
else
{
context.HttpContext.Response.Headers.Append("X-Inertia", "true");
context.HttpContext.Response.Headers.Append("Vary", "Accept");
context.HttpContext.Response.StatusCode = 200;

var jsonResult = new JsonResult(page, options.JsonSerializerOptions);
var jsonResult = new JsonResult(page, _options.JsonSerializerOptions);
await jsonResult.ExecuteResultAsync(context);
}
}
Expand Down Expand Up @@ -89,7 +92,7 @@ private Dictionary<string, List<string>> GetDeferredProps(ActionContext context)
}

// apply json serialization options to dictionary keys before grouping them
var jsonOptions = options.JsonSerializerOptions as JsonSerializerOptions;
var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions;
tmp = JsonSerializer.Deserialize<Dictionary<string, string>>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions);

return tmp!
Expand All @@ -113,7 +116,7 @@ private List<string> GetMergeProps(ActionContext context)
}

// apply json serialization options to dictionary keys before grouping them
var jsonOptions = options.JsonSerializerOptions as JsonSerializerOptions;
var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions;
tmp = JsonSerializer.Deserialize<Dictionary<string, string>>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions);

return tmp!.Select(prop => prop.Key).ToList();
Expand Down
14 changes: 13 additions & 1 deletion InertiaNetCore/ResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ namespace InertiaNetCore;
internal class ResponseFactory(IHttpContextAccessor contextAccessor, SsrGateway ssrGateway, IOptions<InertiaOptions> options)
{
private object? _version;
private bool? _encryptHistory;
private bool _clearHistory;

public Response Render(string component, InertiaProps? props = default)
{
props ??= [];

return new Response(component, props, GetVersion(), options.Value);
return new Response(component, props, GetVersion(), options.Value, _encryptHistory, _clearHistory);
}

public async Task<IHtmlContent> Head(dynamic model)
Expand Down Expand Up @@ -111,6 +113,16 @@ public void Flash(string key, string? value)
context.Features.Set(flash);
}

public void EnableEncryptHistory(bool enable = true)
{
_encryptHistory = enable;
}

public void ClearHistory()
{
_clearHistory = true;
}

public LazyProp<T> Lazy<T>(Func<T?> callback) => new(callback);
public LazyProp<T> Lazy<T>(Func<Task<T?>> callback) => new(callback);
public DeferredProp<T> Defer<T>(Func<T?> callback, string? group) => new(callback, group);
Expand Down

0 comments on commit 796c96c

Please sign in to comment.