11
11
using Microsoft . OpenApi . Models ;
12
12
using Microsoft . IdentityModel . JsonWebTokens ;
13
13
using BotSharp . OpenAPI . BackgroundServices ;
14
+ using System . Text . Json . Serialization ;
15
+ using Microsoft . AspNetCore . Authentication ;
14
16
15
17
namespace BotSharp . OpenAPI ;
16
18
@@ -61,6 +63,9 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
61
63
}
62
64
} ) . AddCookie ( options =>
63
65
{
66
+ // Add these lines for cross-origin cookie support
67
+ options . Cookie . SameSite = Microsoft . AspNetCore . Http . SameSiteMode . None ;
68
+ options . Cookie . SecurePolicy = CookieSecurePolicy . Always ;
64
69
} ) . AddPolicyScheme ( schema , "Mixed authentication" , options =>
65
70
{
66
71
// runs on each request
@@ -82,15 +87,16 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
82
87
} ;
83
88
} ) ;
84
89
90
+ #region OpenId
85
91
// GitHub OAuth
86
92
if ( ! string . IsNullOrWhiteSpace ( config [ "OAuth:GitHub:ClientId" ] ) && ! string . IsNullOrWhiteSpace ( config [ "OAuth:GitHub:ClientSecret" ] ) )
87
93
{
88
94
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
+ } ) ;
94
100
}
95
101
96
102
// Google Identiy OAuth
@@ -100,6 +106,7 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
100
106
{
101
107
options . ClientId = config [ "OAuth:Google:ClientId" ] ;
102
108
options . ClientSecret = config [ "OAuth:Google:ClientSecret" ] ;
109
+ options . Events . OnTicketReceived = OnTicketReceivedContext ;
103
110
} ) ;
104
111
}
105
112
@@ -113,8 +120,9 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
113
120
options . ClientId = config [ "OAuth:Keycloak:ClientId" ] ;
114
121
options . ClientSecret = config [ "OAuth:Keycloak:ClientSecret" ] ;
115
122
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 ;
118
126
} ) ;
119
127
}
120
128
@@ -129,13 +137,17 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
129
137
options . Backchannel = builder . Services . BuildServiceProvider ( )
130
138
. GetRequiredService < IHttpClientFactory > ( )
131
139
. CreateClient ( ) ;
140
+ options . Events . OnTicketReceived = OnTicketReceivedContext ;
132
141
} ) ;
133
142
}
143
+ #endregion
134
144
135
145
// Add services to the container.
136
146
services . AddControllers ( )
137
147
. AddJsonOptions ( options =>
138
148
{
149
+ options . JsonSerializerOptions . PropertyNameCaseInsensitive = true ;
150
+ options . JsonSerializerOptions . Converters . Add ( new JsonStringEnumConverter ( ) ) ;
139
151
options . JsonSerializerOptions . Converters . Add ( new RichContentJsonConverter ( ) ) ;
140
152
options . JsonSerializerOptions . Converters . Add ( new TemplateMessageJsonConverter ( ) ) ;
141
153
} ) ;
@@ -182,6 +194,16 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
182
194
return services ;
183
195
}
184
196
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
+
185
207
/// <summary>
186
208
/// Use Swagger/OpenAPI
187
209
/// </summary>
0 commit comments