forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotSharpOpenApiExtensions.cs
More file actions
259 lines (230 loc) · 9.42 KB
/
BotSharpOpenApiExtensions.cs
File metadata and controls
259 lines (230 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Core.Users.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;
using Microsoft.OpenApi.Models;
using Microsoft.IdentityModel.JsonWebTokens;
using BotSharp.OpenAPI.BackgroundServices;
namespace BotSharp.OpenAPI;
public static class BotSharpOpenApiExtensions
{
private static string policy = "BotSharpCorsPolicy";
/// <summary>
/// Add Swagger/OpenAPI
/// </summary>
/// <param name="services"></param>
/// <param name="config"></param>
/// <returns></returns>
public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection services,
IConfiguration config,
string[] origins,
IHostEnvironment env,
bool enableValidation)
{
services.AddScoped<IUserIdentity, UserIdentity>();
services.AddHostedService<ConversationTimeoutService>();
// Add bearer authentication
var schema = "MIXED_SCHEME";
var builder = services.AddAuthentication(options =>
{
// custom scheme defined in .AddPolicyScheme() below
// inspired from https://weblog.west-wind.com/posts/2022/Mar/29/Combining-Bearer-Token-and-Cookie-Auth-in-ASPNET
options.DefaultScheme = schema;
options.DefaultChallengeScheme = schema;
options.DefaultAuthenticateScheme = schema;
}).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = config["Jwt:Issuer"],
ValidAudience = config["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Jwt:Key"])),
ValidateIssuer = enableValidation,
ValidateAudience = enableValidation,
ValidateLifetime = enableValidation && !env.IsDevelopment(),
ValidateIssuerSigningKey = enableValidation
};
if (!enableValidation)
{
o.TokenValidationParameters.SignatureValidator = (string token, TokenValidationParameters parameters) =>
new JsonWebToken(token);
}
}).AddCookie(options =>
{
}).AddPolicyScheme(schema, "Mixed authentication", options =>
{
// runs on each request
options.ForwardDefaultSelector = context =>
{
// filter by auth type
string authorization = context.Request.Headers[HeaderNames.Authorization];
if (!string.IsNullOrEmpty(authorization) && authorization.StartsWith("Bearer "))
return JwtBearerDefaults.AuthenticationScheme;
else if (context.Request.Cookies.ContainsKey(".AspNetCore.Cookies"))
return CookieAuthenticationDefaults.AuthenticationScheme;
else if (context.Request.Path.StartsWithSegments("/sso") && context.Request.Method == "GET")
return CookieAuthenticationDefaults.AuthenticationScheme;
else if (context.Request.Path.ToString().StartsWith("/signin-") && context.Request.Method == "GET")
return CookieAuthenticationDefaults.AuthenticationScheme;
// otherwise always check for cookie auth
return JwtBearerDefaults.AuthenticationScheme;
};
});
// GitHub OAuth
if (!string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientSecret"]))
{
builder = builder.AddGitHub(options =>
{
options.ClientId = config["OAuth:GitHub:ClientId"];
options.ClientSecret = config["OAuth:GitHub:ClientSecret"];
options.Scope.Add("user:email");
});
}
// Google Identiy OAuth
if (!string.IsNullOrWhiteSpace(config["OAuth:Google:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:Google:ClientSecret"]))
{
builder = builder.AddGoogle(options =>
{
options.ClientId = config["OAuth:Google:ClientId"];
options.ClientSecret = config["OAuth:Google:ClientSecret"];
});
}
// Keycloak Identiy OAuth
if (!string.IsNullOrWhiteSpace(config["OAuth:Keycloak:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:Keycloak:ClientSecret"]))
{
builder = builder.AddKeycloak(options =>
{
options.BaseAddress = new Uri(config["OAuth:Keycloak:BaseAddress"]);
options.Realm = config["OAuth:Keycloak:Realm"];
options.ClientId = config["OAuth:Keycloak:ClientId"];
options.ClientSecret = config["OAuth:Keycloak:ClientSecret"];
options.AccessType = AspNet.Security.OAuth.Keycloak.KeycloakAuthenticationAccessType.Confidential;
int version = Convert.ToInt32(config["OAuth:Keycloak:Version"]??"22") ;
options.Version = new Version(version,0);
});
}
// Wexin OAuth
if (!string.IsNullOrWhiteSpace(config["OAuth:Wexin:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:Wexin:ClientSecret"]))
{
builder = builder.AddWeixin(options =>
{
options.ClientId = config["OAuth:GitHub:ClientId"];
options.ClientSecret = config["OAuth:GitHub:ClientSecret"];
options.Scope.Add("user:email");
options.Backchannel = builder.Services.BuildServiceProvider()
.GetRequiredService<IHttpClientFactory>()
.CreateClient();
});
}
// Add services to the container.
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter());
options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter());
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(
c =>
{
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
}
);
services.AddHttpContextAccessor();
services.AddCors(options =>
{
options.AddPolicy(policy,
builder => builder.WithOrigins(origins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
return services;
}
/// <summary>
/// Use Swagger/OpenAPI
/// </summary>
/// <param name="services"></param>
/// <param name="config"></param>
/// <returns></returns>
public static IApplicationBuilder UseBotSharpOpenAPI(this IApplicationBuilder app, IHostEnvironment env)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
app.UseCors(policy);
app.UseSwagger();
if (env.IsDevelopment())
{
IdentityModelEventSource.ShowPII = true;
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(
endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
return app;
}
/// <summary>
/// Host BotSharp UI built in adapter-static
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IApplicationBuilder UseBotSharpUI(this IApplicationBuilder app, bool isDevelopment = false)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
// app.UseFileServer();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseEndpoints(
endpoints =>
{
// For SPA static file routing
endpoints.MapFallbackToFile("/index.html");
});
app.UseSpa(config =>
{
if (isDevelopment)
{
config.UseProxyToSpaDevelopmentServer("http://localhost:5015");
}
});
return app;
}
}