Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove claim for Discord avatar #585

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions docs/discord.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ _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` |

## Avatars as Claims

Versions of the Discord provider before version `6.0.0` would automatically map the user's avatar URL as the `urn:discord:avatar:url` claim.

This functionality is no longer built-in (see [#584](https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers/issues/584) and [#585](https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers/pull/585)), but can be added to your application with some extra code similar to that shown in the sample below.

```csharp
services.AddAuthentication(options => /* Auth configuration */)
.AddDiscord(options =>
{
options.ClientId = "my-client-id";
options.ClientSecret = "my-client-secret";

options.ClaimActions.MapCustomJson("urn:discord:avatar:url", user =>
string.Format(
CultureInfo.InvariantCulture,
"https://cdn.discordapp.com/avatars/{0}/{1}.{2}",
user.GetString("id"),
user.GetString("avatar"),
user.GetString("avatar").StartsWith("a_") ? "gif" : "png"));
});
```
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