Skip to content

Commit

Permalink
Feature(oidc、sdk auth): add OIDC unit test、sdk auth ssoClient loginSe…
Browse files Browse the repository at this point in the history
…rvice add LoginByLdapAsync method (#478)

* feat:add oidc unit test

* feat:oidc add grantType LDAP

* feat:sdk auth thirdPartyIdpService add GetLdapOptions method

* feat:sdk auth ssoClient loginService add LoginByLdapAsync method

* refactor code
  • Loading branch information
wuweilaiya authored Mar 3, 2023
1 parent d05865c commit 079a0a5
Show file tree
Hide file tree
Showing 17 changed files with 399 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public static class GrantType
public const string LOCAL_PHONE = "local_phone";

public const string THIRD_PARTY_IDP = "third_party_idp";

public const string LDAP = "ldap";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class GrantTypes
new[] { GrantType.IMPLICIT, GrantType.CLIENT_CREDENTIALS };

public static ICollection<string> Code =>
new[] { GrantType.AUTHORIZATION_CODE, GrantType.PHONE_CODE, GrantType.LOCAL_PHONE, GrantType.THIRD_PARTY_IDP };
new[] { GrantType.AUTHORIZATION_CODE, GrantType.PHONE_CODE, GrantType.LOCAL_PHONE, GrantType.THIRD_PARTY_IDP, GrantType.LDAP };

public static ICollection<string> CodeAndClientCredentials =>
new[] { GrantType.AUTHORIZATION_CODE, GrantType.CLIENT_CREDENTIALS };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 LdapOptionsModel
{
public string ServerAddress { get; set; }

public int ServerPort { get; set; }

public int ServerPortSsl { get; set; }

public string BaseDn { get; set; }

public string UserSearchBaseDn { get; set; }

public string GroupSearchBaseDn { get; set; }

public string RootUserDn { get; set; }

public string RootUserPassword { get; set; }

public LdapOptionsModel(string serverAddress, int serverPort, string baseDn, string userSearchBaseDn, string groupSearchBaseDn, string rootUserDn, string rootUserPassword)
{
ServerAddress = serverAddress;
ServerPort = serverPort;
BaseDn = baseDn;
UserSearchBaseDn = userSearchBaseDn;
GroupSearchBaseDn = groupSearchBaseDn;
RootUserDn = rootUserDn;
RootUserPassword = rootUserPassword;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 LoginByLdapModel
{
public string ClientId { get; set; } = "";

public string ClientSecret { get; set; } = "";

public List<string> Scope { get; set; } = new() { "openid", "profile" };

public string UserName { get; set; }

public string Scheme { get; set; } = "Ldap";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ILoginService
Task<TokenModel> LoginByPhoneNumberAsync(LoginByPhoneNumberFromSsoModel login);

Task<LoginByThirdPartyIdpResultModel> LoginByThirdPartyIdpAsync(LoginByThirdPartyIdpModel login);

Task<TokenModel> LoginByLdapAsync(LoginByLdapModel login);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface IThirdPartyIdpService
Task<List<ThirdPartyIdpModel>> GetAllAsync();

Task<List<ThirdPartyIdpModel>> GetAllFromCacheAsync();

Task<LdapOptionsModel?> GetLdapOptionsAsync(string scheme);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Masa.Contrib.Authentication.OpenIdConnect.Cache.Caches;

[ExcludeFromCodeCoverage]
public class ApiResourceCache : IApiResourceCache
{
private readonly IMultilevelCacheClient _memoryCacheClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Masa.Contrib.Authentication.OpenIdConnect.Cache.Caches;

[ExcludeFromCodeCoverage]
public class ApiScopeCache : IApiScopeCache
{
private readonly IMultilevelCacheClient _memoryCacheClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Masa.Contrib.Authentication.OpenIdConnect.Cache.Caches;

[ExcludeFromCodeCoverage]
public class ClientCache : IClientCache
{
private readonly IMultilevelCacheClient _memoryCacheClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace Masa.Contrib.Authentication.OpenIdConnect.Cache.Caches;


[ExcludeFromCodeCoverage]
public class IdentityResourceCache : IIdentityResourceCache
{
private readonly IMultilevelCacheClient _memoryCacheClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.Authentication.OpenIdConnect.Cache.Tests;

[TestClass]
public class ApiResourceCacheTest
{
IApiResourceCache _cache;
ApiResource _apiResource;

[TestInitialize]
public void Initialized()
{
var options = new RedisConfigurationOptions()
{
Servers = new List<RedisServerOptions>
{
new RedisServerOptions
{
Host = "127.0.0.1",
Port = 6379
}
}
};
var serviceCollection = new ServiceCollection();
serviceCollection.AddOidcCache(options);
_cache = serviceCollection.BuildServiceProvider().GetRequiredService<IApiResourceCache>();
_apiResource = new ApiResource("ApiResourceCache", "ApiResourceCache", "ApiResourceCache", "", default, default, default, default);
}

[TestMethod]
public async Task TestSetAsync()
{
await _cache.SetAsync(_apiResource);
var apiResources = await _cache.GetListAsync();
Assert.IsTrue(apiResources.Any(item => item.Name == _apiResource.Name));
}

[TestMethod]
public async Task TestSetRangeAsync()
{
var input = new[] { _apiResource };
await _cache.SetRangeAsync(input);
var apiResources = await _cache.GetListAsync();
Assert.IsTrue(input.All(item => apiResources.Any(item2 => item2.Name == item.Name)));
}

[TestMethod]
public async Task TestGetListAsync()
{
await _cache.SetAsync(_apiResource);
var apiResources = await _cache.GetListAsync(new[] { _apiResource.Name });
Assert.IsTrue(apiResources.All(item => item.Name == _apiResource.Name));
apiResources = await _cache.GetListAsync();
Assert.IsTrue(apiResources.Any(item => item.Name == _apiResource.Name));
}

[TestMethod]
public async Task TestRemoveAsync()
{
await _cache.SetAsync(_apiResource);
await _cache.RemoveAsync(_apiResource);
var apiResources = await _cache.GetListAsync();
Assert.IsTrue(apiResources.All(item => item.Name != _apiResource.Name));
}

[TestMethod]
public async Task TestResetAsync()
{
var input = new[] { _apiResource };
await _cache.ResetAsync(input);
var apiResources = await _cache.GetListAsync();
Assert.IsTrue(apiResources.Select(item => item.Name).Except(input.Select(item => item.Name)).Any() is false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.Authentication.OpenIdConnect.Cache.Tests;

[TestClass]
public class ApiScopeCacheTest
{
IApiScopeCache _cache;
ApiScope _apiScope;

[TestInitialize]
public void Initialized()
{
var options = new RedisConfigurationOptions()
{
Servers = new List<RedisServerOptions>
{
new RedisServerOptions
{
Host = "127.0.0.1",
Port = 6379
}
}
};
var serviceCollection = new ServiceCollection();
serviceCollection.AddOidcCache(options);
_cache = serviceCollection.BuildServiceProvider().GetRequiredService<IApiScopeCache>();
_apiScope = new ApiScope("ApiScope");
}

[TestMethod]
public async Task TestSetAsync()
{
await _cache.SetAsync(_apiScope);
var apiScopes = await _cache.GetListAsync();
Assert.IsTrue(apiScopes.Any(item => item.Name == _apiScope.Name));
}

[TestMethod]
public async Task TestSetRangeAsync()
{
var input = new[] { _apiScope };
await _cache.SetRangeAsync(input);
var apiScopes = await _cache.GetListAsync();
Assert.IsTrue(input.All(item => apiScopes.Any(item2 => item2.Name == item.Name)));
}

[TestMethod]
public async Task TestGetListAsync()
{
await _cache.SetAsync(_apiScope);
var apiScopes = await _cache.GetListAsync(new[] { _apiScope.Name });
Assert.IsTrue(apiScopes.All(item => item.Name == _apiScope.Name));
apiScopes = await _cache.GetListAsync();
Assert.IsTrue(apiScopes.Any(item => item.Name == _apiScope.Name));
}

[TestMethod]
public async Task TestRemoveAsync()
{
await _cache.SetAsync(_apiScope);
await _cache.RemoveAsync(_apiScope);
var apiScopes = await _cache.GetListAsync();
Assert.IsTrue(apiScopes.All(item => item.Name != _apiScope.Name));
}

[TestMethod]
public async Task TestResetAsync()
{
var input = new[] { _apiScope };
await _cache.ResetAsync(input);
var apiScopes = await _cache.GetListAsync();
Assert.IsTrue(apiScopes.Select(item => item.Name).Except(input.Select(item => item.Name)).Any() is false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.Authentication.OpenIdConnect.Cache.Tests;

[TestClass]
public class ClientCacheTest
{
IClientCache _cache;
Client _client;

[TestInitialize]
public void Initialized()
{
var options = new RedisConfigurationOptions()
{
Servers = new List<RedisServerOptions>
{
new RedisServerOptions
{
Host = "127.0.0.1",
Port = 6379
}
}
};
var serviceCollection = new ServiceCollection();
serviceCollection.AddOidcCache(options);
_cache = serviceCollection.BuildServiceProvider().GetRequiredService<IClientCache>();
_client = new Client(ClientTypes.Web, "Client", "Client");
}

[TestMethod]
public async Task TestSetAsync()
{
await _cache.SetAsync(_client);
var clients = await _cache.GetListAsync(new[] { _client.ClientName });
Assert.IsTrue(clients.Any(item => item.ClientName == _client.ClientName));
}

[TestMethod]
public async Task TestSetRangeAsync()
{
var input = new[] { _client };
await _cache.SetRangeAsync(input);
var clients = await _cache.GetListAsync(new[] { _client.ClientName });
Assert.IsTrue(input.All(item => clients.Any(item2 => item2.ClientName == item.ClientName)));
}

[TestMethod]
public async Task TestGetListAsync()
{
await _cache.SetAsync(_client);
var clients = await _cache.GetListAsync(new[] { _client.ClientName });
Assert.IsTrue(clients.All(item => item.ClientName == _client.ClientName));
clients = await _cache.GetListAsync(new[] { _client.ClientName });
Assert.IsTrue(clients.Any(item => item.ClientName == _client.ClientName));
}

[TestMethod]
public async Task TestRemoveAsync()
{
await _cache.SetAsync(_client);
await _cache.RemoveAsync(_client);
var clients = await _cache.GetListAsync(new[] { _client.ClientName });
Assert.IsTrue(clients.All(item => item.ClientName != _client.ClientName));
}

[TestMethod]
public async Task TestResetAsync()
{
var input = new[] { _client };
await _cache.ResetAsync(input);
var clients = await _cache.GetListAsync(new[] { _client.ClientName });
Assert.IsTrue(clients.Select(item => item.ClientName).Except(input.Select(item => item.ClientName)).Any() is false);
}
}
Loading

0 comments on commit 079a0a5

Please sign in to comment.