Skip to content

Commit 854e6bf

Browse files
committed
OnTicketReceivedContext
1 parent 365c1db commit 854e6bf

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Users.Models;
2+
using Microsoft.AspNetCore.Authentication;
23
using System.Security.Claims;
34

45
namespace BotSharp.Abstraction.Users;
@@ -11,7 +12,8 @@ public interface IAuthenticationHook
1112
/// <param name="id"></param>
1213
/// <param name="password"></param>
1314
/// <returns></returns>
14-
Task<User> Authenticate(string id, string password);
15+
Task<User> Authenticate(string id, string password)
16+
=> Task.FromResult(new User());
1517

1618
/// <summary>
1719
/// Add extra claims to user
@@ -30,31 +32,38 @@ bool AddClaims(List<Claim> claims)
3032
bool UserAuthenticated(User user, Token token)
3133
=> true;
3234

35+
Task OAuthCompleted(TicketReceivedContext context)
36+
=> Task.CompletedTask;
37+
3338
/// <summary>
3439
/// Bfore user updating
3540
/// </summary>
3641
/// <param name="user"></param>
3742
/// <returns></returns>
38-
Task UserUpdating(User user);
43+
Task UserUpdating(User user)
44+
=> Task.CompletedTask;
3945

4046
/// <summary>
4147
/// After user created
4248
/// </summary>
4349
/// <param name="user"></param>
4450
/// <returns></returns>
45-
Task UserCreated(User user);
51+
Task UserCreated(User user)
52+
=> Task.CompletedTask;
4653

4754
/// <summary>
4855
/// Reset password
4956
/// </summary>
5057
/// <param name="user"></param>
5158
/// <returns></returns>
52-
Task SendVerificationCode(User user);
59+
Task SendVerificationCode(User user)
60+
=> Task.CompletedTask;
5361

5462
/// <summary>
5563
/// Delete users
5664
/// </summary>
5765
/// <param name="userIds"></param>
5866
/// <returns></returns>
59-
Task DelUsers(List<string> userIds);
67+
Task DelUsers(List<string> userIds)
68+
=> Task.CompletedTask;
6069
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ record = db.GetUserByPhone(id, regionCode: regionCode);
254254
foreach (var hook in hooks)
255255
{
256256
user = await hook.Authenticate(id, password);
257-
if (user == null)
257+
if (user == null || string.IsNullOrEmpty(user.Id))
258258
{
259259
continue;
260260
}

src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.OpenApi.Models;
1212
using Microsoft.IdentityModel.JsonWebTokens;
1313
using BotSharp.OpenAPI.BackgroundServices;
14+
using System.Text.Json.Serialization;
15+
using Microsoft.AspNetCore.Authentication;
1416

1517
namespace BotSharp.OpenAPI;
1618

@@ -61,6 +63,9 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
6163
}
6264
}).AddCookie(options =>
6365
{
66+
// Add these lines for cross-origin cookie support
67+
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None;
68+
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
6469
}).AddPolicyScheme(schema, "Mixed authentication", options =>
6570
{
6671
// runs on each request
@@ -82,15 +87,16 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
8287
};
8388
});
8489

90+
#region OpenId
8591
// GitHub OAuth
8692
if (!string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientSecret"]))
8793
{
8894
builder = builder.AddGitHub(options =>
89-
{
90-
options.ClientId = config["OAuth:GitHub:ClientId"];
91-
options.ClientSecret = config["OAuth:GitHub:ClientSecret"];
92-
options.Scope.Add("user:email");
93-
});
95+
{
96+
options.ClientId = config["OAuth:GitHub:ClientId"];
97+
options.ClientSecret = config["OAuth:GitHub:ClientSecret"];
98+
options.Events.OnTicketReceived = OnTicketReceivedContext;
99+
});
94100
}
95101

96102
// Google Identiy OAuth
@@ -100,6 +106,7 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
100106
{
101107
options.ClientId = config["OAuth:Google:ClientId"];
102108
options.ClientSecret = config["OAuth:Google:ClientSecret"];
109+
options.Events.OnTicketReceived = OnTicketReceivedContext;
103110
});
104111
}
105112

@@ -113,8 +120,9 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
113120
options.ClientId = config["OAuth:Keycloak:ClientId"];
114121
options.ClientSecret = config["OAuth:Keycloak:ClientSecret"];
115122
options.AccessType = AspNet.Security.OAuth.Keycloak.KeycloakAuthenticationAccessType.Confidential;
116-
int version = Convert.ToInt32(config["OAuth:Keycloak:Version"]??"22") ;
117-
options.Version = new Version(version,0);
123+
int version = Convert.ToInt32(config["OAuth:Keycloak:Version"] ?? "22");
124+
options.Version = new Version(version, 0);
125+
options.Events.OnTicketReceived = OnTicketReceivedContext;
118126
});
119127
}
120128

@@ -129,13 +137,17 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
129137
options.Backchannel = builder.Services.BuildServiceProvider()
130138
.GetRequiredService<IHttpClientFactory>()
131139
.CreateClient();
140+
options.Events.OnTicketReceived = OnTicketReceivedContext;
132141
});
133142
}
143+
#endregion
134144

135145
// Add services to the container.
136146
services.AddControllers()
137147
.AddJsonOptions(options =>
138148
{
149+
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
150+
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
139151
options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter());
140152
options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter());
141153
});
@@ -182,6 +194,16 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
182194
return services;
183195
}
184196

197+
private static async Task OnTicketReceivedContext(TicketReceivedContext context)
198+
{
199+
var services = context.HttpContext.RequestServices;
200+
var hooks = services.GetServices<IAuthenticationHook>();
201+
foreach (var hook in hooks)
202+
{
203+
await hook.OAuthCompleted(context);
204+
}
205+
}
206+
185207
/// <summary>
186208
/// Use Swagger/OpenAPI
187209
/// </summary>

0 commit comments

Comments
 (0)