-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
47 lines (40 loc) · 1.51 KB
/
Program.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
using SPWorldsApi.HttpClient;
using SPWorldsApi.Payments;
using SPWorldsApi.Users;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
namespace SPWorldsApi
{
public sealed class SPWorlds : PaymentWrapper
{
internal string token { get; }
public HttpRequest client { get; set; }
string BASE_URL { get; set; } = "https://spworlds.ru/api/public";
AuthenticationHeaderValue AuthHeader { get; set; }
public SPWorlds(string id, string token)
{
var BearerToken = $"{id}:{token}";
this.token = token;
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
AuthHeader = new("Bearer", Base64BearerToken);
client = new(BASE_URL, AuthHeader);
}
/// <summary>
/// Validating wenhook from site
/// </summary>
/// <param name="requestBody">Body of request</param>
/// <param name="base64Hash">X-Body-Hash</param>
/// <returns></returns>
public bool ValidateWebhook(string requestBody, string base64Hash)
{
byte[] requestData = Encoding.UTF8.GetBytes(requestBody);
using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(token)))
{
byte[] hashBytes = hmac.ComputeHash(requestData);
string computedHash = Convert.ToBase64String(hashBytes);
return base64Hash.Equals(computedHash);
}
}
}
}