This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 644
/
WebhookUtility.cs
373 lines (327 loc) · 18.2 KB
/
WebhookUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#if !ONPREMISES
using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Utilities.Async;
using OfficeDevPnP.Core.Utilities.Webhooks;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace OfficeDevPnP.Core.Utilities
{
/// <summary>
/// The list of Hookable Resource Types
/// </summary>
public enum WebHookResourceType
{
/// <summary>
/// List web hooks
/// </summary>
List,
}
/// <summary>
/// Class containing utility methods to manage Webhook on a SharePoint resource
/// Adapted from https://github.com/SharePoint/sp-dev-samples/blob/master/Samples/WebHooks.List/SharePoint.WebHooks.Common/WebHookManager.cs
/// </summary>
internal static class WebhookUtility
{
private const string SubscriptionsUrlPart = "subscriptions";
private const string ListIdentifierFormat = @"{0}/_api/web/lists(guid'{1}')";
public const int MaximumValidityInMonths = 6;
public const int ExpirationDateTimeMaxDays = 180;
/// <summary>
/// The effective maximum expiration datetime
/// </summary>
public static DateTime MaxExpirationDateTime
{
get
{
return DateTime.UtcNow.AddDays(ExpirationDateTimeMaxDays);
}
}
/// <summary>
/// Add a Webhook subscription to a SharePoint resource
/// </summary>
/// <param name="webUrl">Url of the site holding the list</param>
/// <param name="resourceType">The type of Hookable SharePoint resource</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <param name="context">ClientContext instance to use for authentication</param>
/// <param name="subscription">The Webhook subscription to add</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when expiration date is out of valid range.</exception>
/// <returns>The added subscription object</returns>
public static async Task<WebhookSubscription> AddWebhookSubscriptionAsync(string webUrl, WebHookResourceType resourceType, string accessToken, ClientContext context, WebhookSubscription subscription)
{
if (!ValidateExpirationDateTime(subscription.ExpirationDateTime))
throw new ArgumentOutOfRangeException(nameof(subscription.ExpirationDateTime), "The specified expiration date is invalid. Should be greater than today and within 6 months");
await new SynchronizationContextRemover();
string responseString = null;
using (var handler = new HttpClientHandler())
{
context.Web.EnsureProperty(p => p.Url);
if (String.IsNullOrEmpty(accessToken))
{
handler.SetAuthenticationCookies(context);
}
using (var httpClient = new PnPHttpProvider(handler))
{
string identifierUrl = GetResourceIdentifier(resourceType, webUrl, subscription.Resource);
if (string.IsNullOrEmpty(identifierUrl))
{
throw new Exception("Identifier of the resource cannot be determined");
}
string requestUrl = identifierUrl + "/" + SubscriptionsUrlPart;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
request.Headers.Add("X-RequestDigest", await context.GetRequestDigestAsync());
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (!string.IsNullOrEmpty(accessToken))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
request.Content = new StringContent(JsonConvert.SerializeObject(subscription),
Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.SendAsync(request, new System.Threading.CancellationToken());
if (response.IsSuccessStatusCode)
{
responseString = await response.Content.ReadAsStringAsync();
}
else
{
// Something went wrong...
throw new Exception(await response.Content.ReadAsStringAsync());
}
}
}
return JsonConvert.DeserializeObject<WebhookSubscription>(responseString);
}
/// <summary>
/// Add a Webhook subscription to a SharePoint resource
/// </summary>
/// <param name="webUrl">Url of the site holding the list</param>
/// <param name="resourceType">The type of Hookable SharePoint resource</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <param name="context">ClientContext instance to use for authentication</param>
/// <param name="resourceId">The Unique Identifier of the resource</param>
/// <param name="notificationUrl">The Webhook endpoint URL</param>
/// <param name="clientState">The client state to use in the Webhook subscription</param>
/// <param name="validityInMonths">The validity of the subscriptions in months</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when expiration date is out of valid range.</exception>
/// <returns>The added subscription object</returns>
public static async Task<WebhookSubscription> AddWebhookSubscriptionAsync(string webUrl, WebHookResourceType resourceType, string accessToken, ClientContext context, string resourceId, string notificationUrl,
string clientState = null, int validityInMonths = MaximumValidityInMonths)
{
await new SynchronizationContextRemover();
// If validity in months is the Maximum, use the effective max allowed DateTime instead
DateTime expirationDateTime = validityInMonths == MaximumValidityInMonths
? MaxExpirationDateTime
: DateTime.UtcNow.AddMonths(validityInMonths);
var subscription = new WebhookSubscription()
{
Resource = resourceId,
NotificationUrl = notificationUrl,
ExpirationDateTime = expirationDateTime,
ClientState = clientState
};
return await AddWebhookSubscriptionAsync(webUrl, resourceType, accessToken, context, subscription);
}
/// <summary>
/// Updates the expiration datetime (and notification URL) of an existing SharePoint web hook
/// </summary>
/// <param name="webUrl">Url of the site holding resource with the webhook</param>
/// <param name="resourceType">The type of Hookable SharePoint resource</param>
/// <param name="resourceId">Id of the resource (e.g. list id)</param>
/// <param name="subscriptionId">Id of the web hook subscription that we need to delete</param>
/// <param name="webHookEndPoint">Url of the web hook service endpoint (the one that will be called during an event)</param>
/// <param name="expirationDateTime">New web hook expiration date</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <param name="context">ClientContext instance to use for authentication</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when expiration date is out of valid range.</exception>
/// <returns>true if succesful, exception in case something went wrong</returns>
public static async Task<bool> UpdateWebhookSubscriptionAsync(string webUrl, WebHookResourceType resourceType, string resourceId, string subscriptionId,
string webHookEndPoint, DateTime expirationDateTime, string accessToken, ClientContext context)
{
if (!ValidateExpirationDateTime(expirationDateTime))
throw new ArgumentOutOfRangeException(nameof(expirationDateTime), "The specified expiration date is invalid. Should be greater than today and within 6 months");
await new SynchronizationContextRemover();
using (var handler = new HttpClientHandler())
{
context.Web.EnsureProperty(p => p.Url);
if (String.IsNullOrEmpty(accessToken))
{
handler.SetAuthenticationCookies(context);
}
using (var httpClient = new PnPHttpProvider(handler))
{
string identifierUrl = GetResourceIdentifier(resourceType, webUrl, resourceId);
if (string.IsNullOrEmpty(identifierUrl))
{
throw new Exception("Identifier of the resource cannot be determined");
}
string requestUrl = string.Format("{0}/{1}('{2}')", identifierUrl, SubscriptionsUrlPart, subscriptionId);
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUrl);
request.Headers.Add("X-RequestDigest", await context.GetRequestDigestAsync());
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (!string.IsNullOrEmpty(accessToken))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
WebhookSubscription webhookSubscription;
if (string.IsNullOrEmpty(webHookEndPoint))
{
webhookSubscription = new WebhookSubscription()
{
ExpirationDateTime = expirationDateTime
};
}
else
{
webhookSubscription = new WebhookSubscription()
{
NotificationUrl = webHookEndPoint,
ExpirationDateTime = expirationDateTime
};
}
request.Content = new StringContent(JsonConvert.SerializeObject(webhookSubscription),
Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.SendAsync(request, new System.Threading.CancellationToken());
if (response.StatusCode != System.Net.HttpStatusCode.NoContent)
{
// oops...something went wrong, maybe the web hook does not exist?
throw new Exception(await response.Content.ReadAsStringAsync());
}
else
{
return true;
}
}
}
}
/// <summary>
/// Deletes an existing SharePoint web hook
/// </summary>
/// <param name="webUrl">Url of the site holding resource with the webhook</param>
/// <param name="resourceType">The type of Hookable SharePoint resource</param>
/// <param name="resourceId">Id of the resource (e.g. list id)</param>
/// <param name="subscriptionId">Id of the web hook subscription that we need to delete</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <param name="context">ClientContext instance to use for authentication</param>
/// <returns>true if succesful, exception in case something went wrong</returns>
public static async Task<bool> RemoveWebhookSubscriptionAsync(string webUrl, WebHookResourceType resourceType, string resourceId, string subscriptionId, string accessToken, ClientContext context)
{
await new SynchronizationContextRemover();
using (var handler = new HttpClientHandler())
{
context.Web.EnsureProperty(p => p.Url);
if (String.IsNullOrEmpty(accessToken))
{
handler.SetAuthenticationCookies(context);
}
using (var httpClient = new PnPHttpProvider(handler))
{
string identifierUrl = GetResourceIdentifier(resourceType, webUrl, resourceId);
if (string.IsNullOrEmpty(identifierUrl))
{
throw new Exception("Identifier of the resource cannot be determined");
}
string requestUrl = string.Format("{0}/{1}('{2}')", identifierUrl, SubscriptionsUrlPart, subscriptionId);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, requestUrl);
request.Headers.Add("X-RequestDigest", await context.GetRequestDigestAsync());
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (!string.IsNullOrEmpty(accessToken))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
HttpResponseMessage response = await httpClient.SendAsync(request, new System.Threading.CancellationToken());
if (response.StatusCode != System.Net.HttpStatusCode.NoContent)
{
// oops...something went wrong, maybe the web hook does not exist?
throw new Exception(await response.Content.ReadAsStringAsync());
}
else
{
return true;
}
}
}
}
/// <summary>
/// Get all webhook subscriptions on a given SharePoint resource
/// </summary>
/// <param name="webUrl">Url of the site holding the resource</param>
/// <param name="resourceType">The type of SharePoint resource</param>
/// <param name="resourceId">The Unique Identifier of the resource</param>
/// <param name="accessToken">Access token to authenticate against SharePoint</param>
/// <param name="context">ClientContext instance to use for authentication</param>
/// <returns>Collection of <see cref="WebhookSubscription"/> instances, one per returned web hook</returns>
public static async Task<ResponseModel<WebhookSubscription>> GetWebhooksSubscriptionsAsync(string webUrl, WebHookResourceType resourceType, string resourceId, string accessToken, ClientContext context)
{
await new SynchronizationContextRemover();
string responseString = null;
using (var handler = new HttpClientHandler())
{
context.Web.EnsureProperty(p => p.Url);
if (String.IsNullOrEmpty(accessToken))
{
handler.SetAuthenticationCookies(context);
}
using (var httpClient = new PnPHttpProvider(handler))
{
string identifierUrl = GetResourceIdentifier(resourceType, webUrl, resourceId);
if (string.IsNullOrEmpty(identifierUrl))
{
throw new Exception("Identifier of the resource cannot be determined");
}
string requestUrl = identifierUrl + "/" + SubscriptionsUrlPart;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (accessToken != null)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
HttpResponseMessage response = await httpClient.SendAsync(request, new System.Threading.CancellationToken());
if (response.IsSuccessStatusCode)
{
responseString = await response.Content.ReadAsStringAsync();
}
else
{
// oops...something went wrong
throw new Exception(await response.Content.ReadAsStringAsync());
}
}
}
return JsonConvert.DeserializeObject<ResponseModel<WebhookSubscription>>(responseString);
}
/// <summary>
/// Get the proper identifier Url according to the resource type. (No great utility currently with the support for lists only, but will be later with the support for other resources)
/// </summary>
/// <param name="resourceType">The type of resource</param>
/// <param name="webUrl">The URL of the SharePoint web</param>
/// <param name="id">The id part of the resource</param>
/// <returns>The well forned resource identifier URL</returns>
private static string GetResourceIdentifier(WebHookResourceType resourceType, string webUrl, string id)
{
switch (resourceType)
{
case WebHookResourceType.List:
return string.Format(ListIdentifierFormat, webUrl, id);
default:
return null;
}
}
/// <summary>
/// Checks whether the specified expiration datetime is within a valid period
/// </summary>
/// <param name="expirationDateTime">The datetime value to validate</param>
/// <returns><c>true</c> if valid, <c>false</c> otherwise</returns>
private static bool ValidateExpirationDateTime(DateTime expirationDateTime)
{
DateTime utcDateToValidate = expirationDateTime.ToUniversalTime();
DateTime utcNow = DateTime.UtcNow;
return utcDateToValidate > utcNow
&& utcDateToValidate <= MaxExpirationDateTime;
}
}
}
#endif