-
Notifications
You must be signed in to change notification settings - Fork 177
/
FacebookClientWrapper.cs
279 lines (248 loc) · 13.4 KB
/
FacebookClientWrapper.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
using System;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Bot.Builder.Community.Adapters.Facebook.FacebookEvents;
using Bot.Builder.Community.Adapters.Facebook.FacebookEvents.Handover;
using Newtonsoft.Json;
namespace Bot.Builder.Community.Adapters.Facebook
{
/// <summary>
/// Client for interacting with the Facebook API.
/// </summary>
public class FacebookClientWrapper
{
/// <summary>
/// An instance of the FacebookClientWrapperOptions class.
/// </summary>
private readonly FacebookClientWrapperOptions _options;
/// <summary>
/// Initializes a new instance of the <see cref="FacebookClientWrapper"/> class.
/// </summary>
/// <param name="options">An object containing API credentials, a webhook verification token, and other options.</param>
public FacebookClientWrapper(FacebookClientWrapperOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
/// <summary>
/// Sends a REST message to Facebook.
/// </summary>
/// <param name="path">Path to the API endpoint, for example `/me/messages`.</param>
/// <param name="payload">An object to be sent as parameters to the API call.</param>
/// <param name="method">The HTTP method, for example POST, GET, DELETE or PUT.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A task that represents the work queued to execute.</returns>
/// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="payload"/> is null.</exception>
public virtual async Task<string> SendMessageAsync(string path, FacebookMessage payload, HttpMethod method = null, CancellationToken cancellationToken = default)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
if (payload == null)
{
throw new ArgumentNullException(nameof(payload));
}
var proof = GetAppSecretProof();
if (method == null)
{
method = HttpMethod.Post;
}
using (var request = new HttpRequestMessage())
{
request.RequestUri = new Uri($"https://{_options.FacebookApiHost}/{_options.FacebookApiVersion + path}?access_token={_options.FacebookAccessToken}&appsecret_proof={proof.ToLowerInvariant()}");
request.Method = method;
var json = JsonConvert.SerializeObject(
payload,
Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
});
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
using (var client = new HttpClient())
{
var res = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
if (res.IsSuccessStatusCode)
{
var responseBody = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
var stringResponse = JsonConvert.DeserializeObject<FacebookResponseOk>(responseBody);
return stringResponse.MessageId;
}
else
{
// In Azure view this exception via Application Insights/Failures.
throw new HttpRequestException($"SendMessageAsync(): {res.ToString()}");
}
}
}
}
/// <summary>
/// Verifies the SHA1 signature of the raw request payload before bodyParser parses it will abort parsing if signature is invalid, and pass a generic error to response.
/// </summary>
/// <param name="request">Represents the incoming side of an HTTP request.</param>
/// <param name="payload">The request body.</param>
/// <returns>The result of the comparison between the signature in the request and hashed body.</returns>
/// <exception cref="ArgumentNullException"><paramref name="request"/> is null.</exception>
public virtual bool VerifySignature(HttpRequest request, string payload)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var expected = request.Headers["x-hub-signature"].ToString().ToUpperInvariant();
#pragma warning disable CA5350 // Facebook uses SHA1 as cryptographic algorithm.
using (var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(_options.FacebookAppSecret)))
{
hmac.Initialize();
var hashArray = hmac.ComputeHash(Encoding.UTF8.GetBytes(payload));
var hash = $"SHA1={BitConverter.ToString(hashArray).Replace("-", string.Empty)}";
return expected == hash;
}
#pragma warning restore CA5350 // Facebook uses SHA1 as cryptographic algorithm.
}
/// <summary>
/// Generates the app secret proof used to increase security on calls to the Graph API.
/// </summary>
/// <returns>The app secret proof.</returns>
public virtual string GetAppSecretProof()
{
using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(_options.FacebookAppSecret)))
{
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(_options.FacebookAccessToken));
return BitConverter.ToString(hash).Replace("-", string.Empty);
}
}
/// <summary>
/// Verifies the verify token from the message. If the token matches the one configured, sends back the challenge.
/// </summary>
/// <param name="request">Represents the incoming side of an HTTP request.</param>
/// <param name="response">Represents the outgoing side of an HTTP request.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A task that represents the work queued to execute.</returns>
/// <exception cref="ArgumentNullException"><paramref name="request"/> or <paramref name="response"/> is null.</exception>
public virtual async Task VerifyWebhookAsync(HttpRequest request, HttpResponse response, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (response == null)
{
throw new ArgumentNullException(nameof(response));
}
var challenge = string.Empty;
HttpStatusCode statusCode;
if (request.Query["hub.verify_token"].Equals(_options.FacebookVerifyToken))
{
challenge = request.Query["hub.challenge"];
statusCode = HttpStatusCode.OK;
}
else
{
statusCode = HttpStatusCode.Unauthorized;
}
await FacebookHelper.WriteAsync(response, statusCode, challenge, Encoding.UTF8, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Posts webhook control events to Facebook.
/// </summary>
/// <param name="postType">The REST post type (GET, PUT, POST, etc).</param>
/// <param name="content">The string content to be posted to Facebook.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>`true` if the operation succeeded; otherwise, `false`.</returns>
/// <exception cref="ArgumentNullException"><paramref name="postType"/> or <paramref name="content"/> is null.</exception>
public virtual async Task<bool> PostToFacebookApiAsync(string postType, string content, CancellationToken cancellationToken)
{
if (postType == null)
{
throw new ArgumentNullException(nameof(postType));
}
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
var graphApiBaseUrl = $"https://{_options.FacebookApiHost}/{_options.FacebookApiVersion + postType}?access_token={_options.FacebookAccessToken}";
var requestPath = string.Format(System.Globalization.CultureInfo.InvariantCulture, graphApiBaseUrl, postType, _options.FacebookAccessToken);
var stringContent = new StringContent(content, Encoding.UTF8, "application/json");
using (var requestMessage = new HttpRequestMessage())
{
requestMessage.Method = new HttpMethod("POST");
requestMessage.RequestUri = new Uri(requestPath);
requestMessage.Content = stringContent;
requestMessage.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
using (var client = new HttpClient())
{
var res = await client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
return res.IsSuccessStatusCode;
}
}
}
/// <summary>
/// Sends the request_thread_control webhook event to Facebook.
/// </summary>
/// <param name="userId">The sender user ID.</param>
/// <param name="message">An optional message for the metadata parameter.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>`true` if the operation succeeded; otherwise, `false`.</returns>
/// <exception cref="ArgumentNullException"><paramref name="userId"/> is null.</exception>
public virtual async Task<bool> RequestThreadControlAsync(string userId, string message, CancellationToken cancellationToken)
{
if (userId == null)
{
throw new ArgumentNullException(nameof(userId));
}
var content = new { recipient = new { id = userId }, metadata = message };
return await PostToFacebookApiAsync($"/me/{HandoverConstants.RequestThreadControl}", JsonConvert.SerializeObject(content), cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Sends the take_thread_control webhook event to Facebook.
/// </summary>
/// <param name="userId">The sender user ID.</param>
/// <param name="message">An optional message for the metadata parameter.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>`true` if the operation succeeded; otherwise, `false`.</returns>
/// <exception cref="ArgumentNullException"><paramref name="userId"/> is null.</exception>
public virtual async Task<bool> TakeThreadControlAsync(string userId, string message, CancellationToken cancellationToken)
{
if (userId == null)
{
throw new ArgumentNullException(nameof(userId));
}
var content = new { recipient = new { id = userId }, metadata = message };
return await PostToFacebookApiAsync($"/me/{HandoverConstants.TakeThreadControl}", JsonConvert.SerializeObject(content), cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Sends the pass_thread_control webhook event to Facebook.
/// </summary>
/// <param name="targetAppId">The ID of the target app to pass control to.</param>
/// <param name="userId">The sender user ID.</param>
/// <param name="message">An optional message for the metadata parameter.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>`true` if the operation succeeded; otherwise, `false`.</returns>
/// <exception cref="ArgumentNullException"><paramref name="targetAppId"/> or <paramref name="userId"/> is null.</exception>
public virtual async Task<bool> PassThreadControlAsync(string targetAppId, string userId, string message, CancellationToken cancellationToken)
{
if (targetAppId == null)
{
throw new ArgumentNullException(nameof(targetAppId));
}
if (userId == null)
{
throw new ArgumentNullException(nameof(userId));
}
var content = new { recipient = new { id = userId }, target_app_id = targetAppId, metadata = message };
return await PostToFacebookApiAsync($"/me/{HandoverConstants.PassThreadControl}", JsonConvert.SerializeObject(content), cancellationToken).ConfigureAwait(false);
}
}
}