Allow send 3rd original bearer token.

This commit is contained in:
Haiping Chen 2024-02-05 10:00:28 -06:00
parent 8aa7158932
commit 37ce5f4a76
2 changed files with 12 additions and 4 deletions

View file

@ -7,4 +7,5 @@ public interface IAuthenticationHook
{ {
Task<User> Authenticate(string id, string password); Task<User> Authenticate(string id, string password);
void AddClaims(List<Claim> claims); void AddClaims(List<Claim> claims);
void BeforeSending(Token token);
} }

View file

@ -71,13 +71,13 @@ public class UserService : IUserService
record = db.GetUserByUserName(id); record = db.GetUserByUserName(id);
} }
var hooks = _services.GetServices<IAuthenticationHook>();
if (record == null || record.Source != "internal") if (record == null || record.Source != "internal")
{ {
// check 3rd party user // check 3rd party user
var validators = _services.GetServices<IAuthenticationHook>(); foreach (var hook in hooks)
foreach (var validator in validators)
{ {
var user = await validator.Authenticate(id, password); var user = await hook.Authenticate(id, password);
if (user == null) if (user == null)
{ {
continue; continue;
@ -120,13 +120,20 @@ public class UserService : IUserService
var accessToken = GenerateJwtToken(record); var accessToken = GenerateJwtToken(record);
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(accessToken); var jwt = new JwtSecurityTokenHandler().ReadJwtToken(accessToken);
return new Token var token = new Token
{ {
AccessToken = accessToken, AccessToken = accessToken,
ExpireTime = jwt.Payload.Exp.Value, ExpireTime = jwt.Payload.Exp.Value,
TokenType = "Bearer", TokenType = "Bearer",
Scope = "api" Scope = "api"
}; };
foreach (var hook in hooks)
{
hook.BeforeSending(token);
}
return token;
} }
private string GenerateJwtToken(User user) private string GenerateJwtToken(User user)