-
Notifications
You must be signed in to change notification settings - Fork 1
/
EpicAccount.cs
98 lines (86 loc) · 3.77 KB
/
EpicAccount.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
namespace EricLauncher
{
class EpicAccount
{
private const string EXCHANGE_API_URL = "/account/api/oauth/exchange";
private const string VERIFY_API_URL = "/account/api/oauth/verify";
private const string LOGOUT_API_URL = "/account/api/oauth/sessions/kill";
public string? AccountId;
public string? DisplayName;
public string AccessToken;
public DateTime AccessExpiry;
public string RefreshToken;
public DateTime RefreshExpiry;
private HttpClient HTTPClient;
public EpicAccount(EpicLoginResponse login)
{
AccessToken = login.access_token!;
RefreshToken = login.refresh_token!;
AccessExpiry = login.expires_at!;
RefreshExpiry = login.refresh_expires_at!;
AccountId = login.account_id!;
DisplayName = login.displayName;
HTTPClient = new();
HTTPClient.BaseAddress = new Uri(EpicLogin.ACCOUNTS_API_BASE);
HTTPClient.DefaultRequestHeaders.Accept.Clear();
HTTPClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HTTPClient.DefaultRequestHeaders.Add("Authorization", login.token_type + " " + login.access_token);
}
public EpicAccount(StoredAccountInfo info)
{
if (info.AccessToken == null || info.RefreshToken == null)
throw new Exception("Stored account info doesn't have access or refresh token");
if (info.AccountId != null)
AccountId = info.AccountId;
if (info.DisplayName != null)
DisplayName = info.DisplayName;
AccessToken = info.AccessToken;
RefreshToken = info.RefreshToken;
AccessExpiry = info.AccessExpiry;
RefreshExpiry = info.RefreshExpiry;
HTTPClient = new();
HTTPClient.BaseAddress = new Uri(EpicLogin.ACCOUNTS_API_BASE);
HTTPClient.DefaultRequestHeaders.Accept.Clear();
HTTPClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HTTPClient.DefaultRequestHeaders.Add("Authorization", "bearer " + info.AccessToken);
}
public async Task<string> GetExchangeCode()
{
EpicExchangeResponse? resp = await HTTPClient.GetFromJsonAsync<EpicExchangeResponse>(EXCHANGE_API_URL);
return resp!.code!;
}
public async Task<bool> VerifyToken()
{
HttpResponseMessage resp = await HTTPClient.GetAsync(VERIFY_API_URL);
if (!resp.IsSuccessStatusCode)
return false;
EpicVerifyResponse? verify_response = await resp.Content.ReadFromJsonAsync<EpicVerifyResponse>();
// if the token is expiring soon, why bother amirite
return verify_response!.expires_in > 60;
}
public async Task<bool> Logout()
{
HttpResponseMessage resp = await HTTPClient.DeleteAsync($"{LOGOUT_API_URL}/{AccessToken}");
return resp.StatusCode == HttpStatusCode.NoContent || resp.StatusCode == HttpStatusCode.OK;
}
public StoredAccountInfo MakeStoredAccountInfo()
{
StoredAccountInfo info = new StoredAccountInfo();
info.AccountId = AccountId;
info.RefreshExpiry = RefreshExpiry;
info.RefreshToken = RefreshToken;
info.AccessExpiry = AccessExpiry;
info.AccessToken = AccessToken;
info.DisplayName = DisplayName;
return info;
}
}
}