-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathPatreonCookieValidator.cs
46 lines (39 loc) · 1.85 KB
/
PatreonCookieValidator.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using NLog;
using UniversalDownloaderPlatform.Common.Exceptions;
using UniversalDownloaderPlatform.Common.Interfaces;
namespace PatreonDownloader.Implementation
{
internal class PatreonCookieValidator : ICookieValidator
{
private readonly IWebDownloader _webDownloader;
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public PatreonCookieValidator(IWebDownloader webDownloader)
{
_webDownloader = webDownloader ?? throw new ArgumentNullException(nameof(webDownloader));
}
public async Task ValidateCookies(CookieContainer cookieContainer)
{
if (cookieContainer == null)
throw new ArgumentNullException(nameof(cookieContainer));
CookieCollection cookies = cookieContainer.GetCookies(new Uri("https://patreon.com"));
if (cookies["__cf_bm"] == null)
throw new CookieValidationException("__cf_bm cookie not found");
if (cookies["session_id"] == null)
throw new CookieValidationException("session_id cookie not found");
if (cookies["patreon_device_id"] == null)
throw new CookieValidationException("patreon_device_id cookie not found");
if (cookies["datadome"] == null)
throw new CookieValidationException("datadome cookie not found");
string apiResponse = await _webDownloader.DownloadString("https://www.patreon.com/api/current_user");
if (apiResponse.ToLower(CultureInfo.InvariantCulture).Contains("\"status\":\"401\""))
throw new CookieValidationException("current_user api endpoint returned 401 Unauthorized");
}
}
}