Skip to content

Commit

Permalink
feat(oauth2web): redirect uri checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Dec 8, 2023
1 parent 72d8177 commit fb31d3f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
32 changes: 30 additions & 2 deletions DisCatSharp.Extensions.OAuth2Web/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using System.Linq;
using System.Threading.Tasks;

using DisCatSharp.Entities;

using Microsoft.Extensions.DependencyInjection;

// ReSharper disable NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
Expand Down Expand Up @@ -73,8 +75,7 @@ public static async Task<IReadOnlyDictionary<int, OAuth2WebExtension>> UseOAuth2
var oa2W = shard.GetExtension<OAuth2WebExtension>();
oa2W ??= shard.UseOAuth2Web(new(config)
{
StartPort = currentPort,
RedirectUri = $"{baseRedirectUri}s{shard.ShardId}/"
StartPort = currentPort, RedirectUri = $"{baseRedirectUri}s{shard.ShardId}/"
});
currentPort++;

Expand Down Expand Up @@ -124,4 +125,31 @@ public static async Task StopAsync(this IReadOnlyDictionary<int, OAuth2WebExtens
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>
/// 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);
}
4 changes: 2 additions & 2 deletions DisCatSharp.Extensions.OAuth2Web/OAuth2WebConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public class OAuth2WebConfiguration
/// <para>If used in sharding, the redirect uri internally appends <see cref="DisCatSharp.DiscordClient.ShardId"/></para>
/// <example>
/// Example: Sharded redirect uri:
/// <para>Shard 1 will have the uri <c>http://localhost:<see cref="StartPort">StartPort</see>/oauth/s1/</c>.</para>
/// <para>Shard 2 will have the uri <c>http://localhost:<see cref="StartPort">StartPort+1</see>/oauth/s2/</c>.</para>
/// <para>Shard 1 will have the uri <c>http://<see cref="ProxyTargetIpOrHost">Host</see>:<see cref="StartPort">StartPort</see>/oauth/s1/</c>.</para>
/// <para>Shard 2 will have the uri <c>http://<see cref="ProxyTargetIpOrHost">Host</see>:<see cref="StartPort">StartPort+1</see>/oauth/s2/</c>.</para>
/// </example>
/// </summary>
public string RedirectUri { internal get; init; }
Expand Down

0 comments on commit fb31d3f

Please sign in to comment.