7
7
using System . IO ;
8
8
using System . Linq ;
9
9
using System . Net . Http ;
10
+ using System . Net . Http . Headers ;
10
11
using System . Net . Http . Json ;
12
+ using System . Text ;
11
13
using System . Threading . Tasks ;
12
14
13
15
namespace SynoAI . Notifiers . Webhook
@@ -25,6 +27,24 @@ public class Webhook : NotifierBase
25
27
/// The HTTP method (POST/PUT/etc).
26
28
/// </summary>
27
29
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
+
28
48
/// <summary>
29
49
/// The field name when posting the image.
30
50
/// </summary>
@@ -50,6 +70,8 @@ public override async Task Send(Camera camera, ISnapshotManager snapshotManager,
50
70
logger . LogInformation ( $ "{ camera . Name } : Webhook: Processing") ;
51
71
using ( HttpClient client = new HttpClient ( ) )
52
72
{
73
+ client . DefaultRequestHeaders . Authorization = GetAuthenticationHeader ( ) ;
74
+
53
75
MultipartFormDataContent data = new MultipartFormDataContent ( ) ;
54
76
if ( SendTypes )
55
77
{
@@ -112,6 +134,29 @@ public override async Task Send(Camera camera, ISnapshotManager snapshotManager,
112
134
}
113
135
}
114
136
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
+
115
160
/// <summary>
116
161
/// Fetches the response content and parses it a DeepStack object.
117
162
/// </summary>
0 commit comments