-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdropbox-EventCallbackHelper.cs
61 lines (49 loc) · 1.92 KB
/
dropbox-EventCallbackHelper.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
using System;
using System.Security.Cryptography;
using System.Text;
using Dropbox.Sign.Client;
using Dropbox.Sign.Model;
namespace Dropbox.Sign
{
public class EventCallbackHelper
{
public const string EVENT_TYPE_ACCOUNT_CALLBACK = "account_callback";
public const string EVENT_TYPE_APP_CALLBACK = "app_callback";
private EventCallbackHelper() {}
/// <summary>
/// Verify that a callback came from HelloSign.com
/// </summary>
public static bool IsValid(string apiKey, EventCallbackRequest eventCallback)
{
var eventType = ClientUtils.GetEnumMemberAttrValue(eventCallback.Event.EventType);
var message = eventCallback.Event.EventTime + eventType;
return GetHash(message, apiKey) == eventCallback.Event.EventHash;
}
/// <summary>
/// Identifies the callback type, one of "account_callback" or "app_callback".
/// "app_callback" will always include a value for "reported_for_app_id"
/// </summary>
/// <param name="eventCallback"></param>
/// <returns></returns>
public static string GetCallbackType(EventCallbackRequest eventCallback)
{
var metaData = eventCallback.Event.EventMetadata;
if (metaData == null || String.IsNullOrEmpty(metaData.ReportedForAppId))
{
return EVENT_TYPE_ACCOUNT_CALLBACK;
}
return EVENT_TYPE_APP_CALLBACK;
}
private static string GetHash(string text, string key)
{
var encoding = new UTF8Encoding();
var textBytes = encoding.GetBytes(text);
var keyBytes = encoding.GetBytes(key);
var hash = new HMACSHA256(keyBytes);
var hashBytes = hash.ComputeHash(textBytes);
return BitConverter.ToString(hashBytes)
.Replace("-", "")
.ToLower();
}
}
}