-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitchApiAdapter.cs
executable file
·341 lines (265 loc) · 12.8 KB
/
TwitchApiAdapter.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using TwitchLib.Api.Core;
using TwitchLib.Api.Core.Common;
using TwitchLib.Api.Core.Enums;
using TwitchLib.Api.Core.Exceptions;
using TwitchLib.Api.Core.Interfaces;
using TwitchLib.Api.Core.RateLimiter;
using TwitchLib.Api.Core.HttpCallHandlers;
using TwitchLib.Api.Auth;
using TwitchLib.Api.Helix;
using TwitchLib.Api.Helix.Models.Predictions.CreatePrediction;
using Hearthstone_Deck_Tracker.Utility.Logging;
using Nito.AsyncEx;
namespace TwitchPredictionsBG
{
public class AuthExtended : Auth
{
public AuthExtended(IApiSettings settings, IRateLimiter rateLimiter, IHttpCallHandler http) : base(settings, rateLimiter, http)
{
}
new public Task<AuthCodeResponse> GetAccessTokenFromCodeAsync(string code, string clientSecret, string redirectUri, string clientId = null)
{
var internalClientId = clientId;
if (string.IsNullOrWhiteSpace(code))
throw new BadParameterException("The code is not valid. It is not allowed to be null, empty or filled with whitespaces.");
if (string.IsNullOrWhiteSpace(clientSecret))
throw new BadParameterException("The client secret is not valid. It is not allowed to be null, empty or filled with whitespaces.");
if (string.IsNullOrWhiteSpace(redirectUri))
throw new BadParameterException("The redirectUri is not valid. It is not allowed to be null, empty or filled with whitespaces.");
if (string.IsNullOrWhiteSpace(internalClientId))
throw new BadParameterException("The clientId is not valid. It is not allowed to be null, empty or filled with whitespaces.");
var getParams = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("code", code),
new KeyValuePair<string, string>("client_id", internalClientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
new KeyValuePair<string, string>("redirect_uri", redirectUri)
};
return TwitchPostGenericAsync<AuthCodeResponse>("/oauth2/token", ApiVersion.V5, null, getParams, customBase: "https://id.twitch.tv");
}
}
public class TwitchApiAdapter
{
public const string CLIENT_ID = "00iimxu2jkbzhiei8bdsvz8kn1behn";
public const string CLIENT_SECRET = "en1k63d1mott4votunsinas5t729y9";
private IApiSettings settings;
private IRateLimiter rateLimiter;
private IHttpCallHandler http;
public Config config;
private TokenStorage tokenStorage;
private TwitchLibPredicitonsExtender predictions;
private Users users;
private Streams streams;
private AuthExtended auth;
public TwitchApiAdapter(Config config)
{
settings = new ApiSettings();
rateLimiter = new BypassLimiter();
http = new TwitchHttpClient();
this.config = config;
if (this.config.authToken != null)
{
settings.AccessToken = this.config.authToken.AccessToken;
}
settings.ClientId = CLIENT_ID;
settings.Secret = CLIENT_SECRET;
settings.SkipAutoServerTokenGeneration = true;
tokenStorage = new TokenStorage(config);
predictions = new TwitchLibPredicitonsExtender(settings, rateLimiter, http);
users = new Users(settings, rateLimiter, http);
streams = new Streams(settings, rateLimiter, http);
auth = new AuthExtended(settings, rateLimiter, http);
}
public string AuthScopeToString(AuthScopes scope)
{
switch (scope)
{
case AuthScopes.Helix_Channel_Manage_Predictions:
return "channel:manage:predictions";
case AuthScopes.Helix_Channel_Read_Predictions:
return "channel:read:predictions";
default:
return Helpers.AuthScopesToString(scope);
}
}
public string GetAuthorizationCodeUrl(string redirectUri, IEnumerable<AuthScopes> scopes, bool forceVerify = false, string state = null, string clientId = null)
{
var internalClientId = clientId;
string scopesStr = null;
foreach (var scope in scopes)
if (scopesStr == null)
scopesStr = AuthScopeToString(scope);
else
scopesStr += $"+{AuthScopeToString(scope)}";
if (string.IsNullOrWhiteSpace(internalClientId))
throw new BadParameterException("The clientId is not valid. It is not allowed to be null, empty or filled with whitespaces.");
return "https://id.twitch.tv/oauth2/authorize?" +
$"client_id={internalClientId}&" +
$"redirect_uri={System.Web.HttpUtility.UrlEncode(redirectUri)}&" +
"response_type=code&" +
$"scope={scopesStr}&" +
$"state={state}&" +
$"force_verify={forceVerify}";
}
public void OpenAuthorizationLink(string redirectUri, IEnumerable<AuthScopes> scopes, string clientId)
{
var uri = GetAuthorizationCodeUrl(redirectUri, scopes, clientId: clientId);
Process.Start(uri);
}
public async Task<TwitchApiTokenAdapter> HandleCodeRequest(string uri, string redirectUri, string clientId, string clientSecret)
{
var completionSource = new TaskCompletionSource<AuthCodeResponse>();
Action<string> codeToToken = async code => completionSource.TrySetResult(await auth.GetAccessTokenFromCodeAsync(code, clientSecret, redirectUri, clientId));
OauthCallbackListener.ServeCallback(uri, codeToToken);
Log.Info($"Waiting for token");
var tokenAdapter = new TwitchApiTokenAdapter(await completionSource.Task);
Log.Info($"Got token " + tokenAdapter.AccessToken);
tokenStorage.Store(tokenAdapter);
return tokenAdapter;
}
public async Task<TwitchApiTokenAdapter> FetchToken(string clientId, string clientSecret)
{
var token = tokenStorage.Retrieve();
if (token == null)
{
Log.Info($"Token not found");
return null;
}
Log.Info($"Token found");
var validateResponse = await auth.ValidateAccessTokenAsync(token.AccessToken);
if (validateResponse == null)
{
Log.Info($"Token invalid");
var refresh = await auth.RefreshAuthTokenAsync(token.RefreshToken, clientSecret, clientId);
token = new TwitchApiTokenAdapter(refresh);
tokenStorage.Store(token);
Log.Info($"Token refreshed");
}
this.settings.AccessToken = token.AccessToken;
this.config.user = await GetCurrentUser();
Log.Info($"User found {this.config.user.DisplayName}");
this.config.save();
return token;
}
public async Task<TwitchApiTokenAdapter> FetchOrCreateToken(string uri, string redirectUri, IEnumerable<AuthScopes> scopes, string clientId, string clientSecret)
{
var token = await FetchToken(clientId, clientSecret);
if (token != null)
{
return token;
} else
{
Log.Info("Open Authorization Link");
OpenAuthorizationLink(redirectUri, scopes, clientId);
Log.Info("Handling Code Request");
token = await HandleCodeRequest(uri, redirectUri, clientId, clientSecret);
}
this.settings.AccessToken = token.AccessToken;
tokenStorage.Store(token);
this.config.user = await GetCurrentUser();
Log.Info($"User authenticated {this.config.user.DisplayName}");
this.config.save();
return token;
}
public async Task<TwitchLib.Api.Helix.Models.Users.GetUsers.User> GetCurrentUser()
{
var response = await RefreshTokenIfExpired(async () => await this.users.GetUsersAsync());
return response.Users.First();
}
public async Task<Prediction> GetLatestPrediction()
{
var response = await RefreshTokenIfExpired(async () => await this.predictions.GetPredictions(this.config.user.Id));
if (response == null)
{
return null;
}
return response.Data.FirstOrDefault();
}
public async Task<Prediction> CreatePrediction(string title, string[] outcomeTitles, int predictionWindowSeconds)
{
var request = new CreatePredictionRequest();
request.BroadcasterId = this.config.user.Id;
request.Title = title;
request.PredictionWindowSeconds = predictionWindowSeconds;
request.Outcomes = new Outcome[]
{
new Outcome
{
Title = outcomeTitles[0]
},
new Outcome
{
Title = outcomeTitles[1]
},
};
Log.Info($"Token {settings.AccessToken} scopes {String.Join(", ", tokenStorage.Retrieve().Scopes.ToString())}");
var result = await RefreshTokenIfExpired(async () => await this.predictions.CreatePrediction(request));
return result.Data.First();
}
public async Task EndPrediction(string id, PredictionStatusEnum status, string winningOutcomeId)
{
Log.Info($"Token {settings.AccessToken} scopes {String.Join(", ", tokenStorage.Retrieve().Scopes.ToString())}");
await RefreshTokenIfExpired(async () => await this.predictions.EndPrediction(this.config.user.Id, id, status, winningOutcomeId));
}
public async Task<TwitchLib.Api.Helix.Models.Streams.GetStreams.Stream> GetStreamInfoForCurrentUser()
{
var result = await RefreshTokenIfExpired(async() => await streams.GetStreamsAsync(userIds: new List<string> { config.user.Id.ToString() }));
return result.Streams.FirstOrDefault();
}
private readonly AsyncLock _mutex = new AsyncLock();
public async Task<T> RefreshTokenIfExpired<T>(Func<Task<T>> wrappedFunction, string clientSecret = null, string clientId = null)
{
var token = tokenStorage.Retrieve();
if (token == null)
{
return default(T);
}
using (await _mutex.LockAsync())
{
try
{
return await wrappedFunction();
}
catch (Exception ex) when (ex is BadScopeException || ex is TokenExpiredException)
{
var refresh = await auth.RefreshAuthTokenAsync(token.RefreshToken, clientSecret ?? settings.Secret, clientId ?? settings.ClientId);
token = new TwitchApiTokenAdapter(refresh);
Log.Info($"Token after refresh {token.AccessToken} scopes {String.Join(", ", token.Scopes.ToString())}");
tokenStorage.Store(token);
settings.AccessToken = token.AccessToken;
config.authToken = token;
config.save();
}
}
return await wrappedFunction();
}
// Way to check for is live with delay?
// Create timer for delay, postpone prediction requests if stream became live send prediction requests
public async Task<bool> IsCurrentUserStreamLive()
{
if (config.debug)
{
return true;
}
return await GetStreamInfoForCurrentUser() != null;
}
public bool IsPredictionInProgress(Prediction prediction)
{
if (prediction == null)
{
return false;
}
if (prediction.Status == PredictionStatus.Active || prediction.Status == PredictionStatus.Locked)
{
return true;
}
return false;
}
}
}