Skip to content

Commit

Permalink
Add LoginInfoConverter (OrchardCMS#15422)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Mike Alhayek <mike@crestapps.com>
Co-authored-by: hyzx86 <yanzhong.han@jizhousoft.com>
  • Loading branch information
3 people authored and urbanit committed Mar 18, 2024
1 parent 67ef6b0 commit c951cc1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using OrchardCore.Users.Handlers;
using OrchardCore.Users.Indexes;
using OrchardCore.Users.Services;
using OrchardCore.Users.Core.Json;
using System.Text.Json;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -55,6 +57,11 @@ public static IServiceCollection AddUsers(this IServiceCollection services)
services.AddScoped<ITwoFactorAuthenticationHandler, DefaultTwoFactorAuthenticationHandler>();
services.AddScoped<ITwoFactorAuthenticationHandlerCoordinator, DefaultTwoFactorAuthenticationHandlerCoordinator>();

services.Configure<JsonSerializerOptions>(options =>
{
options.Converters.Add(new LoginInfoJsonConverter());
});

return services;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Identity;

namespace OrchardCore.Users.Core.Json;

public class LoginInfoJsonConverter : JsonConverter<UserLoginInfo>
{
public static readonly LoginInfoJsonConverter Instance = new();

public override UserLoginInfo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var loginInfo = new UserLoginInfo(string.Empty, string.Empty, string.Empty);

while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}

if (reader.TokenType == JsonTokenType.PropertyName)
{
var propertyName = reader.GetString();
reader.Read();

switch (propertyName)
{
case nameof(UserLoginInfo.LoginProvider):
loginInfo.LoginProvider = reader.GetString();
break;
case nameof(UserLoginInfo.ProviderKey):
loginInfo.ProviderKey = reader.GetString();
break;
case nameof(UserLoginInfo.ProviderDisplayName):
loginInfo.ProviderDisplayName = reader.GetString();
break;
default:
break;
}
}
}

return loginInfo;
}

public override void Write(Utf8JsonWriter writer, UserLoginInfo objectToWrite, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(nameof(UserLoginInfo.LoginProvider), objectToWrite.LoginProvider);
writer.WriteString(nameof(UserLoginInfo.ProviderKey), objectToWrite.ProviderKey);
writer.WriteString(nameof(UserLoginInfo.ProviderDisplayName), objectToWrite.ProviderDisplayName);
writer.WriteEndObject();
}
}
77 changes: 77 additions & 0 deletions test/OrchardCore.Tests/Serializers/JsonSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Text.Json;
using OrchardCore.Tests.Apis.Context;
using OrchardCore.Users.Core.Json;
using OrchardCore.Users.Indexes;
using OrchardCore.Users.Models;

namespace OrchardCore.Tests.Serializers;

public class JsonSerializerTests
{
private const string _userLoginInfo = "{\"LoginProvider\":\"OpenIdConnect\",\"ProviderKey\":\"abc\",\"ProviderDisplayName\":\"default\"}";

private readonly JsonSerializerOptions _options;

public JsonSerializerTests()
{
_options = new JsonSerializerOptions();
_options.Converters.Add(new LoginInfoJsonConverter());
}

[Fact]
public void Deserialize_WhenCalled_ReturnValidUserLoginInfo()
{
var obj = JsonSerializer.Deserialize<UserLoginInfo>(_userLoginInfo, _options);

Assert.Equal("OpenIdConnect", obj.LoginProvider);
Assert.Equal("abc", obj.ProviderKey);
Assert.Equal("default", obj.ProviderDisplayName);
}

[Fact]
public void Serialize_WhenCalled_ReturnValidJson()
{
var loginInfo = new UserLoginInfo("OpenIdConnect", "abc", "default");
var json = JsonSerializer.Serialize(loginInfo, _options);

Assert.Equal(_userLoginInfo, json);
}

[Fact]
public async Task DefaultContentSerializer_SerializeAndDeserialize_UserWithUserLoginInfo()
{
using var context = new SiteContext();
await context.InitializeAsync();
await context.UsingTenantScopeAsync(async scope =>
{
var loginInfo = new UserLoginInfo("OpenIdConnect", "abc", "default");

var newUser = new User()
{
UserId = "abc",
UserName = "mike",
Email = "test@test.com",
LoginInfos =
[
loginInfo
]
};

var session = scope.ServiceProvider.GetRequiredService<YesSql.ISession>();

await session.SaveAsync(newUser);
await session.SaveChangesAsync();

var dbUser = await session.Query<User, UserIndex>(x => x.UserId == "abc").FirstOrDefaultAsync();

Assert.NotNull(dbUser);

var userLoginInfo = dbUser.LoginInfos.FirstOrDefault();

Assert.NotNull(userLoginInfo);
Assert.Equal(loginInfo.LoginProvider, userLoginInfo.LoginProvider);
Assert.Equal(loginInfo.ProviderKey, userLoginInfo.ProviderKey);
Assert.Equal(loginInfo.ProviderDisplayName, userLoginInfo.ProviderDisplayName);
});
}
}

0 comments on commit c951cc1

Please sign in to comment.