Skip to content

Commit

Permalink
Remove claim for Discord avatar
Browse files Browse the repository at this point in the history
Rather than maintain logic to understand how Discord builds up avatar URLs and handles different image format, remove the computed "avatar URL" claim.
Consumers can instead author their own MapCustomJson on the ClaimActions and derive the location based on their own requirements.
  • Loading branch information
martincostello committed Aug 25, 2021
1 parent 023d2fb commit faa1149
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 30 deletions.
2 changes: 0 additions & 2 deletions docs/discord.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ _None._

| Property Name | Property Type | Description | Default Value |
|:--|:--|:--|:--|
| `DiscordAvatarFormat` | `string` | Gets or sets the URL format string to use for user avatar images. | `DiscordAuthenticationConstants.Urls.AvatarUrlFormat` |
| `DiscordCdn` | `string` | The URL to use for the Discord CDN. | `DiscordAuthenticationConstants.Urls.DiscordCdn` |
| `Prompt` | `string?` | The value to use for the `prompt` query string parameter when making HTTP requests to the authorization endpoint. | `null` |
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ namespace AspNet.Security.OAuth.Discord
/// </summary>
public static class DiscordAuthenticationConstants
{
public static class Urls
{
public const string DiscordCdn = "https://cdn.discordapp.com";
public const string AvatarUrlFormat = "{0}/avatars/{1}/{2}.png";
}

public static class Claims
{
public const string AvatarHash = "urn:discord:avatar:hash";
public const string AvatarUrl = "urn:discord:avatar:url";
public const string Discriminator = "urn:discord:user:discriminator";
}

Expand Down
20 changes: 0 additions & 20 deletions src/AspNet.Security.OAuth.Discord/DiscordAuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* for more information concerning the license and the contributors participating to this project.
*/

using System.Globalization;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;
Expand All @@ -17,18 +16,6 @@ namespace AspNet.Security.OAuth.Discord
/// </summary>
public class DiscordAuthenticationOptions : OAuthOptions
{
/// <summary>
/// Gets or sets the root Discord CDN URL path. The default value is <see cref="Urls.DiscordCdn"/>.
/// </summary>
public string DiscordCdn { get; set; } = Urls.DiscordCdn;

/// <summary>
/// Gets or sets the URL format string for the user avatar URL, using <see cref="string.Format(System.IFormatProvider?, string, object?[])"/>.
/// Substitute <c>{0}</c> for <see cref="DiscordCdn"/>, <c>{1}</c> for the user ID and <c>{2}</c> for the Avatar hash.
/// The default value is <see cref="Urls.AvatarUrlFormat"/>.
/// </summary>
public string DiscordAvatarFormat { get; set; } = Urls.AvatarUrlFormat;

/// <summary>
/// Gets or sets a value which controls how the authorization flow handles existing authorizations.
/// The default value of this property is <see langword="null"/> and the <c>prompt</c> query string
Expand All @@ -52,13 +39,6 @@ public DiscordAuthenticationOptions()
ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
ClaimActions.MapJsonKey(Claims.AvatarHash, "avatar");
ClaimActions.MapJsonKey(Claims.Discriminator, "discriminator");
ClaimActions.MapCustomJson(Claims.AvatarUrl, user =>
string.Format(
CultureInfo.InvariantCulture,
DiscordAvatarFormat,
DiscordCdn.TrimEnd('/'),
user.GetString("id"),
user.GetString("avatar")));

Scope.Add("identify");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ protected internal override void RegisterAuthentication(AuthenticationBuilder bu
[InlineData(ClaimTypes.Email, "john@john-smith.local")]
[InlineData(Claims.Discriminator, "1234")]
[InlineData(Claims.AvatarHash, "dummy-avatar-hash")]
[InlineData(Claims.AvatarUrl, "https://cdn.discordapp.com/avatars/my-id/dummy-avatar-hash.png")]
public async Task Can_Sign_In_Using_Discord(string claimType, string claimValue)
{
// Arrange
Expand Down

0 comments on commit faa1149

Please sign in to comment.