Skip to content

Commit

Permalink
feat:update auth sdk and sso model (#642)
Browse files Browse the repository at this point in the history
* feat:update auth sdk and sso model

* feat:updatej appsettings

* fix:code smell

* fix:code smell
  • Loading branch information
MayueCif authored Jul 7, 2023
1 parent c99a478 commit 67409fe
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public class GrantTypes
public static ICollection<string> ResourceOwnerPassword =>
new[] { GrantType.RESOURCE_OWNER_PASSWORD };

public static ICollection<string> Phone =>
new[] { GrantType.PHONE_CODE, GrantType.LOCAL_PHONE };

public static ICollection<string> Ldap =>
new[] { GrantType.LDAP };

public static ICollection<string> ResourceOwnerPasswordAndClientCredentials =>
new[] { GrantType.RESOURCE_OWNER_PASSWORD, GrantType.CLIENT_CREDENTIALS };

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Guid> UserIds { get; set; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public class MenuModel

public string Url { get; set; }

public string MatchPattern { get; set; }

public List<MenuModel> Children { get; set; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public interface IUserService

Task<T?> GetSystemDataAsync<T>(Guid userId, string systemId);

Task<List<T>> GetSystemListDataAsync<T>(IEnumerable<Guid> userIds, string systemId);
Task<Dictionary<Guid, T>> GetSystemListDataAsync<T>(IEnumerable<Guid> userIds, string systemId);

Task<bool> DisableAsync(DisableUserModel user);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,20 @@ public async Task UpsertSystemDataAsync<T>(string systemId, T data)
public async Task<T?> GetSystemDataAsync<T>(string systemId)
{
var userId = _userContext.GetUserId<Guid>();
var requestUri = $"api/user/systemData";
var data = await _caller.GetAsync<object, string>(requestUri, new { userId, systemId });
return string.IsNullOrEmpty(data) ? default : JsonSerializer.Deserialize<T>(data);
return await GetSystemDataAsync<T>(userId, systemId);
}

public async Task<T?> GetSystemDataAsync<T>(Guid userId, string systemId)
{
var requestUri = $"api/user/systemData";
var data = await _caller.GetAsync<object, string>(requestUri, new { userId, systemId });
return string.IsNullOrEmpty(data) ? default : JsonSerializer.Deserialize<T>(data);
var dataList = await GetSystemListDataAsync<T>(new List<Guid> { userId }, systemId);
return dataList.FirstOrDefault().Value ?? default;
}

public async Task<List<T>> GetSystemListDataAsync<T>(IEnumerable<Guid> userIds, string systemId)
public async Task<Dictionary<Guid, T>> GetSystemListDataAsync<T>(IEnumerable<Guid> userIds, string systemId)
{
var requestUri = $"api/user/systemData/byIds";
var data = await _caller.GetAsync<object, List<string>>(requestUri, new { userIds = string.Join(',', userIds), systemId }) ?? new();
return data.Select(item => JsonSerializer.Deserialize<T>(item)!).ToList();
var data = await _caller.PostAsync<Dictionary<Guid, T>>(requestUri, new GetSystemDataModel { UserIds = userIds.ToList(), SystemId = systemId }) ?? new();
return data;
}

public async Task<bool> DisableAsync(DisableUserModel user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services,
{
callerBuilder
.UseHttpClient(builder => builder.BaseAddress = authServiceBaseAddress)
.AddMiddleware<EnvironmentMiddleware>()
.UseAuthentication();
}, redisOptions);
}
Expand Down Expand Up @@ -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<string>("$public.AppSettings:SsoClient:Url");
services.AddSsoClient(ssoServiceBaseAddressFunc);

return services;
}

public static IServiceCollection AddSsoClient(this IServiceCollection services, Func<string> 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<ISsoClient, SsoClient>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISsoClient>();

Assert.IsNotNull(ssoClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICaller>();
caller.Setup(provider => provider.GetAsync<object, string>(requestUri, It.IsAny<object>(), default))
.ReturnsAsync(data.ToString()).Verifiable();
caller.Setup(provider => provider.PostAsync<Dictionary<Guid, int>>(requestUri, It.IsAny<GetSystemDataModel>(), default))
.ReturnsAsync(new Dictionary<Guid, int>() { { userId, data } }).Verifiable();
var userContext = new Mock<IUserContext>();
userContext.Setup(user => user.GetUserId<Guid>()).Returns(userId).Verifiable();
var userService = GetUserService(caller, userContext);
Expand All @@ -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<ICaller>();
caller.Setup(provider => provider.GetAsync<object, string>(requestUri, It.IsAny<object>(), default))
.ReturnsAsync(JsonSerializer.Serialize(data)).Verifiable();
caller.Setup(provider => provider.PostAsync<Dictionary<Guid, SystemData>>(requestUri, It.IsAny<GetSystemDataModel>(), default))
.ReturnsAsync(new Dictionary<Guid, SystemData>() { { userId, data } }).Verifiable();
var userContext = new Mock<IUserContext>();
userContext.Setup(user => user.GetUserId<Guid>()).Returns(userId).Verifiable();
var userService = GetUserService(caller, userContext);
Expand Down
6 changes: 6 additions & 0 deletions src/Utils/Ldap/Masa.Utils.Ldap.Novell/Entries/LdapUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 7 additions & 1 deletion src/Utils/Ldap/Masa.Utils.Ldap.Novell/LdapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 67409fe

Please sign in to comment.