mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 22:44:29 +01:00
* Move TagHelpers to Abstractions Makes them available for use in plugins. Also cleans up the tag helper references in the view code: As we have it in the root view imports, the individual directives in the views are superfluous. * Move CurrenciesSuggestionsTagHelper back To get rid of the Rating dependency in Abstractions.
32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
using BTCPayServer.Abstractions.Services;
|
|
using BTCPayServer.Security;
|
|
using Microsoft.AspNetCore.Razor.TagHelpers;
|
|
using NBitcoin;
|
|
|
|
namespace BTCPayServer.Abstractions.TagHelpers;
|
|
|
|
[HtmlTargetElement("srv-model")]
|
|
public class SrvModel : TagHelper
|
|
{
|
|
private readonly Safe _safe;
|
|
private readonly ContentSecurityPolicies _csp;
|
|
|
|
public SrvModel(Safe safe, ContentSecurityPolicies csp)
|
|
{
|
|
_safe = safe;
|
|
_csp = csp;
|
|
}
|
|
public string VarName { get; set; } = "srvModel";
|
|
public object Model { get; set; }
|
|
public override void Process(TagHelperContext context, TagHelperOutput output)
|
|
{
|
|
output.TagName = "script";
|
|
output.TagMode = TagMode.StartTagAndEndTag;
|
|
output.Attributes.Add(new TagHelperAttribute("type", "text/javascript"));
|
|
var nonce = RandomUtils.GetUInt256().ToString().Substring(0, 32);
|
|
output.Attributes.Add(new TagHelperAttribute("nonce", nonce));
|
|
_csp.Add("script-src", $"'nonce-{nonce}'");
|
|
output.Content.SetHtmlContent($"var {VarName} = {_safe.Json(Model)};");
|
|
}
|
|
}
|