IAuthenticationHook

This commit is contained in:
Haiping Chen 2024-01-31 09:15:41 -06:00
parent 8217960b60
commit 5ea2823010
11 changed files with 56 additions and 7 deletions

View file

@ -0,0 +1,8 @@
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Abstraction.Users;
public interface IAuthenticationHook
{
Task<User> Authenticate(string id, string password);
}

View file

@ -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;

View file

@ -119,6 +119,7 @@
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
<PackageReference Include="Fluid.Core" Version="2.5.0" />
<PackageReference Include="Nanoid" Version="3.0.0" />
</ItemGroup>
<ItemGroup>

View file

@ -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<IBotSharpRepository>();
var record = db.GetUserByEmail(userEmail);
if (record == null)
{
// check 3rd party user
var validators = _services.GetServices<IAuthenticationHook>();
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<User> GetMyProfile()
{
var db = _services.GetRequiredService<IBotSharpRepository>();

View file

@ -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<PluginLoader>();

View file

@ -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<IConversationHook, StatsConversationHook>();

View file

@ -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;

View file

@ -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; }

View file

@ -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,

View file

@ -82,7 +82,7 @@
"EnableLlmCompletionLog": false,
"EnableExecutionLog": true
},
"Stats": {
"Statistics": {
"DataDir": "stats"
},
"LlamaSharp": {

View file

@ -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",