diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs new file mode 100644 index 00000000..67a50d37 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs @@ -0,0 +1,8 @@ +using BotSharp.Abstraction.Users.Models; + +namespace BotSharp.Abstraction.Users; + +public interface IAuthenticationHook +{ + Task Authenticate(string id, string password); +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs b/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs index 2e96d782..7ca441fe 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs @@ -11,6 +11,7 @@ public class User public string Email { get; set; } = string.Empty; public string Salt { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; + public string Source { get; set; } = "internal"; public string? ExternalId { get; set; } public string Role { get; set; } = UserRole.Client; public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 8e79687f..e6df922d 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -119,6 +119,7 @@ + diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 9fd1c676..85ab30c9 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -2,6 +2,7 @@ using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Users.Models; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; +using NanoidDotNet; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; @@ -28,8 +29,17 @@ public class UserService : IUserService } record = user; - record.UserName = user.UserName.ToLower(); + record.Email = user.Email.ToLower(); + if (string.IsNullOrEmpty(user.UserName)) + { + var name = record.Email.Split("@").First() + "-" + Nanoid.Generate("123456789botsharp", 6); + record.UserName = name; + } + else + { + record.UserName = user.UserName.ToLower(); + } record.Salt = Guid.NewGuid().ToString("N"); record.Password = Utilities.HashText(user.Password, record.Salt); @@ -47,6 +57,31 @@ public class UserService : IUserService var db = _services.GetRequiredService(); var record = db.GetUserByEmail(userEmail); + if (record == null) + { + // check 3rd party user + var validators = _services.GetServices(); + foreach (var validator in validators) + { + var user = await validator.Authenticate(userEmail, password); + if (user != null) + { + // create a local user record + record = new User + { + UserName = user.UserName, + Email = user.Email, + FirstName = user.FirstName, + LastName = user.LastName, + Source = user.Source, + ExternalId = user.ExternalId + }; + await CreateUser(record); + break; + } + } + } + if (record == null) { return default; @@ -96,7 +131,7 @@ public class UserService : IUserService return tokenHandler.WriteToken(token); } - [MemoryCache(10 * 60)] + [MemoryCache(10 * 60, perInstanceCache: true)] public async Task GetMyProfile() { var db = _services.GetRequiredService(); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs index 42299e6f..2c231e16 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs @@ -36,8 +36,8 @@ public class PluginController : ControllerBase { IsHeader = true }, - new PluginMenuDef("Plugins", link: "/page/plugin", icon: "bx bx-plug", weight: 31), - new PluginMenuDef("Settings", link: "/page/setting", icon: "bx bx-cog", weight: 32), + new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31), + new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32), }; var loader = _services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs b/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs index 721aa7d7..e136a961 100644 --- a/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs @@ -12,7 +12,7 @@ public class DashboardPlugin : IBotSharpPlugin public string Id => "d42a0c21-b461-44f6-ada2-499510d260af"; public string Name => "Dashboard"; public string Description => "Dashboard that offers real-time statistics on model performance, usage trends, and user feedback"; - + public string IconUrl => "https://cdn0.iconfinder.com/data/icons/octicons/1024/dashboard-512.png"; public void RegisterDI(IServiceCollection services, IConfiguration config) { services.AddScoped(); diff --git a/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs index ed0f78db..88b37dbb 100644 --- a/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.HuggingFace/Providers/ChatCompletionProvider.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Agents.Enums; -using BotSharp.Abstraction.Conversations.Settings; using BotSharp.Abstraction.Loggers; using BotSharp.Plugin.HuggingFace.Services; using BotSharp.Plugin.HuggingFace.Settings; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs index 71829d54..ae06a708 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs @@ -8,6 +8,7 @@ public class UserDocument : MongoBase public string Email { get; set; } public string Salt { get; set; } public string Password { get; set; } + public string Source { get; set; } = "internal"; public string? ExternalId { get; set; } public string Role { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs index c9fc4539..44faeb42 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs @@ -17,6 +17,7 @@ public partial class MongoRepository Email = user.Email, Password = user.Password, Salt = user.Salt, + Source = user.Source, ExternalId = user.ExternalId, Role = user.Role } : null; @@ -34,6 +35,7 @@ public partial class MongoRepository Email = user.Email, Password = user.Password, Salt = user.Salt, + Source = user.Source, ExternalId = user.ExternalId, Role = user.Role } : null; @@ -52,6 +54,7 @@ public partial class MongoRepository Salt = user.Salt, Password = user.Password, Email = user.Email, + Source = user.Source, ExternalId = user.ExternalId, Role = user.Role, CreatedTime = DateTime.UtcNow, diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index d4b3e483..edc3e5f3 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -82,7 +82,7 @@ "EnableLlmCompletionLog": false, "EnableExecutionLog": true }, - "Stats": { + "Statistics": { "DataDir": "stats" }, "LlamaSharp": { diff --git a/tests/BotSharp.Plugin.PizzaBot/data/agents/8970b1e5-d260-4e2c-90b1-f1415a257c18/agent.json b/tests/BotSharp.Plugin.PizzaBot/data/agents/8970b1e5-d260-4e2c-90b1-f1415a257c18/agent.json index e57ba894..d05e0481 100644 --- a/tests/BotSharp.Plugin.PizzaBot/data/agents/8970b1e5-d260-4e2c-90b1-f1415a257c18/agent.json +++ b/tests/BotSharp.Plugin.PizzaBot/data/agents/8970b1e5-d260-4e2c-90b1-f1415a257c18/agent.json @@ -3,6 +3,7 @@ "name": "Pizza Bot", "description": "AI assistant that can help customer place pizza order.", "type": "routing", + "inheritAgentId": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a", "createdDateTime": "2023-08-18T10:39:32.2349685Z", "updatedDateTime": "2023-08-18T14:39:32.2349686Z", "iconUrl": "https://cdn-icons-png.flaticon.com/512/6978/6978255.png",