1
0
mirror of https://github.com/bitwarden/server synced 2025-12-14 07:13:39 +00:00
Files
server/src/Core/AdminConsole/Utilities/IntegrationTemplateProcessor.cs
Brant DeBow f6cd661e8e [PM-17562] Add HEC integration support (#6010)
* [PM-17562] Add HEC integration support

* Re-ordered parameters per PR suggestion

* Apply suggestions from code review

Co-authored-by: Matt Bishop <mbishop@bitwarden.com>

* Refactored webhook request model validation to be more clear

---------

Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
2025-07-01 08:52:38 -04:00

67 lines
1.8 KiB
C#

#nullable enable
using System.Text.Json;
using System.Text.RegularExpressions;
namespace Bit.Core.AdminConsole.Utilities;
public static partial class IntegrationTemplateProcessor
{
[GeneratedRegex(@"#(\w+)#")]
private static partial Regex TokenRegex();
public static string ReplaceTokens(string template, object values)
{
if (string.IsNullOrEmpty(template))
{
return template;
}
var type = values.GetType();
return TokenRegex().Replace(template, match =>
{
var propertyName = match.Groups[1].Value;
if (propertyName == "EventMessage")
{
return JsonSerializer.Serialize(values);
}
else
{
var property = type.GetProperty(propertyName);
return property?.GetValue(values)?.ToString() ?? match.Value;
}
});
}
public static bool TemplateRequiresUser(string template)
{
if (string.IsNullOrEmpty(template))
{
return false;
}
return template.Contains("#UserName#", StringComparison.Ordinal)
|| template.Contains("#UserEmail#", StringComparison.Ordinal);
}
public static bool TemplateRequiresActingUser(string template)
{
if (string.IsNullOrEmpty(template))
{
return false;
}
return template.Contains("#ActingUserName#", StringComparison.Ordinal)
|| template.Contains("#ActingUserEmail#", StringComparison.Ordinal);
}
public static bool TemplateRequiresOrganization(string template)
{
if (string.IsNullOrEmpty(template))
{
return false;
}
return template.Contains("#OrganizationName#", StringComparison.Ordinal);
}
}