Skip to content

Commit 24f5483

Browse files
Refactor
Simplify some custom action mappings. Remove unused using statement. Adjust whitespace to reduce diff. Remove TODO.
1 parent 621ecff commit 24f5483

File tree

6 files changed

+18
-74
lines changed

6 files changed

+18
-74
lines changed

src/AspNet.Security.OAuth.Automatic/AutomaticAuthenticationHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Microsoft.AspNetCore.WebUtilities;
1818
using Microsoft.Extensions.Logging;
1919
using Microsoft.Extensions.Options;
20-
using Newtonsoft.Json.Linq;
2120

2221
namespace AspNet.Security.OAuth.Automatic
2322
{

src/AspNet.Security.OAuth.Fitbit/FitbitAuthenticationHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] st
9090
}
9191

9292
var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
93+
9394
return OAuthTokenResponse.Success(payload);
9495
}
9596
}

src/AspNet.Security.OAuth.GitHub/GitHubAuthenticationHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ protected virtual async Task<string> GetEmailAsync([NotNull] OAuthTokenResponse
9292
return null;
9393
}
9494

95-
// TODO Review if this is correct for parsing a raw array
9695
using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
9796
{
9897
return (from address in payload.RootElement.EnumerateArray()

src/AspNet.Security.OAuth.Instagram/InstagramAuthenticationHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]
6767
using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
6868
{
6969
var principal = new ClaimsPrincipal(identity);
70-
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement.GetProperty("data"));
71-
context.RunClaimActions();
70+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
71+
context.RunClaimActions(payload.RootElement.GetProperty("data"));
7272

7373
await Options.Events.CreatingTicket(context);
7474
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);

src/AspNet.Security.OAuth.LinkedIn/LinkedInAuthenticationOptions.cs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,49 +39,16 @@ public LinkedInAuthenticationOptions()
3939
ClaimActions.MapJsonKey(Claims.Industry, "industry");
4040
ClaimActions.MapJsonKey(Claims.Summary, "summary");
4141
ClaimActions.MapJsonKey(Claims.Headline, "headline");
42+
ClaimActions.MapCustomJson(Claims.Positions, user => user.GetString("positions"));
4243
ClaimActions.MapJsonKey(Claims.PhoneticFirstName, "phoneticFirstName");
4344
ClaimActions.MapJsonKey(Claims.PhoneticLastName, "phoneticLastName");
4445
ClaimActions.MapJsonKey(Claims.FormattedPhoneticName, "formattedPhoneticName");
46+
ClaimActions.MapCustomJson(Claims.Location, user => user.GetString("location"));
4547
ClaimActions.MapJsonKey(Claims.Specialties, "specialties");
4648
ClaimActions.MapJsonKey(Claims.NumConnections, "numConnections");
4749
ClaimActions.MapJsonKey(Claims.NumConnectionsCapped, "numConnectionsCapped");
4850
ClaimActions.MapJsonKey(Claims.CurrentShare, "currentShare");
49-
50-
ClaimActions.MapCustomJson(
51-
Claims.Positions,
52-
user =>
53-
{
54-
if (user.TryGetProperty("positions", out var positions))
55-
{
56-
return positions.ToString();
57-
}
58-
59-
return null;
60-
});
61-
62-
ClaimActions.MapCustomJson(
63-
Claims.Location,
64-
user =>
65-
{
66-
if (user.TryGetProperty("location", out var positions))
67-
{
68-
return positions.ToString();
69-
}
70-
71-
return null;
72-
});
73-
74-
ClaimActions.MapCustomJson(
75-
Claims.PictureUrls,
76-
user =>
77-
{
78-
if (user.TryGetProperty("pictureUrls", out var positions))
79-
{
80-
return positions.ToString();
81-
}
82-
83-
return null;
84-
});
51+
ClaimActions.MapCustomJson(Claims.PictureUrls, user => user.GetString("pictureUrls"));
8552
}
8653

8754
/// <summary>

src/AspNet.Security.OAuth.Patreon/PatreonAuthenticationOptions.cs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
using System.Security.Claims;
8+
using System.Text.Json;
89
using Microsoft.AspNetCore.Authentication;
910
using Microsoft.AspNetCore.Authentication.OAuth;
1011
using Microsoft.AspNetCore.Http;
@@ -32,42 +33,19 @@ public PatreonAuthenticationOptions()
3233
Scope.Add("my-campaign");
3334

3435
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
36+
ClaimActions.MapCustomJson(ClaimTypes.Name, user => GetUserAttribute(user, "full_name"));
37+
ClaimActions.MapCustomJson(ClaimTypes.Webpage, user => GetUserAttribute(user, "url"));
38+
ClaimActions.MapCustomJson(Claims.Avatar, user => GetUserAttribute(user, "thumb_url"));
39+
}
3540

36-
ClaimActions.MapCustomJson(
37-
ClaimTypes.Name,
38-
user =>
39-
{
40-
if (user.TryGetProperty("attributes", out var attributes))
41-
{
42-
return attributes.GetString("full_name");
43-
}
44-
45-
return null;
46-
});
47-
48-
ClaimActions.MapCustomJson(
49-
ClaimTypes.Webpage,
50-
user =>
51-
{
52-
if (user.TryGetProperty("attributes", out var attributes))
53-
{
54-
return attributes.GetString("url");
55-
}
56-
57-
return null;
58-
});
59-
60-
ClaimActions.MapCustomJson(
61-
Claims.Avatar,
62-
user =>
63-
{
64-
if (user.TryGetProperty("attributes", out var attributes))
65-
{
66-
return attributes.GetString("thumb_url");
67-
}
41+
private static string GetUserAttribute(JsonElement user, string key)
42+
{
43+
if (!user.TryGetProperty("attributes", out var attributes))
44+
{
45+
return null;
46+
}
6847

69-
return null;
70-
});
48+
return attributes.GetString(key);
7149
}
7250
}
7351
}

0 commit comments

Comments
 (0)