Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c9d1246
feat: Add verification code expiration time
AnonymousDotNet Jan 6, 2025
e70a168
feat: Add verification code expiration prompt
AnonymousDotNet Jan 6, 2025
eb3d00b
fix: null pointer exception
AnonymousDotNet Jan 7, 2025
076284a
Merge pull request #64 from AnonymousDotNet/lida_Dev
Oceania2018 Jan 8, 2025
d793605
Merge branch 'SciSharp:master' into master
Oceania2018 Jan 8, 2025
8e5b7ca
Merge branch 'SciSharp:master' into master
Oceania2018 Jan 9, 2025
8e12434
hdong: add renew token service.
YouWeiDH Jan 9, 2025
be6996c
Merge pull request #65 from Qtoss-AI/hdongDev
Oceania2018 Jan 10, 2025
3f64f36
hdong: add simple login API for new login type.
YouWeiDH Jan 13, 2025
7df494d
Merge branch 'SciSharp:master' into master
Oceania2018 Jan 13, 2025
0e9dd8a
Merge pull request #66 from Qtoss-AI/hdongDev
Oceania2018 Jan 13, 2025
43cf874
hdong: clean up code.
YouWeiDH Jan 14, 2025
30dfcda
Merge branch 'master' into hdongDev
YouWeiDH Jan 14, 2025
a2b5bfd
Merge pull request #67 from Qtoss-AI/hdongDev
Oceania2018 Jan 14, 2025
3f7242a
用post取数据
Oceania2018 Jan 15, 2025
e17d852
hdong: split event to update verification code and send.
YouWeiDH Jan 15, 2025
1fc7bc3
Merge pull request #68 from Qtoss-AI/hdongDev
Oceania2018 Jan 16, 2025
a093562
Merge branch 'SciSharp:master' into master
Oceania2018 Jan 17, 2025
c42727a
Skip Qtoss AI user
Oceania2018 Jan 17, 2025
fe38106
Merge branch 'SciSharp:master' into master
Oceania2018 Jan 18, 2025
122f18f
clean code of sql planner
Oceania2018 Jan 18, 2025
6f4448d
Remove UserSingleLoginFilter
Oceania2018 Jan 18, 2025
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
@@ -1,7 +1,9 @@
using System.Diagnostics;
using System.Net.Http;

namespace BotSharp.Abstraction.Browsing.Models;

[DebuggerStepThrough]
public class HttpRequestParams
{
[JsonPropertyName("url")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool UserAuthenticated(User user, Token token)
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
Task VerificationCodeResetPassword(User user);
Task SendVerificationCode(User user);

/// <summary>
/// Delete users
Expand Down
7 changes: 5 additions & 2 deletions src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public interface IUserService
Task<Token?> GetAdminToken(string authorization);
Task<Token?> GetToken(string authorization);
Task<Token> CreateTokenByUser(User user);
Task<Token> RenewToken();
Task<User> GetMyProfile();
Task<bool> VerifyUserNameExisting(string userName);
Task<bool> VerifyEmailExisting(string email);
Task<bool> VerifyPhoneExisting(string phone, string regionCode);
Task<bool> SendVerificationCodeResetPasswordNoLogin(User user);
Task<bool> SendVerificationCodeResetPasswordLogin();
Task<User> ResetVerificationCode(User user);
Task<bool> SendVerificationCodeNoLogin(User user);
Task<bool> SendVerificationCodeLogin();
Task<bool> SetUserPassword(User user);
Task<bool> ResetUserPassword(User user);
Task<bool> ModifyUserEmail(string email);
Task<bool> ModifyUserPhone(string phone, string regionCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class User
public string Type { get; set; } = UserType.Client;
public string Role { get; set; } = UserRole.User;
public string? VerificationCode { get; set; }
public DateTime? VerificationCodeExpireAt { get; set; }
public bool Verified { get; set; }
public string RegionCode { get; set; } = "CN";
public string? AffiliateId { get; set; }
Expand Down
110 changes: 84 additions & 26 deletions src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ public async Task<User> CreateUser(User user)
record = db.GetUserByUserName(user.UserName);
}

if (record != null && record.Verified)
{
// account is already activated
_logger.LogWarning($"User account already exists: {record.Id} {record.UserName}");
return record;
}

if (!string.IsNullOrWhiteSpace(user.Phone))
if (record == null && !string.IsNullOrWhiteSpace(user.Phone))
{
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
}
Expand All @@ -70,6 +63,13 @@ record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(us
record = db.GetUserByEmail(user.Email);
}

if (record != null && record.Verified)
{
// account is already activated
_logger.LogWarning($"User account already exists: {record.Id} {record.UserName}");
return record;
}

if (record != null)
{
hasRegisterId = record.Id;
Expand All @@ -94,8 +94,13 @@ record = user;
//record.Phone = "+" + Regex.Match(user.Phone, @"\d+").Value;
record.Phone = Regex.Match(user.Phone, @"\d+").Value;
}

record.Salt = Guid.NewGuid().ToString("N");
record.Password = Utilities.HashTextMd5($"{user.Password}{record.Salt}");

if (!string.IsNullOrWhiteSpace(user.Password))
{
record.Password = Utilities.HashTextMd5($"{user.Password}{record.Salt}");
}

if (_setting.NewUserVerification)
{
Expand Down Expand Up @@ -482,7 +487,7 @@ record = db.GetUserByPhone(id, regionCode: (string.IsNullOrWhiteSpace(model.Regi
return default;
}

if (record.VerificationCode != model.VerificationCode)
if (record.VerificationCode != model.VerificationCode || (record.VerificationCodeExpireAt != null && DateTime.UtcNow > record.VerificationCodeExpireAt))
{
return default;
}
Expand Down Expand Up @@ -520,6 +525,16 @@ public async Task<Token> CreateTokenByUser(User user)
return token;
}

public async Task<Token> RenewToken()
{
var newToken = GenerateJwtToken(await GetMyProfile());
var newJwt = new JwtSecurityTokenHandler().ReadJwtToken(newToken);
Token token = new Token();
token.AccessToken = newToken;
token.ExpireTime = newJwt.Payload.Exp.Value;
return token;
}

public async Task<bool> VerifyUserNameExisting(string userName)
{
if (string.IsNullOrEmpty(userName))
Expand Down Expand Up @@ -572,15 +587,32 @@ public async Task<bool> VerifyPhoneExisting(string phone, string regionCode)
return false;
}

public async Task<bool> SendVerificationCodeResetPasswordNoLogin(User user)
public async Task<bool> SendVerificationCodeNoLogin(User user)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
User? record = await ResetVerificationCode(user);

User? record = null;
if (record == null)
{
return false;
}

//send code to user Email.
var hooks = _services.GetServices<IAuthenticationHook>();
foreach (var hook in hooks)
{
await hook.SendVerificationCode(record);
}

return true;
}

public async Task<User> ResetVerificationCode(User user)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
User record = null;
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
{
return false;
return null;
}

if (!string.IsNullOrEmpty(user.Phone))
Expand All @@ -595,25 +627,18 @@ record = db.GetUserByEmail(user.Email);

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

record.VerificationCode = Nanoid.Generate(alphabet: "0123456789", size: 6);

//update current verification code.
db.UpdateUserVerificationCode(record.Id, record.VerificationCode);

//send code to user Email.
var hooks = _services.GetServices<IAuthenticationHook>();
foreach (var hook in hooks)
{
await hook.VerificationCodeResetPassword(record);
}

return true;
return record;
}

public async Task<bool> SendVerificationCodeResetPasswordLogin()
public async Task<bool> SendVerificationCodeLogin()
{
var db = _services.GetRequiredService<IBotSharpRepository>();

Expand All @@ -638,7 +663,7 @@ record = db.GetUserById(_user.Id);
var hooks = _services.GetServices<IAuthenticationHook>();
foreach (var hook in hooks)
{
await hook.VerificationCodeResetPassword(record);
await hook.SendVerificationCode(record);
}

return true;
Expand Down Expand Up @@ -669,7 +694,40 @@ record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(us
return false;
}

if (user.VerificationCode != record.VerificationCode)
if (user.VerificationCode != record.VerificationCode || (record.VerificationCodeExpireAt != null && DateTime.UtcNow > record.VerificationCodeExpireAt))
{
return false;
}

var newPassword = Utilities.HashTextMd5($"{user.Password}{record.Salt}");
db.UpdateUserPassword(record.Id, newPassword);
return true;
}

public async Task<bool> SetUserPassword(User user)
{
if (!string.IsNullOrEmpty(user.Id) && !string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
{
return false;
}
var db = _services.GetRequiredService<IBotSharpRepository>();

User? record = null;

if (!string.IsNullOrEmpty(user.Id))
{
record = db.GetUserById(user.Id);
}
else if (!string.IsNullOrEmpty(user.Phone))
{
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
}
else if (!string.IsNullOrEmpty(user.Email))
{
record = db.GetUserByEmail(user.Email);
}

if (record == null)
{
return false;
}
Expand Down
10 changes: 0 additions & 10 deletions src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.OpenApi.Models;
using Microsoft.IdentityModel.JsonWebTokens;
using BotSharp.OpenAPI.BackgroundServices;
using BotSharp.OpenAPI.Filters;

namespace BotSharp.OpenAPI;

Expand All @@ -33,15 +32,6 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
services.AddScoped<IUserIdentity, UserIdentity>();
services.AddHostedService<ConversationTimeoutService>();

var enableSingleLogin = bool.Parse(config["Jwt:EnableSingleLogin"] ?? "false");
if (enableSingleLogin)
{
services.AddMvc(options =>
{
options.Filters.Add<UserSingleLoginFilter>();
});
}

// Add bearer authentication
var schema = "MIXED_SCHEME";
var builder = services.AddAuthentication(options =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ public async Task<bool> VerifyPhoneExisting([FromQuery] string phone, [FromQuery
[HttpPost("/user/verifycode-out")]
public async Task<bool> SendVerificationCodeResetPassword([FromBody] UserCreationModel user)
{
return await _userService.SendVerificationCodeResetPasswordNoLogin(user.ToUser());
return await _userService.SendVerificationCodeNoLogin(user.ToUser());
}

[HttpPost("/user/verifycode-in")]
public async Task<bool> SendVerificationCodeResetPasswordLogined()
{
return await _userService.SendVerificationCodeResetPasswordLogin();
return await _userService.SendVerificationCodeLogin();
}

[AllowAnonymous]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class UserDocument : MongoBase
public string Type { get; set; } = UserType.Client;
public string Role { get; set; } = null!;
public string? VerificationCode { get; set; }
public DateTime? VerificationCodeExpireAt { get; set; }
public bool Verified { get; set; }
public string? RegionCode { get; set; }
public string? AffiliateId { get; set; }
Expand Down Expand Up @@ -48,6 +49,7 @@ public User ToUser()
EmployeeId = EmployeeId,
IsDisabled = IsDisabled,
VerificationCode = VerificationCode,
VerificationCodeExpireAt = VerificationCodeExpireAt,
Verified = Verified,
RegionCode = RegionCode,
Permissions = Permissions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void UpdateUserVerificationCode(string userId, string verficationCode)
{
var filter = Builders<UserDocument>.Filter.Eq(x => x.Id, userId);
var update = Builders<UserDocument>.Update.Set(x => x.VerificationCode, verficationCode)
.Set(x => x.VerificationCodeExpireAt, DateTime.UtcNow.AddMinutes(5))
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Users.UpdateOne(filter, update);
}
Expand Down
Loading