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);
void AddClaims(List<Claim> claims);
void BeforeSending(Token token);
}

View file

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