From 37ce5f4a7678a8d460bc1dd289d4a85de0ecc3c0 Mon Sep 17 00:00:00 2001 From: Haiping Chen <101423@smsassist.com> Date: Mon, 5 Feb 2024 10:00:28 -0600 Subject: [PATCH] Allow send 3rd original bearer token. --- .../Users/IAuthenticationHook.cs | 1 + .../BotSharp.Core/Users/Services/UserService.cs | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs index 267a997c..33b8086f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs @@ -7,4 +7,5 @@ public interface IAuthenticationHook { Task Authenticate(string id, string password); void AddClaims(List claims); + void BeforeSending(Token token); } diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index c9521dff..ce9a9d60 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -71,13 +71,13 @@ public class UserService : IUserService record = db.GetUserByUserName(id); } + var hooks = _services.GetServices(); if (record == null || record.Source != "internal") { // check 3rd party user - var validators = _services.GetServices(); - 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)