From ffbedcf08f40d6ac6c45ea7289188ae8a60e21b4 Mon Sep 17 00:00:00 2001 From: Mayue Date: Fri, 7 Jul 2023 15:49:02 +0800 Subject: [PATCH 1/8] feat:update auth sdk and sso model --- .../Constans/GrantType.cs | 17 ++++++++ .../Constans/GrantTypes.cs | 6 +++ .../Models/GrantType.cs | 22 ---------- .../_Imports.cs | 1 + .../Model/GetSystemDataModel.cs | 11 +++++ .../Model/MenuModel.cs | 2 + .../Service/IUserService.cs | 2 +- .../EnvironmentMiddleware.cs | 40 ------------------- .../Service/UserService.cs | 15 +++---- .../ServiceCollectionExtensions.cs | 13 +----- .../SsoClientTest.cs | 2 +- .../UserServiceTest.cs | 12 +++--- .../Entries/LdapUser.cs | 6 +++ .../Masa.Utils.Ldap.Novell/LdapProvider.cs | 8 +++- .../appsettings.json | 25 ++++++++---- 15 files changed, 83 insertions(+), 99 deletions(-) delete mode 100644 src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Models/GrantType.cs create mode 100644 src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/GetSystemDataModel.cs delete mode 100644 src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs diff --git a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs index 8cd09ed80..bac798fea 100644 --- a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs +++ b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs @@ -5,23 +5,40 @@ namespace Masa.BuildingBlocks.Authentication.OpenIdConnect.Models.Constans; public static class GrantType { + [Description("Implicit")] public const string IMPLICIT = "implicit"; + [Description("hybrid")] public const string HYBRID = "hybrid"; + [Description("AuthorizationCode")] public const string AUTHORIZATION_CODE = "authorization_code"; + [Description("ClientCredentials")] public const string CLIENT_CREDENTIALS = "client_credentials"; + [Description("ResourceOwnerPassword")] public const string RESOURCE_OWNER_PASSWORD = "password"; + [Description("DeviceFlow")] public const string DEVICE_FLOW = "urn:ietf:params:oauth:grant-type:device_code"; + [Description("PhoneCode")] public const string PHONE_CODE = "phone_code"; + [Description("Phone")] public const string LOCAL_PHONE = "local_phone"; + [Description("ThirdPartyIdp")] public const string THIRD_PARTY_IDP = "third_party_idp"; + [Description("Ldap")] public const string LDAP = "ldap"; + + public static List<(string, string)> DisallowGrantTypeCombinations = new() + { + (IMPLICIT, AUTHORIZATION_CODE), + (IMPLICIT, HYBRID), + (AUTHORIZATION_CODE, HYBRID), + }; } diff --git a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantTypes.cs b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantTypes.cs index 2d9030aa8..2aa97e2d5 100644 --- a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantTypes.cs +++ b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantTypes.cs @@ -29,6 +29,12 @@ public class GrantTypes public static ICollection ResourceOwnerPassword => new[] { GrantType.RESOURCE_OWNER_PASSWORD }; + public static ICollection Phone => + new[] { GrantType.PHONE_CODE, GrantType.LOCAL_PHONE }; + + public static ICollection Ldap => + new[] { GrantType.LDAP }; + public static ICollection ResourceOwnerPasswordAndClientCredentials => new[] { GrantType.RESOURCE_OWNER_PASSWORD, GrantType.CLIENT_CREDENTIALS }; diff --git a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Models/GrantType.cs b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Models/GrantType.cs deleted file mode 100644 index d5db09b60..000000000 --- a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Models/GrantType.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) MASA Stack All rights reserved. -// Licensed under the MIT License. See LICENSE.txt in the project root for license information. - -namespace Masa.BuildingBlocks.Authentication.OpenIdConnect.Models.Models; - -public class GrantType -{ - public const string Implicit = "implicit"; - public const string Hybrid = "hybrid"; - public const string AuthorizationCode = "authorization_code"; - public const string ClientCredentials = "client_credentials"; - public const string ResourceOwnerPassword = "password"; - public const string DeviceFlow = "urn:ietf:params:oauth:grant-type:device_code"; - - public static List<(string, string)> DisallowGrantTypeCombinations = new() - { - (Implicit, AuthorizationCode), - (Implicit, Hybrid), - (AuthorizationCode, Hybrid), - }; -} - diff --git a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/_Imports.cs b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/_Imports.cs index 3a1a2c45c..f6a1b35b6 100644 --- a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/_Imports.cs +++ b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/_Imports.cs @@ -1,6 +1,7 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. +global using Masa.BuildingBlocks.Authentication.OpenIdConnect.Models.Constans; global using Masa.BuildingBlocks.Authentication.OpenIdConnect.Models.Enums; global using Masa.BuildingBlocks.Authentication.OpenIdConnect.Models.Models; global using System.ComponentModel; diff --git a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/GetSystemDataModel.cs b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/GetSystemDataModel.cs new file mode 100644 index 000000000..4ae7ee23a --- /dev/null +++ b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/GetSystemDataModel.cs @@ -0,0 +1,11 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.BuildingBlocks.StackSdks.Auth.Contracts.Model; + +public class GetSystemDataModel +{ + public string SystemId { get; set; } + + public List UserIds { get; set; } = new(); +} diff --git a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/MenuModel.cs b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/MenuModel.cs index 5c6975e30..3ba310e00 100644 --- a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/MenuModel.cs +++ b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/MenuModel.cs @@ -15,5 +15,7 @@ public class MenuModel public string Url { get; set; } + public string MatchPattern { get; set; } + public List Children { get; set; } = new(); } diff --git a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs index eaaf39452..2c4e6a871 100644 --- a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs +++ b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs @@ -71,7 +71,7 @@ public interface IUserService Task GetSystemDataAsync(Guid userId, string systemId); - Task> GetSystemListDataAsync(IEnumerable userIds, string systemId); + Task> GetSystemListDataAsync(IEnumerable userIds, string systemId); Task DisableAsync(DisableUserModel user); diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs deleted file mode 100644 index 02921861f..000000000 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) MASA Stack All rights reserved. -// Licensed under the MIT License. See LICENSE.txt in the project root for license information. - -using Microsoft.Extensions.Logging; - -namespace Masa.Contrib.StackSdks.Auth; - -public class EnvironmentMiddleware : ICallerMiddleware -{ - readonly ILogger? _logger; - - public EnvironmentMiddleware(ILoggerFactory? loggerFactory = null) - { - _logger = loggerFactory?.CreateLogger(); - } - - public async Task HandleAsync(MasaHttpContext masaHttpContext, CallerHandlerDelegate next, CancellationToken cancellationToken = default) - { - if (masaHttpContext.RequestMessage.Content != null && masaHttpContext.RequestMessage.Content.Headers.ContentType?.MediaType == "application/json") - { - var body = await masaHttpContext.RequestMessage.Content.ReadAsStringAsync(CancellationToken.None); - try - { - var obj = JsonSerializer.Deserialize(body, new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true - }); - if (!string.IsNullOrEmpty(obj?.Environment)) - { - masaHttpContext.RequestMessage.Headers.Add(IsolationConsts.ENVIRONMENT, obj?.Environment); - } - } - catch (Exception e) - { - _logger?.LogError(e, "EnvironmentMiddleware: Handle."); - } - } - await next(); - } -} diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs index 500fc3d27..6fb808543 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs @@ -207,23 +207,20 @@ public async Task UpsertSystemDataAsync(string systemId, T data) public async Task GetSystemDataAsync(string systemId) { var userId = _userContext.GetUserId(); - var requestUri = $"api/user/systemData"; - var data = await _caller.GetAsync(requestUri, new { userId, systemId }); - return string.IsNullOrEmpty(data) ? default : JsonSerializer.Deserialize(data); + return await GetSystemDataAsync(userId, systemId); } public async Task GetSystemDataAsync(Guid userId, string systemId) { - var requestUri = $"api/user/systemData"; - var data = await _caller.GetAsync(requestUri, new { userId, systemId }); - return string.IsNullOrEmpty(data) ? default : JsonSerializer.Deserialize(data); + var dataList = await GetSystemListDataAsync(new List { userId }, systemId); + return dataList.FirstOrDefault().Value ?? default; } - public async Task> GetSystemListDataAsync(IEnumerable userIds, string systemId) + public async Task> GetSystemListDataAsync(IEnumerable userIds, string systemId) { var requestUri = $"api/user/systemData/byIds"; - var data = await _caller.GetAsync>(requestUri, new { userIds = string.Join(',', userIds), systemId }) ?? new(); - return data.Select(item => JsonSerializer.Deserialize(item)!).ToList(); + var data = await _caller.PostAsync>(requestUri, new GetSystemDataModel { UserIds = userIds.ToList(), SystemId = systemId }) ?? new(); + return data; } public async Task DisableAsync(DisableUserModel user) diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs index 0eec47ac4..32e739e3e 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs @@ -26,7 +26,6 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services, { callerBuilder .UseHttpClient(builder => builder.BaseAddress = authServiceBaseAddress) - .AddMiddleware() .UseAuthentication(); }, redisOptions); } @@ -62,19 +61,11 @@ private static IServiceCollection AddAuthClient(this IServiceCollection services return services; } - public static IServiceCollection AddSsoClient(this IServiceCollection services, IConfiguration configuration) - { - var ssoServiceBaseAddressFunc = () => configuration.GetValue("$public.AppSettings:SsoClient:Url"); - services.AddSsoClient(ssoServiceBaseAddressFunc); - - return services; - } - - public static IServiceCollection AddSsoClient(this IServiceCollection services, Func ssoServiceBaseAddressFunc) + public static IServiceCollection AddSsoClient(this IServiceCollection services, string ssoServiceAddress) { services.AddHttpClient(DEFAULT_SSO_CLIENT_NAME, httpClient => { - httpClient.BaseAddress = new Uri(ssoServiceBaseAddressFunc()); + httpClient.BaseAddress = new Uri(ssoServiceAddress); }); services.AddSingleton(); diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/SsoClientTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/SsoClientTest.cs index f946114ed..62fd52c49 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/SsoClientTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/SsoClientTest.cs @@ -10,7 +10,7 @@ public class SsoClientTest public void TestAddSsoClient() { var services = new ServiceCollection(); - services.AddSsoClient(() => "https://localhost:18102"); + services.AddSsoClient("https://localhost:18102"); var ssoClient = services.BuildServiceProvider().GetRequiredService(); Assert.IsNotNull(ssoClient); diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs index 6f3c5b78f..d5c8fda5f 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs @@ -483,10 +483,10 @@ public async Task TestIntGetUserSystemDataAsync(string systemId) { var userId = Guid.Parse("A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1"); var data = 1; - var requestUri = $"api/user/systemData"; + var requestUri = $"api/user/systemData/byIds"; var caller = new Mock(); - caller.Setup(provider => provider.GetAsync(requestUri, It.IsAny(), default)) - .ReturnsAsync(data.ToString()).Verifiable(); + caller.Setup(provider => provider.PostAsync>(requestUri, It.IsAny(), default)) + .ReturnsAsync(new Dictionary() { { userId, data } }).Verifiable(); var userContext = new Mock(); userContext.Setup(user => user.GetUserId()).Returns(userId).Verifiable(); var userService = GetUserService(caller, userContext); @@ -504,10 +504,10 @@ public async Task TestObjectGetUserSystemDataAsync(string systemId) Name = "name", Value = "value" }; - var requestUri = $"api/user/systemData"; + var requestUri = $"api/user/systemData/byIds"; var caller = new Mock(); - caller.Setup(provider => provider.GetAsync(requestUri, It.IsAny(), default)) - .ReturnsAsync(JsonSerializer.Serialize(data)).Verifiable(); + caller.Setup(provider => provider.PostAsync>(requestUri, It.IsAny(), default)) + .ReturnsAsync(new Dictionary() { { userId, data } }).Verifiable(); var userContext = new Mock(); userContext.Setup(user => user.GetUserId()).Returns(userId).Verifiable(); var userService = GetUserService(caller, userContext); diff --git a/src/Utils/Ldap/Masa.Utils.Ldap.Novell/Entries/LdapUser.cs b/src/Utils/Ldap/Masa.Utils.Ldap.Novell/Entries/LdapUser.cs index 39657d1ef..87e7ef739 100644 --- a/src/Utils/Ldap/Masa.Utils.Ldap.Novell/Entries/LdapUser.cs +++ b/src/Utils/Ldap/Masa.Utils.Ldap.Novell/Entries/LdapUser.cs @@ -49,5 +49,11 @@ public class LdapUser public string Phone { get; set; } = string.Empty; + public string Company { get; set; } = string.Empty; + + public string Title { get; set; } = string.Empty; + + public string Department { get; set; } = string.Empty; + public LdapAddress Address { get; set; } = new(); } diff --git a/src/Utils/Ldap/Masa.Utils.Ldap.Novell/LdapProvider.cs b/src/Utils/Ldap/Masa.Utils.Ldap.Novell/LdapProvider.cs index 44ee1e917..3eea657b4 100644 --- a/src/Utils/Ldap/Masa.Utils.Ldap.Novell/LdapProvider.cs +++ b/src/Utils/Ldap/Masa.Utils.Ldap.Novell/LdapProvider.cs @@ -82,7 +82,10 @@ public async Task AddUserAsync(LdapUser user, string password) new LdapAttribute("userAccountControl", "512"), new LdapAttribute("givenName", user.FirstName), new LdapAttribute("sn", user.LastName), - new LdapAttribute("mail", user.EmailAddress) + new LdapAttribute("mail", user.EmailAddress), + new LdapAttribute("company", user.Company), + new LdapAttribute("department", user.Department), + new LdapAttribute("title", user.Title) }; attributeSet.AddAttribute("displayName", user.DisplayName); @@ -213,6 +216,9 @@ private LdapUser CreateUser(string distinguishedName, LdapAttributeSet attribute ldapUser.Description = attributeSet.GetString("description"); ldapUser.Phone = attributeSet.GetString("telephoneNumber"); ldapUser.EmailAddress = attributeSet.GetString("mail"); + ldapUser.Company = attributeSet.GetString("company"); + ldapUser.Department = attributeSet.GetString("department"); + ldapUser.Title = attributeSet.GetString("title"); ldapUser.Address = new LdapAddress { Street = attributeSet.GetString("streetAddress"), diff --git a/src/Utils/Ldap/Tests/Masa.Utils.Ldap.Novell.Tests/appsettings.json b/src/Utils/Ldap/Tests/Masa.Utils.Ldap.Novell.Tests/appsettings.json index 5a64f707c..465f276fe 100644 --- a/src/Utils/Ldap/Tests/Masa.Utils.Ldap.Novell.Tests/appsettings.json +++ b/src/Utils/Ldap/Tests/Masa.Utils.Ldap.Novell.Tests/appsettings.json @@ -1,12 +1,21 @@ { + //"LdapOptions": { + // "ServerAddress": "localhost", + // "ServerPort": 5389, + // "ServerPortSsl": 5636, + // "BaseDn": "dc=example,dc=com", + // "UserSearchBaseDn": "OU=XXXX,dc=example,dc=com", + // "GroupSearchBaseDn": "", + // "RootUserDn": "cn=admin,dc=example,dc=com", + // "RootUserPassword": "password" + //} "LdapOptions": { - "ServerAddress": "localhost", - "ServerPort": 5389, - "ServerPortSsl": 5636, - "BaseDn": "dc=example,dc=com", - "UserSearchBaseDn": "OU=XXXX,dc=example,dc=com", - "GroupSearchBaseDn": "", - "RootUserDn": "cn=admin,dc=example,dc=com", - "RootUserPassword": "password" + "ServerAddress": "192.168.10.21", + "ServerPort": "389", + "BaseDn": "DC=lonsid,DC=cn", + "UserSearchBaseDn": "OU=朗诗德集团,DC=lonsid,DC=cn", + "GroupSearchBaseDn": "DC=lonsid,DC=cn", + "RootUserDn": "lonsid\\bpm", + "RootUserPassword": "Abc@321" } } \ No newline at end of file From ad83725ee3a5aa04fb1ee689ba90195e93f2ac55 Mon Sep 17 00:00:00 2001 From: Mayue Date: Fri, 7 Jul 2023 15:52:12 +0800 Subject: [PATCH 2/8] feat:updatej appsettings --- .../appsettings.json | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/Utils/Ldap/Tests/Masa.Utils.Ldap.Novell.Tests/appsettings.json b/src/Utils/Ldap/Tests/Masa.Utils.Ldap.Novell.Tests/appsettings.json index 465f276fe..5a64f707c 100644 --- a/src/Utils/Ldap/Tests/Masa.Utils.Ldap.Novell.Tests/appsettings.json +++ b/src/Utils/Ldap/Tests/Masa.Utils.Ldap.Novell.Tests/appsettings.json @@ -1,21 +1,12 @@ { - //"LdapOptions": { - // "ServerAddress": "localhost", - // "ServerPort": 5389, - // "ServerPortSsl": 5636, - // "BaseDn": "dc=example,dc=com", - // "UserSearchBaseDn": "OU=XXXX,dc=example,dc=com", - // "GroupSearchBaseDn": "", - // "RootUserDn": "cn=admin,dc=example,dc=com", - // "RootUserPassword": "password" - //} "LdapOptions": { - "ServerAddress": "192.168.10.21", - "ServerPort": "389", - "BaseDn": "DC=lonsid,DC=cn", - "UserSearchBaseDn": "OU=朗诗德集团,DC=lonsid,DC=cn", - "GroupSearchBaseDn": "DC=lonsid,DC=cn", - "RootUserDn": "lonsid\\bpm", - "RootUserPassword": "Abc@321" + "ServerAddress": "localhost", + "ServerPort": 5389, + "ServerPortSsl": 5636, + "BaseDn": "dc=example,dc=com", + "UserSearchBaseDn": "OU=XXXX,dc=example,dc=com", + "GroupSearchBaseDn": "", + "RootUserDn": "cn=admin,dc=example,dc=com", + "RootUserPassword": "password" } } \ No newline at end of file From a15c535f131e89a76fc0d91c2487e8b7e3500cd4 Mon Sep 17 00:00:00 2001 From: Mayue Date: Fri, 7 Jul 2023 16:05:02 +0800 Subject: [PATCH 3/8] fix:code smell --- .../Constans/GrantType.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs index bac798fea..e7edbaa72 100644 --- a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs +++ b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs @@ -35,7 +35,7 @@ public static class GrantType [Description("Ldap")] public const string LDAP = "ldap"; - public static List<(string, string)> DisallowGrantTypeCombinations = new() + public static readonly List<(string, string)> DisallowGrantTypeCombinations = new() { (IMPLICIT, AUTHORIZATION_CODE), (IMPLICIT, HYBRID), From 9edb6b4b1f901527ad081a77e9cfcdc5db28b917 Mon Sep 17 00:00:00 2001 From: Mayue Date: Fri, 7 Jul 2023 16:23:18 +0800 Subject: [PATCH 4/8] fix:code smell --- .../Constans/GrantType.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs index e7edbaa72..7b34c5cd3 100644 --- a/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs +++ b/src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Models/Constans/GrantType.cs @@ -35,10 +35,12 @@ public static class GrantType [Description("Ldap")] public const string LDAP = "ldap"; - public static readonly List<(string, string)> DisallowGrantTypeCombinations = new() + private static readonly List<(string, string)> _disallowCombinations = new List<(string, string)> { (IMPLICIT, AUTHORIZATION_CODE), (IMPLICIT, HYBRID), (AUTHORIZATION_CODE, HYBRID), }; + + public static IReadOnlyCollection<(string, string)> DisallowGrantTypeCombinations => _disallowCombinations.AsReadOnly(); } From 398e18352be833f38f5c5a10455ad69e5c2806f3 Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 10:05:09 +0800 Subject: [PATCH 5/8] fix:GetSystemListDataAsync T --- .../Service/UserService.cs | 6 +++--- .../UserServiceTest.cs | 20 ------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs index 6fb808543..84365b280 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs @@ -216,11 +216,11 @@ public async Task UpsertSystemDataAsync(string systemId, T data) return dataList.FirstOrDefault().Value ?? default; } - public async Task> GetSystemListDataAsync(IEnumerable userIds, string systemId) + public async Task> GetSystemListDataAsync(IEnumerable userIds, string systemId) { var requestUri = $"api/user/systemData/byIds"; - var data = await _caller.PostAsync>(requestUri, new GetSystemDataModel { UserIds = userIds.ToList(), SystemId = systemId }) ?? new(); - return data; + var data = await _caller.PostAsync>(requestUri, new GetSystemDataModel { UserIds = userIds.ToList(), SystemId = systemId }) ?? new(); + return data.ToDictionary(d => d.Key, d => typeof(T) == typeof(string) ? (T)(object)d.Value : JsonSerializer.Deserialize(d.Value)); } public async Task DisableAsync(DisableUserModel user) diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs index d5c8fda5f..c75c739ec 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs @@ -515,26 +515,6 @@ public async Task TestObjectGetUserSystemDataAsync(string systemId) Assert.IsTrue(result is not null); } - [TestMethod] - [DataRow("masa-auth")] - public async Task TestGetUserSystemDataAsync(string systemId) - { - var userIds = new List { Guid.NewGuid() }; - var data = new SystemData - { - Name = "name", - Value = "value" - }; - var requestUri = $"api/user/systemData/byIds"; - var caller = new Mock(); - caller.Setup(provider => provider.GetAsync(requestUri, new { userIds = string.Join(',', userIds), systemId }, default)) - .ReturnsAsync(JsonSerializer.Serialize(data)).Verifiable(); - var userContext = new Mock(); - var userService = GetUserService(caller, userContext); - var result = await userService.GetSystemListDataAsync(userIds, systemId); - Assert.IsTrue(result is not null); - } - [TestMethod] [DataRow("masa-auth")] public async Task TestIntUpsertSystemDataAsync(string systemId) From 175857494422d24395710e9bb67143b307006b95 Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 10:35:51 +0800 Subject: [PATCH 6/8] fix:test error --- .../Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs index c75c739ec..26654657e 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs @@ -485,8 +485,8 @@ public async Task TestIntGetUserSystemDataAsync(string systemId) var data = 1; var requestUri = $"api/user/systemData/byIds"; var caller = new Mock(); - caller.Setup(provider => provider.PostAsync>(requestUri, It.IsAny(), default)) - .ReturnsAsync(new Dictionary() { { userId, data } }).Verifiable(); + caller.Setup(provider => provider.PostAsync>(requestUri, It.IsAny(), default)) + .ReturnsAsync(new Dictionary() { { userId, data.ToString() } }).Verifiable(); var userContext = new Mock(); userContext.Setup(user => user.GetUserId()).Returns(userId).Verifiable(); var userService = GetUserService(caller, userContext); @@ -506,8 +506,8 @@ public async Task TestObjectGetUserSystemDataAsync(string systemId) }; var requestUri = $"api/user/systemData/byIds"; var caller = new Mock(); - caller.Setup(provider => provider.PostAsync>(requestUri, It.IsAny(), default)) - .ReturnsAsync(new Dictionary() { { userId, data } }).Verifiable(); + caller.Setup(provider => provider.PostAsync>(requestUri, It.IsAny(), default)) + .ReturnsAsync(new Dictionary() { { userId, JsonSerializer.Serialize(data) } }).Verifiable(); var userContext = new Mock(); userContext.Setup(user => user.GetUserId()).Returns(userId).Verifiable(); var userService = GetUserService(caller, userContext); From 292d4cca5eaa6465ee22a45a6a1f2400df5d1c05 Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 10:52:31 +0800 Subject: [PATCH 7/8] fix:code smell --- .../Masa.Contrib.StackSdks.Auth/Service/UserService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs index 84365b280..ea3780b23 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs @@ -220,7 +220,7 @@ public async Task UpsertSystemDataAsync(string systemId, T data) { var requestUri = $"api/user/systemData/byIds"; var data = await _caller.PostAsync>(requestUri, new GetSystemDataModel { UserIds = userIds.ToList(), SystemId = systemId }) ?? new(); - return data.ToDictionary(d => d.Key, d => typeof(T) == typeof(string) ? (T)(object)d.Value : JsonSerializer.Deserialize(d.Value)); + return data.ToDictionary(d => d.Key, d => typeof(T) == typeof(string) ? (T?)(object)d.Value : JsonSerializer.Deserialize(d.Value)); } public async Task DisableAsync(DisableUserModel user) From 78cc783ecf55151223081b059a507af0df30263b Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 11:03:36 +0800 Subject: [PATCH 8/8] fix:code smell --- .../Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs | 2 +- .../Masa.Contrib.StackSdks.Auth/Service/UserService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs index 2c4e6a871..d0983c65f 100644 --- a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs +++ b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs @@ -71,7 +71,7 @@ public interface IUserService Task GetSystemDataAsync(Guid userId, string systemId); - Task> GetSystemListDataAsync(IEnumerable userIds, string systemId); + Task> GetSystemListDataAsync(IEnumerable userIds, string systemId); Task DisableAsync(DisableUserModel user); diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs index ea3780b23..84365b280 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs @@ -220,7 +220,7 @@ public async Task UpsertSystemDataAsync(string systemId, T data) { var requestUri = $"api/user/systemData/byIds"; var data = await _caller.PostAsync>(requestUri, new GetSystemDataModel { UserIds = userIds.ToList(), SystemId = systemId }) ?? new(); - return data.ToDictionary(d => d.Key, d => typeof(T) == typeof(string) ? (T?)(object)d.Value : JsonSerializer.Deserialize(d.Value)); + return data.ToDictionary(d => d.Key, d => typeof(T) == typeof(string) ? (T)(object)d.Value : JsonSerializer.Deserialize(d.Value)); } public async Task DisableAsync(DisableUserModel user)