1111using Microsoft . OpenApi . Models ;
1212using Microsoft . IdentityModel . JsonWebTokens ;
1313using BotSharp . OpenAPI . BackgroundServices ;
14+ using System . Text . Json . Serialization ;
15+ using Microsoft . AspNetCore . Authentication ;
1416
1517namespace 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