Skip to content

Commit 4752073

Browse files
author
jason.wang
committed
add client & affiliate token
1 parent cadec5c commit 4752073

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ public interface IUserService
88
Task<User> GetUser(string id);
99
Task<User> CreateUser(User user);
1010
Task<Token> ActiveUser(UserActivationModel model);
11-
Task<Token?> GetToken(string authorization);
11+
Task<Token?> GetAffiliateToken(string authorization);
12+
Task<Token?> GetClientToken(string authorization);
1213
Task<User> GetMyProfile();
1314
Task<bool> VerifyUserNameExisting(string userName);
1415
Task<bool> VerifyEmailExisting(string email);

src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,45 @@ public async Task<bool> UpdatePassword(string password, string verificationCode)
106106
return true;
107107
}
108108

109-
public async Task<Token?> GetToken(string authorization)
109+
public async Task<Token> GetAffiliateToken(string authorization)
110+
{
111+
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
112+
var (id, password) = base64.SplitAsTuple(":");
113+
var db = _services.GetRequiredService<IBotSharpRepository>();
114+
var record = db.GetUserByPhone(id);
115+
116+
var isCanLoginAffiliateRoleType = record == null && record.Type != UserType.Client;
117+
if (isCanLoginAffiliateRoleType)
118+
{
119+
return await GetToken(record, id, password);
120+
}
121+
122+
return default;
123+
}
124+
125+
public async Task<Token> GetClientToken(string authorization)
110126
{
111127
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
112128
var (id, password) = base64.SplitAsTuple(":");
113129

114-
var hooks = _services.GetServices<IAuthenticationHook>();
115130
var db = _services.GetRequiredService<IBotSharpRepository>();
116131
var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id);
117132
if (record == null)
118133
{
119134
record = db.GetUserByUserName(id);
120135
}
121136

137+
if (record != null && record.Type == UserType.Affiliate)
138+
{
139+
return default;
140+
}
141+
142+
return await GetToken(record, id, password);
143+
}
144+
145+
private async Task<Token?> GetToken(User record, string id, string password)
146+
{
147+
var hooks = _services.GetServices<IAuthenticationHook>();
122148
//verify password is correct or not.
123149
if (record != null && !hooks.Any())
124150
{
@@ -242,7 +268,7 @@ private string GenerateJwtToken(User user)
242268
};
243269
var tokenHandler = new JwtSecurityTokenHandler();
244270
var token = tokenHandler.CreateToken(tokenDescriptor);
245-
SaveUserTokenExpiresCache(user.Id,expires).GetAwaiter().GetResult();
271+
SaveUserTokenExpiresCache(user.Id, expires).GetAwaiter().GetResult();
246272
return tokenHandler.WriteToken(token);
247273
}
248274

src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task<ActionResult<Token>> GetToken([FromHeader(Name = "Authorizatio
2525
authcode = authcode.Split(' ')[1];
2626
}
2727

28-
var token = await _userService.GetToken(authcode);
28+
var token = await _userService.GetClientToken(authcode);
2929

3030
if (token == null)
3131
{

0 commit comments

Comments
 (0)