Skip to content

Commit

Permalink
Replace JObject with JsonDocument in Authentication #4260
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Jan 28, 2019
1 parent 7d4b6fc commit 5b0da93
Show file tree
Hide file tree
Showing 31 changed files with 362 additions and 132 deletions.
37 changes: 37 additions & 0 deletions src/Security/Authentication/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
bin/
obj/
.vs/
.vscode/
*.suo
*.user
_ReSharper.*
*.DS_Store
*.userprefs
*.pidb
*.vspx
*.psess
*.binlog
*.log
artifacts/
StyleCop.Cache
node_modules/
*.snk
.nuget
.r
.w
.deps
msbuild.ProjectImports.zip
.env
scripts/tmp/
.dotnet/
.tools/
src/**/global.json
BenchmarkDotNet.Artifacts/
korebuild-lock.txt
.gradle/
src/SignalR/clients/**/dist/
modules/
!launchSettings.json

# Template config files for blazor templates is generated on-build
src/Components/**/.template.config/
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1780/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"CookieSample": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:1782/"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1771/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"CookieSessionSample": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:1776/"
}
}
}
4 changes: 2 additions & 2 deletions src/Security/Authentication/Facebook/src/FacebookHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;

namespace Microsoft.AspNetCore.Authentication.Facebook
{
Expand Down Expand Up @@ -41,7 +41,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIden
throw new HttpRequestException($"An error occurred when retrieving Facebook user information ({response.StatusCode}). Please check if the authentication information is correct and the corresponding Facebook Graph API is enabled.");
}

var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

var context = new OAuthCreatingTicketContext(new ClaimsPrincipal(identity), properties, Context, Scheme, Options, Backchannel, tokens, payload);
context.RunClaimActions();
Expand Down
4 changes: 2 additions & 2 deletions src/Security/Authentication/Google/src/GoogleHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;

namespace Microsoft.AspNetCore.Authentication.Google
{
Expand All @@ -37,7 +37,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
throw new HttpRequestException($"An error occurred when retrieving Google user information ({response.StatusCode}). Please check if the authentication information is correct and the corresponding Google+ API is enabled.");
}

var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

var context = new OAuthCreatingTicketContext(new ClaimsPrincipal(identity), properties, Context, Scheme, Options, Backchannel, tokens, payload);
context.RunClaimActions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://localhost:44318/",
"sslPort": 44318
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SocialSample": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:44318/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;

namespace Microsoft.AspNetCore.Authentication.MicrosoftAccount
{
Expand All @@ -30,7 +30,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIden
throw new HttpRequestException($"An error occurred when retrieving Microsoft user information ({response.StatusCode}). Please check if the authentication information is correct and the corresponding Microsoft Account API is enabled.");
}

var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

var context = new OAuthCreatingTicketContext(new ClaimsPrincipal(identity), properties, Context, Scheme, Options, Backchannel, tokens, payload);
context.RunClaimActions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.MicrosoftAccount;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Authentication.MicrosoftAccount
{
Expand All @@ -29,7 +27,19 @@ public MicrosoftAccountOptions()
ClaimActions.MapJsonKey(ClaimTypes.Name, "displayName");
ClaimActions.MapJsonKey(ClaimTypes.GivenName, "givenName");
ClaimActions.MapJsonKey(ClaimTypes.Surname, "surname");
ClaimActions.MapCustomJson(ClaimTypes.Email, user => user.Value<string>("mail") ?? user.Value<string>("userPrincipalName"));
ClaimActions.MapCustomJson(ClaimTypes.Email, user =>
{
var root = user.RootElement;
if (root.TryGetProperty("mail", out var mail))
{
return mail.GetString();
}
if (root.TryGetProperty("userPrincipalName", out mail))
{
return mail.GetString();
}
return null;
});
}
}
}
6 changes: 3 additions & 3 deletions src/Security/Authentication/OAuth/src/ClaimAction.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Security.Claims;
using Newtonsoft.Json.Linq;
using System.Text.Json;

namespace Microsoft.AspNetCore.Authentication.OAuth.Claims
{
Expand Down Expand Up @@ -37,6 +37,6 @@ public ClaimAction(string claimType, string valueType)
/// <param name="userData">The source data to examine. This value may be null.</param>
/// <param name="identity">The identity to add Claims to.</param>
/// <param name="issuer">The value to use for Claim.Issuer when creating a Claim.</param>
public abstract void Run(JObject userData, ClaimsIdentity identity, string issuer);
public abstract void Run(JsonDocument userData, ClaimsIdentity identity, string issuer);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Security.Claims;
using System.Text.Json;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
using Newtonsoft.Json.Linq;

namespace Microsoft.AspNetCore.Authentication
{
Expand Down Expand Up @@ -69,7 +69,7 @@ public static void MapJsonSubKey(this ClaimActionCollection collection, string c
/// <param name="collection"></param>
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="resolver">The Func that will be called to select value from the given json user data.</param>
public static void MapCustomJson(this ClaimActionCollection collection, string claimType, Func<JObject, string> resolver)
public static void MapCustomJson(this ClaimActionCollection collection, string claimType, Func<JsonDocument, string> resolver)
{
collection.MapCustomJson(claimType, ClaimValueTypes.String, resolver);
}
Expand All @@ -82,7 +82,7 @@ public static void MapCustomJson(this ClaimActionCollection collection, string c
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="valueType">The value to use for Claim.ValueType when creating a Claim.</param>
/// <param name="resolver">The Func that will be called to select value from the given json user data.</param>
public static void MapCustomJson(this ClaimActionCollection collection, string claimType, string valueType, Func<JObject, string> resolver)
public static void MapCustomJson(this ClaimActionCollection collection, string claimType, string valueType, Func<JsonDocument, string> resolver)
{
collection.Add(new CustomJsonClaimAction(claimType, valueType, resolver));
}
Expand Down
10 changes: 5 additions & 5 deletions src/Security/Authentication/OAuth/src/CustomJsonClaimAction.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Security.Claims;
using Newtonsoft.Json.Linq;
using System.Text.Json;

namespace Microsoft.AspNetCore.Authentication.OAuth.Claims
{
Expand All @@ -18,7 +18,7 @@ public class CustomJsonClaimAction : ClaimAction
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="valueType">The value to use for Claim.ValueType when creating a Claim.</param>
/// <param name="resolver">The Func that will be called to select value from the given json user data.</param>
public CustomJsonClaimAction(string claimType, string valueType, Func<JObject, string> resolver)
public CustomJsonClaimAction(string claimType, string valueType, Func<JsonDocument, string> resolver)
: base(claimType, valueType)
{
Resolver = resolver;
Expand All @@ -27,10 +27,10 @@ public CustomJsonClaimAction(string claimType, string valueType, Func<JObject, s
/// <summary>
/// The Func that will be called to select value from the given json user data.
/// </summary>
public Func<JObject, string> Resolver { get; }
public Func<JsonDocument, string> Resolver { get; }

/// <inheritdoc />
public override void Run(JObject userData, ClaimsIdentity identity, string issuer)
public override void Run(JsonDocument userData, ClaimsIdentity identity, string issuer)
{
if (userData == null)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Security/Authentication/OAuth/src/DeleteClaimAction.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using System.Security.Claims;
using Newtonsoft.Json.Linq;
using System.Text.Json;

namespace Microsoft.AspNetCore.Authentication.OAuth.Claims
{
Expand All @@ -22,7 +22,7 @@ public DeleteClaimAction(string claimType)
}

/// <inheritdoc />
public override void Run(JObject userData, ClaimsIdentity identity, string issuer)
public override void Run(JsonDocument userData, ClaimsIdentity identity, string issuer)
{
foreach (var claim in identity.FindAll(ClaimType).ToList())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.Globalization;
using System.Net.Http;
using System.Security.Claims;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;

namespace Microsoft.AspNetCore.Authentication.OAuth
{
Expand All @@ -33,7 +33,7 @@ public OAuthCreatingTicketContext(
OAuthOptions options,
HttpClient backchannel,
OAuthTokenResponse tokens)
: this(principal, properties, context, scheme, options, backchannel, tokens, user: new JObject())
: this(principal, properties, context, scheme, options, backchannel, tokens, user: JsonDocument.Parse("{}"))
{ }

/// <summary>
Expand All @@ -55,7 +55,7 @@ public OAuthCreatingTicketContext(
OAuthOptions options,
HttpClient backchannel,
OAuthTokenResponse tokens,
JObject user)
JsonDocument user)
: base(context, scheme, options)
{
if (backchannel == null)
Expand All @@ -82,9 +82,9 @@ public OAuthCreatingTicketContext(

/// <summary>
/// Gets the JSON-serialized user or an empty
/// <see cref="JObject"/> if it is not available.
/// <see cref="JsonDocument"/> if it is not available.
/// </summary>
public JObject User { get; }
public JsonDocument User { get; }

/// <summary>
/// Gets the token response returned by the authentication service.
Expand Down Expand Up @@ -136,7 +136,7 @@ public TimeSpan? ExpiresIn

public void RunClaimActions() => RunClaimActions(User);

public void RunClaimActions(JObject userData)
public void RunClaimActions(JsonDocument userData)
{
if (userData == null)
{
Expand All @@ -149,4 +149,4 @@ public void RunClaimActions(JObject userData)
}
}
}
}
}
Loading

0 comments on commit 5b0da93

Please sign in to comment.