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

WebDriver improvement #475

Merged
merged 25 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e18845e
hdong:hard code page size to 10 for Qtoss project currently.
YouWeiDH Apr 25, 2024
8e2a97e
Add UserLanguage to IUserIdentity
Oceania2018 May 12, 2024
2651db2
Merge branch 'master' of https://github.com/White-Tailed-Eagle/BotSharp
Oceania2018 May 12, 2024
8cdab6b
Merge branch 'SciSharp:master' into master
Oceania2018 May 13, 2024
81d556c
Merge branch 'SciSharp:master' into master
Oceania2018 May 13, 2024
5610fb3
Merge branch 'SciSharp:master' into master
Oceania2018 May 13, 2024
151ec19
Merge branch 'SciSharp:master' into master
Oceania2018 May 15, 2024
8eee1fd
Change SendHttpRequest args
Oceania2018 May 17, 2024
018d889
Add UserCreated hook.
Oceania2018 May 21, 2024
3fd4396
Merge branch 'SciSharp:master' into master
Oceania2018 May 22, 2024
f584c31
Fix UserService.
Oceania2018 May 23, 2024
e8656a9
Add new user verification code.
Oceania2018 May 26, 2024
76451bf
Merge branch 'SciSharp:master' into master
Oceania2018 May 27, 2024
8ff4d06
Merge branch 'master' into hdongDev
YouWeiDH May 28, 2024
0ffabd3
hdong: #65.
YouWeiDH May 28, 2024
bcf4198
Merge branch 'SciSharp:master' into master
Oceania2018 May 28, 2024
c32c4f5
Merge branch 'master' into hdongDev
YouWeiDH May 28, 2024
0cf0e98
Merge branch 'SciSharp:master' into master
Oceania2018 May 28, 2024
71278f2
Merge branch 'master' into hdongDev
YouWeiDH May 29, 2024
c9428cd
hdong: clean up code for Pagination size.
YouWeiDH May 29, 2024
8a9b6f3
hdong: remove the limit.
YouWeiDH May 29, 2024
9fc7f1c
hdong:change request method and API name.
YouWeiDH May 29, 2024
54da560
Merge pull request #1 from Qtoss-AI/hdongDev
Oceania2018 May 29, 2024
9213c7f
Merge branch 'SciSharp:master' into master
Oceania2018 May 29, 2024
e9ae471
Remove openIfNotExist from LaunchBrowser
Oceania2018 May 29, 2024
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 @@ -4,7 +4,7 @@ namespace BotSharp.Abstraction.Browsing;

public interface IWebBrowser
{
Task<BrowserActionResult> LaunchBrowser(string contextId, string? url, bool openIfNotExist = true);
Task<BrowserActionResult> LaunchBrowser(string contextId, string? url);
Task<BrowserActionResult> ScreenshotAsync(string contextId, string path);
Task<BrowserActionResult> ScrollPageAsync(BrowserActionParams actionParams);

Expand All @@ -24,6 +24,6 @@ public interface IWebBrowser
Task<T> EvaluateScript<T>(string contextId, string script);
Task CloseBrowser(string contextId);
Task CloseCurrentPage(string contextId);
Task<BrowserActionResult> SendHttpRequest(string contextId, HttpRequestParams actionParams);
Task<BrowserActionResult> SendHttpRequest(MessageInfo message, HttpRequestParams actionParams);
Task<BrowserActionResult> GetAttributeValue(MessageInfo message, ElementLocatingArgs location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public class BrowserActionResult
public string Selector { get; set; }
public string Body { get; set; }
public bool IsHighlighted { get; set; }

public override string ToString()
{
return $"{IsSuccess} - {Selector}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ElementActionArgs

public ElementPosition? Position { get; set; }

public string? PressKey { get; set; }

/// <summary>
/// Required for deserialization
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ public class ElementLocatingArgs
[JsonPropertyName("match_rule")]
public string MatchRule { get; set; } = string.Empty;

[JsonPropertyName("tag")]
public string? Tag { get; set; } = null!;

[JsonPropertyName("text")]
public string? Text { get; set; }

Expand All @@ -20,6 +23,8 @@ public class ElementLocatingArgs
[JsonPropertyName("selector")]
public string? Selector { get; set; }

public bool Parent { get; set; }

public bool FailIfMultiple { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HttpRequestParams

public HttpRequestParams(string url, HttpMethod method, string? payload = null)
{
Method = HttpMethod.Get;
Method = method;
Url = url;
Payload = payload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ public interface IBotSharpRepository
#endregion

#region User
User? GetUserByEmail(string email);
User? GetUserById(string id);
User? GetUserByUserName(string userName);
void CreateUser(User user);
User? GetUserByEmail(string email) => throw new NotImplementedException();
User? GetUserById(string id) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void CreateUser(User user) => throw new NotImplementedException();
void UpdateUserVerified(string userId) => throw new NotImplementedException();
#endregion

#region Agent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface IAuthenticationHook
Task<User> Authenticate(string id, string password);
void AddClaims(List<Claim> claims);
void BeforeSending(Token token);
Task UserCreated(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface IUserIdentity
string FirstName { get; }
string LastName { get; }
string FullName { get; }
string? UserLanguage { get; }
}
4 changes: 4 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using BotSharp.Abstraction.Users.Models;
using BotSharp.OpenAPI.ViewModels.Users;

namespace BotSharp.Abstraction.Users;

public interface IUserService
{
Task<User> GetUser(string id);
Task<User> CreateUser(User user);
Task<Token> ActiveUser(UserActivationModel model);
Task<Token?> GetToken(string authorization);
Task<User> GetMyProfile();
Task<bool> VerifyUserUnique(string userName);
Task<bool> VerifyEmailUnique(string email);
}
2 changes: 2 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class User
public string Source { get; set; } = "internal";
public string? ExternalId { get; set; }
public string Role { get; set; } = UserRole.Client;
public string? VerificationCode { get; set; }
public bool Verified { get; set; }
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotSharp.OpenAPI.ViewModels.Users;

public class UserActivationModel
{
public string UserName { get; set; }
public string VerificationCode { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BotSharp.Abstraction.Users.Settings;

public class AccountSetting
{
/// <summary>
/// Whether to enable verification code to verify the authenticity of new users
/// </summary>
public bool NewUserVerification { get; set; }
}
11 changes: 4 additions & 7 deletions src/Infrastructure/BotSharp.Abstraction/Utilities/Pagination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ public int Page

public int Size
{
get
get
{
if (_size <= 0) return 20;
if (_size > 100) return 100;

return _size;
}
set
}
set
{
_size = value;
}
}
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using BotSharp.Abstraction.Settings;
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Users.Settings;

namespace BotSharp.Core;

Expand Down Expand Up @@ -84,6 +85,10 @@ public static void RegisterPlugins(IServiceCollection services, IConfiguration c
return settingService.Bind<PluginSettings>("PluginLoader");
});

var accountSettings = new AccountSetting();
config.Bind("Account", accountSettings);
services.AddScoped(x => accountSettings);

var loader = new PluginLoader(services, config, pluginSettings);
loader.Load(assembly =>
{
Expand Down
15 changes: 0 additions & 15 deletions src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Abstraction.Tasks.Models;
using BotSharp.Abstraction.Users.Models;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -176,20 +175,6 @@ public IEnumerable<string> TruncateConversation(string conversationId, string me
=> throw new NotImplementedException();
#endregion

#region User
public User? GetUserByEmail(string email)
=> throw new NotImplementedException();

public User? GetUserById(string id)
=> throw new NotImplementedException();

public User? GetUserByUserName(string userName)
=> throw new NotImplementedException();

public void CreateUser(User user)
=> throw new NotImplementedException();
#endregion

#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ public void CreateUser(User user)
var path = Path.Combine(dir, "user.json");
File.WriteAllText(path, JsonSerializer.Serialize(user, _options));
}

public void UpdateUserVerified(string userId)
{
var user = GetUserById(userId);
user.Verified = true;
var dir = Path.Combine(_dbSettings.FileRepository, "users", user.Id);
var path = Path.Combine(dir, "user.json");
File.WriteAllText(path, JsonSerializer.Serialize(user, _options));
}
}
10 changes: 10 additions & 0 deletions src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ public string FullName
return $"{FirstName} {LastName}".Trim();
}
}

[JsonPropertyName("user_language")]
public string? UserLanguage
{
get
{
_contextAccessor.HttpContext.Request.Headers.TryGetValue("User-Language", out var languages);
return languages.FirstOrDefault();
}
}
}
93 changes: 91 additions & 2 deletions src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BotSharp.Abstraction.Users.Models;
using BotSharp.Abstraction.Users.Settings;
using BotSharp.OpenAPI.ViewModels.Users;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using NanoidDotNet;
Expand All @@ -12,12 +14,17 @@ public class UserService : IUserService
private readonly IServiceProvider _services;
private readonly IUserIdentity _user;
private readonly ILogger _logger;
private readonly AccountSetting _setting;

public UserService(IServiceProvider services, IUserIdentity user, ILogger<UserService> logger)
public UserService(IServiceProvider services,
IUserIdentity user,
ILogger<UserService> logger,
AccountSetting setting)
{
_services = services;
_user = user;
_logger = logger;
_setting = setting;
}

public async Task<User> CreateUser(User user)
Expand Down Expand Up @@ -51,11 +58,23 @@ record = user;
record.Salt = Guid.NewGuid().ToString("N");
record.Password = Utilities.HashText(user.Password, record.Salt);

if (_setting.NewUserVerification)
{
record.VerificationCode = Nanoid.Generate(alphabet: "0123456789", size: 6);
record.Verified = false;
}

db.CreateUser(record);

_logger.LogWarning($"Created new user account: {record.Id} {record.UserName}");
Utilities.ClearCache();

var hooks = _services.GetServices<IAuthenticationHook>();
foreach (var hook in hooks)
{
await hook.UserCreated(record);
}

return record;
}

Expand All @@ -71,7 +90,7 @@ record = user;
record = db.GetUserByUserName(id);
}

User? user = null;
User? user = record;
var hooks = _services.GetServices<IAuthenticationHook>();
if (record == null || record.Source != "internal")
{
Expand Down Expand Up @@ -114,6 +133,11 @@ record = db.GetUserByUserName(id);
return default;
}

if (_setting.NewUserVerification && !record.Verified)
{
return default;
}

#if !DEBUG
if (Utilities.HashText(password, record.Salt) != record.Password)
{
Expand Down Expand Up @@ -200,4 +224,69 @@ public async Task<User> GetUser(string id)
var user = db.GetUserById(id);
return user;
}

public async Task<Token> ActiveUser(UserActivationModel model)
{
var id = model.UserName;
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id);
if (record == null)
{
record = db.GetUserByUserName(id);
}

if (record == null)
{
return default;
}

if (record.VerificationCode != model.VerificationCode)
{
return default;
}

if (record.Verified)
{
return default;
}

db.UpdateUserVerified(record.Id);

var accessToken = GenerateJwtToken(record);
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(accessToken);
var token = new Token
{
AccessToken = accessToken,
ExpireTime = jwt.Payload.Exp.Value,
TokenType = "Bearer",
Scope = "api"
};
return token;
}

public async Task<bool> VerifyUserUnique(string userName)
{
if (string.IsNullOrEmpty(userName))
return false;

var db = _services.GetRequiredService<IBotSharpRepository>();
var user = db.GetUserByUserName(userName);
if (user == null)
return true;

return false;
}

public async Task<bool> VerifyEmailUnique(string email)
{
if (string.IsNullOrEmpty(email))
return false;

var db = _services.GetRequiredService<IBotSharpRepository>();
var emailName = db.GetUserByEmail(email);
if (emailName == null)
return true;

return false;
}
}
Loading