Skip to content

Commit c45b01a

Browse files
authored
Merge pull request #289 from hchen2020/master
Allow send 3rd original bearer token.
2 parents 8aa7158 + 37ce5f4 commit c45b01a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface IAuthenticationHook
77
{
88
Task<User> Authenticate(string id, string password);
99
void AddClaims(List<Claim> claims);
10+
void BeforeSending(Token token);
1011
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public async Task<Token> GetToken(string authorization)
7171
record = db.GetUserByUserName(id);
7272
}
7373

74+
var hooks = _services.GetServices<IAuthenticationHook>();
7475
if (record == null || record.Source != "internal")
7576
{
7677
// check 3rd party user
77-
var validators = _services.GetServices<IAuthenticationHook>();
78-
foreach (var validator in validators)
78+
foreach (var hook in hooks)
7979
{
80-
var user = await validator.Authenticate(id, password);
80+
var user = await hook.Authenticate(id, password);
8181
if (user == null)
8282
{
8383
continue;
@@ -120,13 +120,20 @@ record = db.GetUserByUserName(id);
120120

121121
var accessToken = GenerateJwtToken(record);
122122
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(accessToken);
123-
return new Token
123+
var token = new Token
124124
{
125125
AccessToken = accessToken,
126126
ExpireTime = jwt.Payload.Exp.Value,
127127
TokenType = "Bearer",
128128
Scope = "api"
129129
};
130+
131+
foreach (var hook in hooks)
132+
{
133+
hook.BeforeSending(token);
134+
}
135+
136+
return token;
130137
}
131138

132139
private string GenerateJwtToken(User user)

0 commit comments

Comments
 (0)