-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2WebExtension.cs
More file actions
599 lines (522 loc) · 21.9 KB
/
OAuth2WebExtension.cs
File metadata and controls
599 lines (522 loc) · 21.9 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// This file is part of the DisCatSharp project.
//
// Copyright (c) 2021-2025 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DisCatSharp.Common.Utilities;
using DisCatSharp.Entities;
using DisCatSharp.Entities.OAuth2;
using DisCatSharp.Extensions.OAuth2Web.EventArgs;
using DisCatSharp.Extensions.OAuth2Web.EventHandling;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace DisCatSharp.Extensions.OAuth2Web;
/// <summary>
/// Represents a <see cref="OAuth2WebExtension" />.
/// </summary>
public sealed class OAuth2WebExtension : BaseExtension
{
/// <summary>
/// Fired when an access token was refreshed.
/// </summary>
private readonly AsyncEvent<DiscordOAuth2Client, AccessTokenRefreshEventArgs> _accessTokenRefreshed;
/// <summary>
/// Fired when an access token was revoked.
/// </summary>
private readonly AsyncEvent<DiscordOAuth2Client, AccessTokenRevokeEventArgs> _accessTokenRevoked;
/// <summary>
/// Fired when an authorizaton code was exchanged.
/// </summary>
private readonly AsyncEvent<DiscordOAuth2Client, AuthorizationCodeExchangeEventArgs> _authorizationCodeExchanged;
/// <summary>
/// Fired when an authorizaton code was received.
/// </summary>
private readonly AsyncEvent<DiscordOAuth2Client, AuthorizationCodeReceiveEventArgs> _authorizationCodeReceived;
/// <summary>
/// Gets the authorization code event waiter.
/// </summary>
private readonly AuthorizationCodeEventWaiter _authorizationCodeWaiter;
/// <summary>
/// Initializes a new instance of the <see cref="OAuth2WebExtension" /> class.
/// </summary>
/// <param name="configuration">The config.</param>
internal OAuth2WebExtension(OAuth2WebConfiguration configuration) // , DiscordClient discordClient)
{
this.Configuration = configuration;
this.OAuth2Client = new(this.Configuration.ClientId, this.Configuration.ClientSecret, this.Configuration.RedirectUri, this.ServiceProvider, this.Configuration.Proxy, default, default, default, this.Configuration.MinimumLogLevel, this.Configuration.LogTimestampFormat); // , discordClient: discordClient);
this._authorizationCodeReceived = new("OAUTH2_AUTH_CODE_RECEIVED", TimeSpan.Zero, this.OAuth2Client.EventErrorHandler);
this._authorizationCodeExchanged = new("OAUTH2_AUTH_CODE_EXCHANGED", TimeSpan.Zero, this.OAuth2Client.EventErrorHandler);
this._accessTokenRefreshed = new("OAUTH2_ACCESS_TOKEN_REFRESHED", TimeSpan.Zero, this.OAuth2Client.EventErrorHandler);
this._accessTokenRevoked = new("OAUTH2_ACCESS_TOKEN_REVOKED", TimeSpan.Zero, this.OAuth2Client.EventErrorHandler);
this.ServiceProvider = configuration.ServiceProvider;
this._authorizationCodeWaiter = new(this, this.OAuth2Client);
this.AuthorizationCodeExchanged += this.OnAuthorizationCodeExchangedAsync;
this.AccessTokenRefreshed += this.OnAccessTokenRefreshedAsync;
this.AccessTokenRevoked += this.OnAccessTokenRevokedAsync;
var builder = WebApplication.CreateBuilder();
builder.Services.AddRouting();
builder.Services.AddAuthorization();
builder.WebHost.UseUrls(this.Configuration.ListenAll
? $"http://*:{this.Configuration.StartPort}"
: $"http://127.0.0.1:{this.Configuration.StartPort}");
this.WEB_APP = builder.Build();
this.WEB_APP.UseRouting();
this.WEB_APP.UseAuthorization();
this.WEB_APP.MapGet("/oauth/{shard}/", this.HandleOAuth2Async);
this.WEB_APP.MapGet("/oauth/", this.HandleOAuth2Async);
}
/// <summary>
/// Gets the logger for this extension.
/// </summary>
public ILogger<BaseDiscordClient> Logger { get; set; }
/// <summary>
/// Gets the OAuth2 Web configuration.
/// </summary>
internal OAuth2WebConfiguration Configuration { get; }
/// <summary>
/// Gets the oauth2 client.
/// </summary>
public DiscordOAuth2Client OAuth2Client { get; }
/// <summary>
/// Gets the web application.
/// </summary>
private WebApplication WEB_APP { get; }
/// <summary>
/// Gets the service provider this OAuth2 Web module was configured with.
/// </summary>
public IServiceProvider ServiceProvider { get; private set; }
/// <summary>
/// All pending OAuth2 request urls.
/// </summary>
internal List<string> OAuth2RequestUrls { get; } = [];
/// <summary>
/// Gets all access tokens mapped to user id.
/// </summary>
internal ConcurrentDictionary<ulong, DiscordAccessToken> UserIdAccessTokenMapper { get; } = new();
/// <summary>
/// Triggered when an authorizaton code was received.
/// </summary>
public event AsyncEventHandler<DiscordOAuth2Client, AuthorizationCodeReceiveEventArgs> AuthorizationCodeReceived
{
add => this._authorizationCodeReceived.Register(value);
remove => this._authorizationCodeReceived.Unregister(value);
}
/// <summary>
/// Triggered when an authorizaton code was exchanged.
/// </summary>
public event AsyncEventHandler<DiscordOAuth2Client, AuthorizationCodeExchangeEventArgs> AuthorizationCodeExchanged
{
add => this._authorizationCodeExchanged.Register(value);
remove => this._authorizationCodeExchanged.Unregister(value);
}
/// <summary>
/// Triggered when an access token was refreshed.
/// </summary>
public event AsyncEventHandler<DiscordOAuth2Client, AccessTokenRefreshEventArgs> AccessTokenRefreshed
{
add => this._accessTokenRefreshed.Register(value);
remove => this._accessTokenRefreshed.Unregister(value);
}
/// <summary>
/// Triggered when an access token was revoked.
/// </summary>
public event AsyncEventHandler<DiscordOAuth2Client, AccessTokenRevokeEventArgs> AccessTokenRevoked
{
add => this._accessTokenRevoked.Register(value);
remove => this._accessTokenRevoked.Unregister(value);
}
/// <summary>
/// Gets an access token for <paramref name="user" />.
/// </summary>
/// <param name="user">The user to get the token from.</param>
/// <param name="token">The found access token.</param>
/// <returns>Whether an access token was found.</returns>
public bool TryGetAccessToken(DiscordUser user, out DiscordAccessToken? token)
=> this.TryGetAccessToken(user.Id, out token);
/// <summary>
/// Gets an access token for <paramref name="userId" />.
/// </summary>
/// <param name="userId">The user id to get the token from.</param>
/// <param name="token">The found access token.</param>
/// <returns>Whether an access token was found.</returns>
public bool TryGetAccessToken(ulong userId, out DiscordAccessToken? token)
=> this.UserIdAccessTokenMapper.TryGetValue(userId, out token);
/// <summary>
/// Refreshes an access token for <paramref name="user" />.
/// <para>Fires an <see cref="AccessTokenRefreshed" /> event.</para>
/// </summary>
/// <param name="user">The user to revoke their token from.</param>
public Task<bool> RefreshAccessTokenAsync(DiscordUser user)
=> this.RefreshAccessTokenAsync(user.Id);
/// <summary>
/// Revokes an access token for <paramref name="user" />.
/// <para>Fires an <see cref="AccessTokenRevoked" /> event.</para>
/// </summary>
/// <param name="user">The user to revoke their token from.</param>
public Task<bool> RevokeAccessTokenAsync(DiscordUser user)
=> this.RevokeAccessTokenAsync(user.Id);
/// <summary>
/// Refreshes an access token for <paramref name="userId" />.
/// <para>Fires an <see cref="AccessTokenRefreshed" /> event.</para>
/// </summary>
/// <param name="userId">The user id to revoke their token from.</param>
public async Task<bool> RefreshAccessTokenAsync(ulong userId)
{
if (!this.UserIdAccessTokenMapper.TryGetValue(userId, out var token))
return false;
await this.RefreshAccessTokenAsync(token, userId);
return true;
}
/// <summary>
/// Revokes an access token for <paramref name="userId" />.
/// <para>Fires an <see cref="AccessTokenRevoked" /> event.</para>
/// </summary>
/// <param name="userId">The user id to revoke their token from.</param>
public async Task<bool> RevokeAccessTokenAsync(ulong userId)
{
if (!this.UserIdAccessTokenMapper.TryGetValue(userId, out var token))
return false;
await this.RevokeAccessTokenAsync(token, userId);
return true;
}
/// <summary>
/// Refreshes all access tokens.
/// <para>Fires an <see cref="AccessTokenRefreshed" /> event for every refreshed token.</para>
/// </summary>
public async Task RefreshAllAccessTokensAsync()
{
foreach (var mappedToken in this.UserIdAccessTokenMapper)
await this.RefreshAccessTokenAsync(mappedToken.Value, mappedToken.Key);
}
/// <summary>
/// Revokes all access tokens.
/// <para>Fires an <see cref="AccessTokenRevoked" /> event for every refreshed token.</para>
/// </summary>
public async Task RevokeAllAccessTokensAsync()
{
foreach (var mappedToken in this.UserIdAccessTokenMapper)
await this.RevokeAccessTokenAsync(mappedToken.Value, mappedToken.Key);
}
/// <summary>
/// Handles access token revokes.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event args.</param>
private Task OnAccessTokenRevokedAsync(DiscordOAuth2Client sender, AccessTokenRevokeEventArgs e)
{
this.UserIdAccessTokenMapper.TryRemove(e.UserId, out _);
if (this.Client.UserCache.TryGetValue(e.UserId, out var user))
user.AccessToken = null;
e.Handled = false;
return Task.CompletedTask;
}
/// <summary>
/// Handles access token refreshes.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event args.</param>
private Task OnAccessTokenRefreshedAsync(DiscordOAuth2Client sender, AccessTokenRefreshEventArgs e)
{
_ = this.UserIdAccessTokenMapper.AddOrUpdate(e.UserId, e.RefreshedDiscordAccessToken, (id, old) =>
{
old.AccessToken = e.RefreshedDiscordAccessToken.AccessToken;
old.ExpiresIn = e.RefreshedDiscordAccessToken.ExpiresIn;
old.RefreshToken = e.RefreshedDiscordAccessToken.RefreshToken;
old.Scope = e.RefreshedDiscordAccessToken.Scope;
old.TokenType = e.RefreshedDiscordAccessToken.TokenType;
return old;
});
if (this.Client.UserCache.TryGetValue(e.UserId, out var user))
user.AccessToken = e.RefreshedDiscordAccessToken;
e.Handled = false;
return Task.CompletedTask;
}
/// <summary>
/// Handles authorization code exchanges.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event args.</param>
private Task OnAuthorizationCodeExchangedAsync(DiscordOAuth2Client sender, AuthorizationCodeExchangeEventArgs e)
{
_ = this.UserIdAccessTokenMapper.AddOrUpdate(e.UserId, e.DiscordAccessToken, (id, old) =>
{
old.AccessToken = e.DiscordAccessToken.AccessToken;
old.ExpiresIn = e.DiscordAccessToken.ExpiresIn;
old.RefreshToken = e.DiscordAccessToken.RefreshToken;
old.Scope = e.DiscordAccessToken.Scope;
old.TokenType = e.DiscordAccessToken.TokenType;
return old;
});
if (this.Client.UserCache.TryGetValue(e.UserId, out var user))
user.AccessToken = e.DiscordAccessToken;
e.Handled = false;
return Task.CompletedTask;
}
/// <summary>
/// DO NOT USE THIS MANUALLY.
/// </summary>
/// <param name="client">DO NOT USE THIS MANUALLY.</param>
/// <exception cref="InvalidOperationException" />
protected internal override void Setup(DiscordClient client)
{
if (this.Client != null)
throw new InvalidOperationException("What did I tell you?");
this.Client = client;
var a = typeof(OAuth2WebExtension).GetTypeInfo().Assembly;
var iv = a.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if (iv != null)
this.VersionString = iv.InformationalVersion;
else
{
var v = a.GetName().Version;
var vs = v.ToString(3);
if (v.Revision > 0)
this.VersionString = $"{vs}, CI build {v.Revision}";
}
this.HasVersionCheckSupport = true;
this.RepositoryOwner = "Aiko-IT-Systems";
this.Repository = "DisCatSharp.Extensions";
this.PackageId = "DisCatSharp.Extensions.OAuth2Web";
this.Logger = client.Logger;
this.OAuth2Client.DiscordConfiguration = client.Configuration;
this.ServiceProvider = this.Configuration.ServiceProvider;
}
/// <summary>
/// Starts the web server.
/// </summary>
public void Start()
=> Task.Run(() => this.WEB_APP.RunAsync());
/// <summary>
/// Adds a url to pending urls.
/// </summary>
/// <param name="uri">The url to add.</param>
public void SubmitPendingOAuth2Url(Uri uri)
=> this.OAuth2RequestUrls.Add(uri.AbsoluteUri);
/// <summary>
/// Generates an OAuth2 url and ads it to the pending urls.
/// </summary>
/// <param name="userId">The user to generate the url for.</param>
/// <param name="scopes">The scopes to request.</param>
/// <param name="suppressPrompt">Whether to suppress the prompt. Works only if previously authorized with same scopes.</param>
/// <returns></returns>
public Uri GenerateOAuth2Url(ulong userId, IEnumerable<string> scopes, bool suppressPrompt = false)
{
var generatedUrl = this.OAuth2Client.GenerateOAuth2Url(string.Join(' ', scopes), this.Configuration.SecureStates ? this.OAuth2Client.GenerateSecureState(userId) : this.OAuth2Client.GenerateState(), suppressPrompt);
this.SubmitPendingOAuth2Url(generatedUrl);
return generatedUrl;
}
/// <summary>
/// Waits for an access token.
/// <para>Make sure to submit <paramref name="uri" /> to <see cref="SubmitPendingOAuth2Url" /> before calling.</para>
/// </summary>
/// <param name="user">The user to wait for.</param>
/// <param name="uri">
/// The oauth url generated from <see cref="DiscordOAuth2Client.GenerateOAuth2Url" /> or
/// <see cref="GenerateOAuth2Url" /> to wait for.
/// </param>
/// <param name="token">A custom cancellation token that can be cancelled at any point.</param>
public async Task<OAuth2Result<AuthorizationCodeExchangeEventArgs>> WaitForAccessTokenAsync(DiscordUser user, Uri uri, CancellationToken token)
{
var state = this.OAuth2Client.GetStateFromUri(uri);
var res = await this._authorizationCodeWaiter
.WaitForMatchAsync(new(state,
x => x.UserId == user.Id, token)).ConfigureAwait(false);
return new(res is null, res!);
}
/// <summary>
/// Waits for an access token.
/// <para>Make sure to submit <paramref name="uri" /> to <see cref="SubmitPendingOAuth2Url" /> before calling.</para>
/// </summary>
/// <param name="user">The user to wait for.</param>
/// <param name="uri">The oauth url generated from <see cref="DiscordOAuth2Client.GenerateOAuth2Url" /> to wait for.</param>
/// <param name="timeoutOverride">Override the timeout period of one minute.</param>
public Task<OAuth2Result<AuthorizationCodeExchangeEventArgs>> WaitForAccessTokenAsync(DiscordUser user, Uri uri, TimeSpan? timeoutOverride = null)
=> this.WaitForAccessTokenAsync(user, uri, GetCancellationToken(timeoutOverride));
/// <summary>
/// Refreshes an access token.
/// </summary>
/// <param name="accessToken">The access token to refresh.</param>
/// <param name="userId">The user id the access token belongs to.</param>
internal async Task RefreshAccessTokenAsync(DiscordAccessToken accessToken, ulong userId)
{
var freshToken = await this.OAuth2Client.RefreshAccessTokenAsync(accessToken);
_ = Task.Run(() => this._accessTokenRefreshed.InvokeAsync(this.OAuth2Client, new(this.ServiceProvider)
{
RefreshedDiscordAccessToken = freshToken,
UserId = userId
}));
}
/// <summary>
/// Revokes an access token.
/// </summary>
/// <param name="accessToken">The access token to revoke.</param>
/// <param name="userId">The user id the access token belongs to.</param>
internal async Task RevokeAccessTokenAsync(DiscordAccessToken accessToken, ulong userId)
{
await this.OAuth2Client.RevokeByAccessTokenAsync(accessToken);
await this.OAuth2Client.RevokeByRefreshTokenAsync(accessToken);
_ = Task.Run(() => this._accessTokenRevoked.InvokeAsync(this.OAuth2Client, new(this.ServiceProvider)
{
RevokedDiscordAccessToken = accessToken,
UserId = userId
}));
}
/// <summary>
/// Handles the OAuth2 authorization code exchange.
/// </summary>
/// <param name="context">The http context.</param>
/// <param name="shard">The shard id.</param>
private async Task HandleOAuth2Async(HttpContext context, string? shard = null)
{
try
{
Uri requestUrl = new(context.Request.GetDisplayUrl());
var shardId = 0;
if (shard is not null)
shardId = Convert.ToInt32(shard!.ToCharArray()[1]);
if (!this.OAuth2RequestUrls.Any(u =>
this.OAuth2Client.ValidateState(new(u), requestUrl, this.Configuration.SecureStates)))
{
context.Response.StatusCode = 500;
if (this.Configuration is { UseHtmlOutput: true, HtmlOutputInvalidState: not null })
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(this.Configuration.HtmlOutputInvalidState, Encoding.UTF8);
}
else
{
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(
"{ \"handled\": false, \"error\": true, \"message\": \"Invalid state\" }");
}
return;
}
var code = this.OAuth2Client.GetCodeFromUri(requestUrl);
var state = this.OAuth2Client.GetStateFromUri(requestUrl);
await this._authorizationCodeReceived.InvokeAsync(this.OAuth2Client,
new(this.ServiceProvider)
{
ReceivedCode = code,
ReceivedState = state
});
var accessToken = await this.OAuth2Client.ExchangeAccessTokenAsync(code);
var info = await this.OAuth2Client.GetCurrentAuthorizationInformationAsync(accessToken);
if (this.Configuration.SecureStates)
{
var stateUserId = ulong.Parse(this.OAuth2Client.ReadSecureState(state).Split("::")[1]);
if (stateUserId != info.User?.Id)
{
this.Logger.LogCritical("OAuth2Web::SecurityException - Received user id {receivedUserId} does not matches authorized user id {authorizedUserId} or authorized is null.", stateUserId, info.User?.Id);
throw new SecurityException("State mismatch");
}
}
var targetPending = this.OAuth2RequestUrls.First(u => this.OAuth2Client.ValidateState(new(u), requestUrl, this.Configuration.SecureStates));
this.OAuth2RequestUrls.Remove(targetPending);
if (info.User is not null)
{
this.UserIdAccessTokenMapper.TryAdd(info.User.Id, accessToken);
if (this.Client.UserCache.TryGetValue(info.User.Id, out var value))
value.AccessToken = accessToken;
}
_ = Task.Run(async () => await this._authorizationCodeExchanged.InvokeAsync(this.OAuth2Client,
new(this.ServiceProvider)
{
ExchangedCode = code,
ReceivedState = state,
DiscordAccessToken = accessToken,
UserId = info.User!.Id
}));
context.Response.StatusCode = 200;
if (this.Configuration is { UseHtmlOutput: true, HtmlOutputSuccess: not null })
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(this.Configuration.HtmlOutputSuccess, Encoding.UTF8);
}
else
{
context.Response.ContentType = "application/json";
await context.Response.WriteAsync("{ \"handled\": true, \"error\": false, \"message\": \"You can close this tab now\" }");
}
}
catch (SecurityException ex)
{
_ = Task.Run(() => this.OAuth2Client.OAuth2ClientErroredInternal.InvokeAsync(this.OAuth2Client,
new(this.ServiceProvider)
{
EventName = "HandleOAuth2Async",
Exception = ex
}));
context.Response.StatusCode = 401;
if (this.Configuration is { UseHtmlOutput: true, HtmlOutputSecurityException: not null })
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(this.Configuration.HtmlOutputSecurityException, Encoding.UTF8);
}
else
{
context.Response.ContentType = "application/json";
await context.Response.WriteAsync("{ \"handled\": false, \"error\": true, \"message\": \"Security Exception\" }");
}
}
catch (Exception ex)
{
_ = Task.Run(() => this.OAuth2Client.OAuth2ClientErroredInternal.InvokeAsync(this.OAuth2Client,
new(this.ServiceProvider)
{
EventName = "HandleOAuth2Async",
Exception = ex
}));
context.Response.StatusCode = 500;
if (this.Configuration is { UseHtmlOutput: true, HtmlOutputException: not null })
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(this.Configuration.HtmlOutputException, Encoding.UTF8);
}
else
{
context.Response.ContentType = "application/json";
await context.Response.WriteAsync("{ \"handled\": false, \"error\": true, \"message\": \"Something went wrong\" }");
}
}
}
/// <summary>
/// Gets the cancellation token.
/// </summary>
/// <param name="timeout">The timeout.</param>
private static CancellationToken GetCancellationToken(TimeSpan? timeout = null)
=> new CancellationTokenSource(timeout ?? DiscordOAuth2Client.EventExecutionLimit).Token;
/// <summary>
/// Stops the web server.
/// </summary>
public Task StopAsync()
=> this.WEB_APP.StopAsync();
}