Skip to content

Commit

Permalink
feat(auth):add auth sdk (#67)
Browse files Browse the repository at this point in the history
* auth sdk

* auth sdk

* auth test

* optimize code

* optimize code
  • Loading branch information
wuweilaiya authored May 26, 2022
1 parent c482330 commit 1976e16
Show file tree
Hide file tree
Showing 16 changed files with 376 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Masa.Contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.Service.Contracts", "src\BuildingBlocks\MASA.BuildingBlocks\src\Service\Masa.BuildingBlocks.Service.Contracts\Masa.BuildingBlocks.Service.Contracts.csproj", "{736F69E9-89D0-47E2-99AF-9412AAE1C6BB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.BasicAbility.Auth", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Auth\Masa.BuildingBlocks.BasicAbility.Auth.csproj", "{4B818EE6-8E13-40DA-B99A-218BB228EE91}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.Auth", "src\BasicAbility\Masa.Contrib.BasicAbility.Auth\Masa.Contrib.BasicAbility.Auth.csproj", "{CE1CABB0-B307-4709-84E0-583382FAAA29}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.Auth.Tests", "test\Masa.Contrib.BasicAbility.Auth.Tests\Masa.Contrib.BasicAbility.Auth.Tests.csproj", "{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -849,6 +855,30 @@ Global
{736F69E9-89D0-47E2-99AF-9412AAE1C6BB}.Release|Any CPU.Build.0 = Release|Any CPU
{736F69E9-89D0-47E2-99AF-9412AAE1C6BB}.Release|x64.ActiveCfg = Release|Any CPU
{736F69E9-89D0-47E2-99AF-9412AAE1C6BB}.Release|x64.Build.0 = Release|Any CPU
{4B818EE6-8E13-40DA-B99A-218BB228EE91}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B818EE6-8E13-40DA-B99A-218BB228EE91}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B818EE6-8E13-40DA-B99A-218BB228EE91}.Debug|x64.ActiveCfg = Debug|Any CPU
{4B818EE6-8E13-40DA-B99A-218BB228EE91}.Debug|x64.Build.0 = Debug|Any CPU
{4B818EE6-8E13-40DA-B99A-218BB228EE91}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B818EE6-8E13-40DA-B99A-218BB228EE91}.Release|Any CPU.Build.0 = Release|Any CPU
{4B818EE6-8E13-40DA-B99A-218BB228EE91}.Release|x64.ActiveCfg = Release|Any CPU
{4B818EE6-8E13-40DA-B99A-218BB228EE91}.Release|x64.Build.0 = Release|Any CPU
{CE1CABB0-B307-4709-84E0-583382FAAA29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE1CABB0-B307-4709-84E0-583382FAAA29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE1CABB0-B307-4709-84E0-583382FAAA29}.Debug|x64.ActiveCfg = Debug|Any CPU
{CE1CABB0-B307-4709-84E0-583382FAAA29}.Debug|x64.Build.0 = Debug|Any CPU
{CE1CABB0-B307-4709-84E0-583382FAAA29}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE1CABB0-B307-4709-84E0-583382FAAA29}.Release|Any CPU.Build.0 = Release|Any CPU
{CE1CABB0-B307-4709-84E0-583382FAAA29}.Release|x64.ActiveCfg = Release|Any CPU
{CE1CABB0-B307-4709-84E0-583382FAAA29}.Release|x64.Build.0 = Release|Any CPU
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}.Debug|x64.ActiveCfg = Debug|Any CPU
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}.Debug|x64.Build.0 = Debug|Any CPU
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}.Release|Any CPU.Build.0 = Release|Any CPU
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}.Release|x64.ActiveCfg = Release|Any CPU
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -953,6 +983,9 @@ Global
{7C7CEBAE-38A4-435D-BEC2-2425D58FABDC} = {E33ADF54-4D35-49B7-BDA6-412587CA39FF}
{EB0F6BDA-C6AA-4859-834E-5613BB38862B} = {5DFAF4A2-ECB5-46E4-904D-1EA5F48B2D48}
{736F69E9-89D0-47E2-99AF-9412AAE1C6BB} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{4B818EE6-8E13-40DA-B99A-218BB228EE91} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{CE1CABB0-B307-4709-84E0-583382FAAA29} = {5DFAF4A2-ECB5-46E4-904D-1EA5F48B2D48}
{2B644A8C-F0EE-4566-AB78-9E1C6D4185A3} = {38E6C400-90C0-493E-9266-C1602E229F1B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down
21 changes: 21 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Auth/AuthClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.BasicAbility.Auth;

public class AuthClient : IAuthClient
{
public AuthClient(ICallerProvider callerProvider)
{
UserService = new UserService(callerProvider);
SubjectService = new SubjectService(callerProvider);
TeamService = new TeamService(callerProvider);
}

public IUserService UserService { get; }

public ISubjectService SubjectService { get; }

public ITeamService TeamService { get; }
}

7 changes: 7 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Auth/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Masa.Contrib.BasicAbility.Auth;

internal class Constants
{
public const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.auth";
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="$(MasaUtilsPackageVersion)" />
<PackageReference Include="Masa.Utils.Configuration.Json" Version="$(MasaUtilsPackageVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Auth\Masa.BuildingBlocks.BasicAbility.Auth.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Masa.Contrib.BasicAbility.Auth.Service;

public class SubjectService : ISubjectService
{
readonly ICallerProvider _callerProvider;

public SubjectService(ICallerProvider callerProvider)
{
_callerProvider = callerProvider;
}

public async Task<List<SubjectModel>> GetListAsync(string filter)
{
var requestUri = $"api/subject/list";
return await _callerProvider.GetAsync<object, List<SubjectModel>>(requestUri, new { filter }) ?? new();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Masa.Contrib.BasicAbility.Auth.Service;

public class TeamService : ITeamService
{
readonly ICallerProvider _callerProvider;

public TeamService(ICallerProvider callerProvider)
{
_callerProvider = callerProvider;
}

public async Task<TeamDetailModel?> GetDetailAsync(Guid id)
{
var requestUri = $"api/team/deatil";
return await _callerProvider.GetAsync<object, TeamDetailModel>(requestUri, new { id });
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Masa.Contrib.BasicAbility.Auth.Service;

public class UserService : IUserService
{
readonly ICallerProvider _callerProvider;

public UserService(ICallerProvider callerProvider)
{
_callerProvider = callerProvider;
}

public async Task<UserModel?> AddAsync(AddUserModel user)
{
var requestUri = $"api/user/addExternal";
return await _callerProvider.PostAsync<AddUserModel, UserModel>(requestUri, user);
}

public async Task<List<StaffModel>> GetListByDepartmentAsync(Guid departmentId)
{
var requestUri = $"api/staff/getListByDepartment";
return await _callerProvider.GetAsync<object, List<StaffModel>>(requestUri, new { id = departmentId }) ?? new();
}

public async Task<List<StaffModel>> GetListByRoleAsync(Guid roleId)
{
var requestUri = $"api/staff/getListByRole";
return await _callerProvider.GetAsync<object, List<StaffModel>>(requestUri, new { id = roleId }) ?? new();
}

public async Task<List<StaffModel>> GetListByTeamAsync(Guid teamId)
{
var requestUri = $"api/staff/getListByTeam";
return await _callerProvider.GetAsync<object, List<StaffModel>>(requestUri, new { id = teamId }) ?? new();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


namespace Masa.Contrib.BasicAbility.Auth;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddAuthClient(this IServiceCollection services, string authServiceBaseAddress)
{
ArgumentNullException.ThrowIfNull(authServiceBaseAddress, nameof(authServiceBaseAddress));

return services.AddAuthClient(callerOptions =>
{
callerOptions.UseHttpClient(builder =>
{
builder.Name = DEFAULT_CLIENT_NAME;
builder.Configure = opt => opt.BaseAddress = new Uri(authServiceBaseAddress);
});
});
}

public static IServiceCollection AddAuthClient(this IServiceCollection services, Action<CallerOptions> callerOptions)
{
ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions));

services.AddCaller(callerOptions);

services.AddScoped<IAuthClient>(serviceProvider =>
{
var callProvider = serviceProvider.GetRequiredService<ICallerFactory>().CreateClient(DEFAULT_CLIENT_NAME);
var authClient = new AuthClient(callProvider);
return authClient;
});

return services;
}
}

11 changes: 11 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Auth/_import.cs
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.

global using Masa.BuildingBlocks.BasicAbility.Auth;
global using Masa.BuildingBlocks.BasicAbility.Auth.Model;
global using Masa.BuildingBlocks.BasicAbility.Auth.Service;
global using Masa.Contrib.BasicAbility.Auth.Service;
global using Masa.Utils.Caller.Core;
global using Masa.Utils.Caller.HttpClient;
global using Microsoft.Extensions.DependencyInjection;
global using static Masa.Contrib.BasicAbility.Auth.Constants;
2 changes: 1 addition & 1 deletion src/BuildingBlocks/MASA.BuildingBlocks
Submodule MASA.BuildingBlocks updated 19 files
+10 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Enum/GenderTypes.cs
+10 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Enum/StaffTypes.cs
+12 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Enum/SubjectTypes.cs
+9 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Enum/TeamTypes.cs
+14 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/IAuthClient.cs
+9 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Masa.BuildingBlocks.BasicAbility.Auth.csproj
+30 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Model/AddUserModel.cs
+28 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Model/AddressValueModel.cs
+27 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Model/StaffModel.cs
+38 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Model/SubjectModel.cs
+42 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Model/TeamDetailModel.cs
+20 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Model/TeamPersonnelModel.cs
+58 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Model/UserModel.cs
+10 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Service/ISubjectService.cs
+10 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Service/ITeamService.cs
+16 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/Service/IUserService.cs
+6 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth/_import.cs
+0 −14 src/Service/Masa.BuildingBlocks.Service.Contracts/BaseResponse.cs
+28 −34 src/Storage/Masa.BuildingBlocks.Storage.ObjectStorage/IClient.cs
32 changes: 32 additions & 0 deletions test/Masa.Contrib.BasicAbility.Auth.Tests/AuthClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Masa.Contrib.BasicAbility.Auth.Tests;

[TestClass]
public class AuthClientTest
{
[TestMethod]
public void TestAddAuthClient()
{
var services = new ServiceCollection();
services.AddAuthClient("https://localhost:18102");
var authClient = services.BuildServiceProvider().GetRequiredService<IAuthClient>();

Assert.IsNotNull(authClient);
}

[TestMethod]
public void TestAddAuthClientShouldThrowArgumentNullException()
{
var services = new ServiceCollection();

Assert.ThrowsException<ArgumentNullException>(() => services.AddAuthClient(authServiceBaseAddress: null));
}

[TestMethod]
public void TestAddAuthClientShouldThrowArgumentNullException2()
{
var services = new ServiceCollection();

Assert.ThrowsException<ArgumentNullException>(() => services.AddAuthClient(callerOptions: null));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\BasicAbility\Masa.Contrib.BasicAbility.Auth\Masa.Contrib.BasicAbility.Auth.csproj" />
<ProjectReference Include="..\..\src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Auth\Masa.BuildingBlocks.BasicAbility.Auth.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions test/Masa.Contrib.BasicAbility.Auth.Tests/SubjectServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Masa.Contrib.BasicAbility.Auth.Tests;

[TestClass]
public class SubjectServiceTest
{
[TestMethod]
public async Task TestGetListAsync()
{
var data = new List<SubjectModel>()
{
new SubjectModel()
};
string filter = "test";
var requestUri = $"api/subject/list";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<object, List<SubjectModel>>(requestUri, It.IsAny<object>(), default)).ReturnsAsync(data).Verifiable();
var authClient = new AuthClient(callerProvider.Object);
var result = await authClient.SubjectService.GetListAsync(filter);
callerProvider.Verify(provider => provider.GetAsync<object, List<SubjectModel>>(requestUri, It.IsAny<object>(), default), Times.Once);
Assert.IsTrue(result.Count == 1);
}
}

20 changes: 20 additions & 0 deletions test/Masa.Contrib.BasicAbility.Auth.Tests/TeamServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Masa.Contrib.BasicAbility.Auth.Tests;

[TestClass]
public class TeamServiceTest
{
[TestMethod]
public async Task TestGetListAsync()
{
var data = new TeamDetailModel();
Guid teamId = Guid.NewGuid();
var requestUri = $"api/team/deatil";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<object, TeamDetailModel>(requestUri, It.IsAny<object>(), default)).ReturnsAsync(data).Verifiable();
var authClient = new AuthClient(callerProvider.Object);
var result = await authClient.TeamService.GetDetailAsync(teamId);
callerProvider.Verify(provider => provider.GetAsync<object, TeamDetailModel>(requestUri, It.IsAny<object>(), default), Times.Once);
Assert.IsTrue(result is not null);
}
}

Loading

0 comments on commit 1976e16

Please sign in to comment.