add client & affiliate token

This commit is contained in:
jason.wang 2024-09-20 15:48:34 +08:00
parent cadec5c2a7
commit 47520736ba
3 changed files with 32 additions and 5 deletions

View file

@ -8,7 +8,8 @@ public interface IUserService
Task<User> GetUser(string id);
Task<User> CreateUser(User user);
Task<Token> ActiveUser(UserActivationModel model);
Task<Token?> GetToken(string authorization);
Task<Token?> GetAffiliateToken(string authorization);
Task<Token?> GetClientToken(string authorization);
Task<User> GetMyProfile();
Task<bool> VerifyUserNameExisting(string userName);
Task<bool> VerifyEmailExisting(string email);

View file

@ -106,12 +106,27 @@ public class UserService : IUserService
return true;
}
public async Task<Token?> GetToken(string authorization)
public async Task<Token> GetAffiliateToken(string authorization)
{
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
var (id, password) = base64.SplitAsTuple(":");
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.GetUserByPhone(id);
var isCanLoginAffiliateRoleType = record == null && record.Type != UserType.Client;
if (isCanLoginAffiliateRoleType)
{
return await GetToken(record, id, password);
}
return default;
}
public async Task<Token> GetClientToken(string authorization)
{
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
var (id, password) = base64.SplitAsTuple(":");
var hooks = _services.GetServices<IAuthenticationHook>();
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id);
if (record == null)
@ -119,6 +134,17 @@ public class UserService : IUserService
record = db.GetUserByUserName(id);
}
if (record != null && record.Type == UserType.Affiliate)
{
return default;
}
return await GetToken(record, id, password);
}
private async Task<Token?> GetToken(User record, string id, string password)
{
var hooks = _services.GetServices<IAuthenticationHook>();
//verify password is correct or not.
if (record != null && !hooks.Any())
{
@ -242,7 +268,7 @@ public class UserService : IUserService
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(tokenDescriptor);
SaveUserTokenExpiresCache(user.Id,expires).GetAwaiter().GetResult();
SaveUserTokenExpiresCache(user.Id, expires).GetAwaiter().GetResult();
return tokenHandler.WriteToken(token);
}

View file

@ -25,7 +25,7 @@ public class UserController : ControllerBase
authcode = authcode.Split(' ')[1];
}
var token = await _userService.GetToken(authcode);
var token = await _userService.GetClientToken(authcode);
if (token == null)
{