From 67409fe9ef560a271f9f15cd6c161f5ac2d93364 Mon Sep 17 00:00:00 2001 From: MayueCif <6133287+MayueCif@users.noreply.github.com> Date: Fri, 7 Jul 2023 16:33:59 +0800 Subject: [PATCH] feat:update auth sdk and sso model (#642) * feat:update auth sdk and sso model * feat:updatej appsettings * fix:code smell * fix:code smell --- .../Constans/GrantType.cs | 19 +++++++++ .../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 +++- 14 files changed, 68 insertions(+), 91 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..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 @@ -5,23 +5,42 @@ 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"; + + 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(); } 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"),