Skip to content

Commit 2517fbc

Browse files
committed
#12 Added support for Basic and Bearer webhook authentication
1 parent 2683dfb commit 2517fbc

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SynoAI.Notifiers.Webhook
2+
{
3+
public enum AuthorizationMethod
4+
{
5+
None,
6+
Basic,
7+
Bearer
8+
}
9+
}

SynoAI/Notifiers/Webhook/Webhook.cs

+45
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using System.IO;
88
using System.Linq;
99
using System.Net.Http;
10+
using System.Net.Http.Headers;
1011
using System.Net.Http.Json;
12+
using System.Text;
1113
using System.Threading.Tasks;
1214

1315
namespace SynoAI.Notifiers.Webhook
@@ -25,6 +27,24 @@ public class Webhook : NotifierBase
2527
/// The HTTP method (POST/PUT/etc).
2628
/// </summary>
2729
public string Method { get; set; }
30+
31+
/// <summary>
32+
/// The type of authentication.
33+
/// </summary>
34+
public AuthorizationMethod Authentication { get; set; }
35+
/// <summary>
36+
/// The username when using Basic authentication.
37+
/// </summary>
38+
public string Username {get;set;}
39+
/// <summary>
40+
/// The password to use when using Basic authentication.
41+
/// </summary>
42+
public string Password {get;set;}
43+
/// <summary>
44+
/// The token to use when using Bearer authentication.
45+
/// </summary>
46+
public string Token {get;set;}
47+
2848
/// <summary>
2949
/// The field name when posting the image.
3050
/// </summary>
@@ -50,6 +70,8 @@ public override async Task Send(Camera camera, ISnapshotManager snapshotManager,
5070
logger.LogInformation($"{camera.Name}: Webhook: Processing");
5171
using (HttpClient client = new HttpClient())
5272
{
73+
client.DefaultRequestHeaders.Authorization = GetAuthenticationHeader();
74+
5375
MultipartFormDataContent data = new MultipartFormDataContent();
5476
if (SendTypes)
5577
{
@@ -112,6 +134,29 @@ public override async Task Send(Camera camera, ISnapshotManager snapshotManager,
112134
}
113135
}
114136

137+
/// <summary>
138+
/// Generates an authentication header for the client.
139+
/// </summary>
140+
/// <returns>An authentication header.</returns>
141+
private AuthenticationHeaderValue GetAuthenticationHeader()
142+
{
143+
string parameter;
144+
switch (Authentication)
145+
{
146+
case AuthorizationMethod.Basic:
147+
byte[] bytes = Encoding.ASCII.GetBytes($"{Username}:{Password}");
148+
parameter = Convert.ToBase64String(bytes);
149+
break;
150+
case AuthorizationMethod.Bearer:
151+
parameter = Token;
152+
break;
153+
default:
154+
return null;
155+
}
156+
157+
return new AuthenticationHeaderValue(Authentication.ToString(), parameter);
158+
}
159+
115160
/// <summary>
116161
/// Fetches the response content and parses it a DeepStack object.
117162
/// </summary>

SynoAI/Notifiers/Webhook/WebhookFactory.cs

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public override INotifier Create(ILogger logger, IConfigurationSection section)
1414
logger.LogInformation("Processing Webhook Config");
1515

1616
string url = section.GetValue<string>("Url");
17+
AuthorizationMethod authentication = section.GetValue<AuthorizationMethod>("Authorization", AuthorizationMethod.None);
18+
string username = section.GetValue<string>("Username", null);
19+
string password = section.GetValue<string>("Password", null);
20+
string token = section.GetValue<string>("Token", null);
1721
string field = section.GetValue<string>("Field", "image");
1822
string method = section.GetValue<string>("Method", "POST");
1923
bool sendImage = section.GetValue<bool>("SendImage", true);
@@ -22,6 +26,10 @@ public override INotifier Create(ILogger logger, IConfigurationSection section)
2226
Webhook webhook = new Webhook()
2327
{
2428
Url = url,
29+
Authentication = authentication,
30+
Username = username,
31+
Password = password,
32+
Token = token,
2533
SendImage = sendImage,
2634
SendTypes = sendTypes
2735
};

0 commit comments

Comments
 (0)