Skip to content

Commit af1e337

Browse files
zAfLukevinchalet
authored andcommitted
Fix the Twitch provider to use the correct user information endpoint and add new claims
1 parent 9796089 commit af1e337

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
namespace AspNet.Security.OAuth.Twitch
8+
{
9+
/// <summary>
10+
/// Contains constants specific to the <see cref="TwitchAuthenticationHandler"/>.
11+
/// </summary>
12+
public static class TwitchAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
public const string BroadcasterType = "urn:twitch:broadcastertype";
17+
public const string Description = "urn:twitch:description";
18+
public const string DisplayName = "urn:twitch:displayname";
19+
public const string OfflineImageUrl = "urn:twitch:offlineimageurl";
20+
public const string ProfileImageUrl = "urn:twitch:profileimageurl";
21+
public const string Type = "urn:twitch:type";
22+
}
23+
}
24+
}

src/AspNet.Security.OAuth.Twitch/TwitchAuthenticationDefaults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public static class TwitchAuthenticationDefaults
4747
/// <summary>
4848
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
4949
/// </summary>
50-
public const string UserInformationEndpoint = "https://api.twitch.tv/helix/user/";
50+
public const string UserInformationEndpoint = "https://api.twitch.tv/helix/users/";
5151
}
5252
}

src/AspNet.Security.OAuth.Twitch/TwitchAuthenticationOptions.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.AspNetCore.Authentication;
99
using Microsoft.AspNetCore.Authentication.OAuth;
1010
using Microsoft.AspNetCore.Http;
11+
using static AspNet.Security.OAuth.Twitch.TwitchAuthenticationConstants;
1112

1213
namespace AspNet.Security.OAuth.Twitch
1314
{
@@ -25,10 +26,52 @@ public TwitchAuthenticationOptions()
2526
TokenEndpoint = TwitchAuthenticationDefaults.TokenEndpoint;
2627
UserInformationEndpoint = TwitchAuthenticationDefaults.UserInformationEndpoint;
2728

28-
Scope.Add("user_read");
29+
Scope.Add("user:read:email");
2930

30-
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "_id");
31-
ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
31+
ClaimActions.MapCustomJson(ClaimTypes.NameIdentifier, user =>
32+
{
33+
return user["data"]?[0]?.Value<string>("id");
34+
});
35+
36+
ClaimActions.MapCustomJson(ClaimTypes.Name, user =>
37+
{
38+
return user["data"]?[0]?.Value<string>("login");
39+
});
40+
41+
ClaimActions.MapCustomJson(Claims.DisplayName, user =>
42+
{
43+
return user["data"]?[0]?.Value<string>("display_name");
44+
});
45+
46+
ClaimActions.MapCustomJson(ClaimTypes.Email, user =>
47+
{
48+
return user["data"]?[0]?.Value<string>("email");
49+
});
50+
51+
ClaimActions.MapCustomJson(Claims.Type, user =>
52+
{
53+
return user["data"]?[0]?.Value<string>("type");
54+
});
55+
56+
ClaimActions.MapCustomJson(Claims.BroadcasterType, user =>
57+
{
58+
return user["data"]?[0]?.Value<string>("broadcaster_type");
59+
});
60+
61+
ClaimActions.MapCustomJson(Claims.Description, user =>
62+
{
63+
return user["data"]?[0]?.Value<string>("description");
64+
});
65+
66+
ClaimActions.MapCustomJson(Claims.ProfileImageUrl, user =>
67+
{
68+
return user["data"]?[0]?.Value<string>("profile_image_url");
69+
});
70+
71+
ClaimActions.MapCustomJson(Claims.OfflineImageUrl, user =>
72+
{
73+
return user["data"]?[0]?.Value<string>("offline_image_url");
74+
});
3275
}
3376
}
3477
}

0 commit comments

Comments
 (0)