-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
164 lines (144 loc) · 7.3 KB
/
ExtensionMethods.cs
File metadata and controls
164 lines (144 loc) · 7.3 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
// 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.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
// ReSharper disable NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
namespace DisCatSharp.Extensions.OAuth2Web;
/// <summary>
/// Defines various extensions specific to OAuth2Web.
/// </summary>
public static class ExtensionMethods
{
/// <summary>
/// Enables OAuth2Web module on this <see cref="DiscordClient" />.
/// </summary>
/// <param name="client">Client to enable OAuth2Web for.</param>
/// <param name="config">OAuth2Web configuration to use.</param>
/// <returns>Created <see cref="OAuth2WebExtension" />.</returns>
public static OAuth2WebExtension UseOAuth2Web(this DiscordClient client, OAuth2WebConfiguration config)
{
if (client.GetExtension<OAuth2WebExtension>() != null)
throw new InvalidOperationException("OAuth2Web is already enabled for that client.");
config.ServiceProvider ??= client.ServiceProvider ?? new ServiceCollection().BuildServiceProvider(true);
var oa2W = new OAuth2WebExtension(config); // , client);
client.AddExtension(oa2W);
return oa2W;
}
/// <summary>
/// Enables OAuth2Web module on all shards in this <see cref="DiscordShardedClient" />.
/// </summary>
/// <param name="client">Client to enable OAuth2Web for.</param>
/// <param name="config">OAuth2Web configuration to use.</param>
/// <returns>A dictionary of created <see cref="OAuth2WebExtension" />, indexed by shard id.</returns>
public static async Task<IReadOnlyDictionary<int, OAuth2WebExtension>> UseOAuth2WebAsync(this DiscordShardedClient client, OAuth2WebConfiguration config)
{
var modules = new Dictionary<int, OAuth2WebExtension>();
await client.InitializeShardsAsync().ConfigureAwait(false);
var currentPort = config.StartPort;
var baseRedirectUri = config.RedirectUri;
foreach (var shard in client.ShardClients.Select(xkvp => xkvp.Value))
{
var oa2W = shard.GetExtension<OAuth2WebExtension>();
oa2W ??= shard.UseOAuth2Web(new(config)
{
StartPort = currentPort,
RedirectUri = $"{baseRedirectUri}s{shard.ShardId}/"
});
currentPort++;
modules[shard.ShardId] = oa2W;
}
return new ReadOnlyDictionary<int, OAuth2WebExtension>(modules);
}
/// <summary>
/// Gets the active OAuth2Web module for this client.
/// </summary>
/// <param name="client">Client to get OAuth2Web module from.</param>
/// <returns>The module, or null if not activated.</returns>
public static OAuth2WebExtension? GetOAuth2Web(this DiscordClient client)
=> client.GetExtension<OAuth2WebExtension>();
/// <summary>
/// Gets the active OAuth2Web modules for all shards in this client.
/// </summary>
/// <param name="client">Client to get OAuth2Web instances from.</param>
/// <returns>A dictionary of the modules, indexed by shard id.</returns>
public static async Task<IReadOnlyDictionary<int, OAuth2WebExtension?>> GetOAuth2WebAsync(this DiscordShardedClient client)
{
await client.InitializeShardsAsync().ConfigureAwait(false);
var extensions = client.ShardClients.Select(xkvp => xkvp.Value).ToDictionary(shard => shard.ShardId, shard => shard.GetExtension<OAuth2WebExtension>());
return new ReadOnlyDictionary<int, OAuth2WebExtension?>(extensions);
}
/// <summary>
/// Starts the oauth2 web server for all shards.
/// </summary>
/// <param name="extensions">The extension dictionary.</param>
public static void Start(this IReadOnlyDictionary<int, OAuth2WebExtension> extensions)
{
foreach (var extension in extensions.Values)
extension.Start();
}
/// <summary>
/// Stops the oauth2 web server for all shards.
/// </summary>
/// <param name="extensions">The extension dictionary.</param>
public static async Task StopAsync(this IReadOnlyDictionary<int, OAuth2WebExtension> extensions)
{
foreach (var extension in extensions.Values)
await extension.StopAsync();
}
/// <summary>
/// <para>Checks if all redirect uris are set for the application in the developer portal.</para>
/// <para>Use this function after you've executed <see cref="DiscordShardedClient.StartAsync" />.</para>
/// </summary>
/// <param name="extensions">The extensions.</param>
/// <param name="client">The <see cref="DiscordShardedClient" />.</param>
/// <returns>Whether all required redirect uris are set.</returns>
public static bool HasAllRequiredRedirectUrisSet(this IReadOnlyDictionary<int, OAuth2WebExtension> extensions, DiscordShardedClient client)
=> extensions.Values.Select(extension => extension.Configuration.RedirectUri).All(client.CurrentApplication.RedirectUris.Contains);
/// <summary>
/// <para>Checks if the redirect uri is set for the application in the developer portal.</para>
/// <para>Use this function after you've executed <see cref="DiscordClient.ConnectAsync" />.</para>
/// </summary>
/// <param name="extensions">The extensions.</param>
/// <param name="client">The <see cref="DiscordClient" />.</param>
/// <returns>Whether the required redirect uris is set.</returns>
public static bool HasRequiredRedirectUriSet(this OAuth2WebExtension extensions, DiscordClient client)
=> client.CurrentApplication.RedirectUris.Contains(extensions.Configuration.RedirectUri);
/// <summary>
/// Gets the required redirect uris for the developer portal.
/// </summary>
/// <param name="extensions">The extensions.</param>
/// <returns>The required redirect uris.</returns>
public static IReadOnlyList<string> GetRequiredRedirectUris(this IReadOnlyDictionary<int, OAuth2WebExtension> extensions)
=> extensions.Values.Select(extension => extension.Configuration.RedirectUri).ToList().AsReadOnly();
/// <summary>
/// <para>Checks if all redirect uris are set for the application in the developer portal.</para>
/// <para>Use this function after you've executed <see cref="DiscordClient.ConnectAsync" />.</para>
/// </summary>
/// <param name="extension">The extension.</param>
/// <returns>Whether the required redirect uri is set.</returns>
public static bool HasRequiredRedirectUriSet(this OAuth2WebExtension extension)
=> extension.Client.CurrentApplication.RedirectUris.Contains(extension.Configuration.RedirectUri);
}