Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature(sdk auth):fix third party multilevel cache #321

Merged
merged 3 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public interface IUserService

Task<UserModel?> FindByEmailAsync(string email);

Task<UserModel> GetCurrentUserAsync();
Task<UserModel?> FindByIdAsync(Guid userId);

Task<UserModel?> GetCurrentUserAsync();

Task<StaffDetailModel?> GetCurrentStaffAsync();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.StackSdks.Auth.Model;

public class AuthClientMultilevelCacheProvider
{
readonly IMultilevelCacheClientFactory _clientFactory;

public AuthClientMultilevelCacheProvider(IMultilevelCacheClientFactory clientFactory)
{
_clientFactory = clientFactory;
}

public IMultilevelCacheClient GetMultilevelCacheClient()
{
return _clientFactory.Create(DEFAULT_CLIENT_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public class ThirdPartyIdpCacheService : IThirdPartyIdpCacheService
{
readonly IMultilevelCacheClient _memoryCacheClient;

public ThirdPartyIdpCacheService(IMultilevelCacheClient memoryCacheClient)
public ThirdPartyIdpCacheService(AuthClientMultilevelCacheProvider authClientMultilevelCacheProvider)
{
_memoryCacheClient = memoryCacheClient;
_memoryCacheClient = authClientMultilevelCacheProvider.GetMultilevelCacheClient();
}

public async Task<List<ThirdPartyIdpModel>> GetAllAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ public async Task<long> GetTotalByTeamAsync(Guid teamId)
return await _caller.GetAsync<object, UserModel>(requestUri, new { email });
}

public async Task<UserModel> GetCurrentUserAsync()
public async Task<UserModel?> FindByIdAsync(Guid id)
{
var id = _userContext.GetUserId<Guid>();
var requestUri = $"api/user/byId/{id}";
return await _caller.GetAsync<object, UserModel>(requestUri, new { id }) ?? new();
return await _caller.GetAsync<object, UserModel>(requestUri, new { id });
}

public async Task<UserModel?> GetCurrentUserAsync()
{
var id = _userContext.GetUserId<Guid>();
return await FindByIdAsync(id);
}

public async Task<StaffDetailModel?> GetCurrentStaffAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
Expand Down Expand Up @@ -41,16 +42,7 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services,
services.AddScoped<HttpEnvironmentDelegatingHandler>();
services.AddCaller(callerOptions);

services.AddMultilevelCache(
DEFAULT_CLIENT_NAME,
distributedCacheOptions => distributedCacheOptions.UseStackExchangeRedisCache(redisOptions),
multilevelCacheOptions =>
{
multilevelCacheOptions.SubscribeKeyType = SubscribeKeyType.SpecificPrefix;
multilevelCacheOptions.SubscribeKeyPrefix = DEFAULT_SUBSCRIBE_KEY_PREFIX;
}
);

services.AddAuthClientMultilevelCache(redisOptions);
services.AddSingleton<IThirdPartyIdpCacheService, ThirdPartyIdpCacheService>();
services.AddSingleton<ISsoClient, SsoClient>();
services.AddScoped<IAuthClient>(serviceProvider =>
Expand All @@ -73,4 +65,20 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services,

return services;
}

public static IServiceCollection AddAuthClientMultilevelCache(this IServiceCollection services, RedisConfigurationOptions redisOptions)
{
services.AddMultilevelCache(
DEFAULT_CLIENT_NAME,
distributedCacheOptions => distributedCacheOptions.UseStackExchangeRedisCache(redisOptions),
multilevelCacheOptions =>
{
multilevelCacheOptions.SubscribeKeyType = SubscribeKeyType.SpecificPrefix;
multilevelCacheOptions.SubscribeKeyPrefix = DEFAULT_SUBSCRIBE_KEY_PREFIX;
}
);
services.AddSingleton<AuthClientMultilevelCacheProvider>();

return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
global using Masa.Contrib.Service.Caller.Authentication.OpenIdConnect;
global using Masa.Contrib.Service.Caller.HttpClient;
global using Masa.Contrib.StackSdks.Auth;
global using Masa.Contrib.StackSdks.Auth.Model;
global using Masa.Contrib.StackSdks.Auth.Service;
global using Microsoft.AspNetCore.Http;
global using Microsoft.Extensions.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ public async Task TestFindByEmailAsync(string email)
Assert.IsTrue(result is not null && result.Email == data.Email);
}

[TestMethod]
public async Task TestFindByIdAsync()
{
var userId = Guid.Parse("A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1");
var data = new UserModel();
var requestUri = $"api/user/byId/{userId}";
var caller = new Mock<ICaller>();
caller.Setup(provider => provider.GetAsync<object, UserModel>(requestUri, It.IsAny<object>(), default)).ReturnsAsync(data).Verifiable();
var userContext = new Mock<IUserContext>();
var userService = new UserService(caller.Object, userContext.Object);
var result = await userService.FindByIdAsync(userId);
caller.Verify(provider => provider.GetAsync<object, UserModel>(requestUri, It.IsAny<object>(), default), Times.Once);
Assert.IsTrue(result is not null);
}

[TestMethod]
public async Task TestGetCurrentUserAsync()
{
Expand Down