-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPresentationServiceCollectionExtensions.cs
More file actions
249 lines (216 loc) · 9.3 KB
/
PresentationServiceCollectionExtensions.cs
File metadata and controls
249 lines (216 loc) · 9.3 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
using SignFabric.ActionFilter;
using SignFabric.Application.Identity;
using SignFabric.Infrastructure.Identity;
using SignFabric.Presentation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using LiteDB.Identity.Extensions;
using LiteDB.Identity.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;
using TXTextControl.Web;
using TXTextControl.Web.DocumentEditor.Backend;
namespace SignFabric {
public static class PresentationServiceCollectionExtensions {
public static IServiceCollection AddPresentation(this IServiceCollection services, IConfiguration configuration) {
var connectionString = configuration.GetConnectionString("IdentityLiteDB");
EnsureLiteDbDirectory(connectionString);
services.AddAuthentication(options => {
options.DefaultScheme = "Identity.Application";
options.DefaultSignInScheme = "Identity.External";
})
.AddCookie("Identity.Application")
.AddCookie("Identity.External")
.AddCookie(IdentityConstants.TwoFactorUserIdScheme)
.AddCookie(IdentityConstants.TwoFactorRememberMeScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => {
var bearer = configuration.GetSection("Authentication:Bearer");
var localOAuth = configuration.GetSection("Authentication:LocalOAuth");
var authority = bearer["Authority"];
var audience = bearer["Audience"];
var localEnabled = localOAuth.GetValue("Enabled", false);
var localIssuer = localOAuth["Issuer"];
var localAudience = localOAuth["Audience"];
var localSigningKey = localOAuth["SigningKey"];
if (!string.IsNullOrWhiteSpace(authority)) {
options.Authority = authority;
}
if (!string.IsNullOrWhiteSpace(audience)) {
options.Audience = audience;
}
options.RequireHttpsMetadata = bearer.GetValue("RequireHttpsMetadata", true);
options.MapInboundClaims = false;
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuer = !string.IsNullOrWhiteSpace(authority) || localEnabled,
ValidIssuers = new[] { localIssuer }.Where(value => !string.IsNullOrWhiteSpace(value)),
ValidateAudience = !string.IsNullOrWhiteSpace(audience) || !string.IsNullOrWhiteSpace(localAudience),
ValidAudiences = new[] { audience, localAudience }.Where(value => !string.IsNullOrWhiteSpace(value)),
ValidateIssuerSigningKey = localEnabled,
IssuerSigningKey = localEnabled && !string.IsNullOrWhiteSpace(localSigningKey)
? new SymmetricSecurityKey(Encoding.UTF8.GetBytes(localSigningKey))
: null
};
});
var oidc = configuration.GetSection("Authentication:OpenIdConnect");
if (oidc.GetValue("Enabled", false)) {
services
.AddAuthentication()
.AddOpenIdConnect("OpenIdConnect", oidc["DisplayName"] ?? "Single Sign-On", options => {
options.Authority = oidc["Authority"];
options.ClientId = oidc["ClientId"];
options.ClientSecret = oidc["ClientSecret"];
options.CallbackPath = oidc["CallbackPath"] ?? "/signin-oidc";
options.SignedOutCallbackPath = oidc["SignedOutCallbackPath"] ?? "/signout-callback-oidc";
options.ResponseType = oidc["ResponseType"] ?? "code";
options.SaveTokens = oidc.GetValue("SaveTokens", true);
options.SignInScheme = IdentityConstants.ExternalScheme;
options.RequireHttpsMetadata = oidc.GetValue("RequireHttpsMetadata", true);
options.Scope.Clear();
foreach (var scope in oidc.GetSection("Scopes").Get<string[]>() ?? new[] { "openid", "profile", "email" }) {
if (!string.IsNullOrWhiteSpace(scope)) {
options.Scope.Add(scope);
}
}
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "roles";
});
}
services.AddLiteDBIdentity(connectionString)
.AddRoles<LiteDbRole>()
.AddDefaultTokenProviders()
.AddDefaultUI();
services.AddAuthorization(options => {
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.AddPolicy(ApiAuthorization.EnvelopeCreatePolicy, policy => {
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
policy.RequireAssertion(context =>
ApiAuthorization.HasPermission(context.User, ApiAuthorization.EnvelopeCreatePermission));
});
options.AddPolicy(ApiAuthorization.EnvelopeReadPolicy, policy => {
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
policy.RequireAssertion(context =>
ApiAuthorization.HasPermission(context.User, ApiAuthorization.EnvelopeReadPermission) ||
ApiAuthorization.HasPermission(context.User, ApiAuthorization.EnvelopeCreatePermission));
});
});
services.AddRazorPages();
services.AddControllersWithViews(options => {
options.Conventions.Add(new TextControlAnonymousControllerConvention());
});
services.AddHostedService<DocumentEditorWorkerManager>();
services.AddHostedService<IdentityBootstrapHostedService>();
return services;
}
private static void EnsureLiteDbDirectory(string connectionString) {
if (string.IsNullOrWhiteSpace(connectionString)) {
throw new InvalidOperationException("The IdentityLiteDB connection string is missing.");
}
var builder = new DbConnectionStringBuilder {
ConnectionString = connectionString
};
if (!builder.TryGetValue("Filename", out var filenameValue)) {
throw new InvalidOperationException("The IdentityLiteDB connection string must contain a Filename value.");
}
var directory = Path.GetDirectoryName(Path.GetFullPath(filenameValue.ToString()));
if (!string.IsNullOrWhiteSpace(directory)) {
Directory.CreateDirectory(directory);
}
}
public static WebApplication UsePresentationPipeline(this WebApplication app) {
app.UseExceptionHandler(errorApp => {
errorApp.Run(HandleExceptionAsync);
});
app.UseStatusCodePagesWithReExecute("/Error/{0}");
if (!app.Environment.IsDevelopment()) {
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseWebSockets();
app.UseTXWebSocketMiddleware();
app.UseMiddleware<OpenedMiddleware>();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapRazorPages();
app.MapControllerRoute(
name: "textcontrol-resources",
pattern: "TextControl/{action}/{id?}")
.AllowAnonymous();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}");
return app;
}
private static async Task HandleExceptionAsync(HttpContext context) {
var feature = context.Features.Get<IExceptionHandlerFeature>();
var message = GetUserErrorMessage(feature?.Error);
if (IsTextControlSigningRequest(context.Request, feature?.Error)) {
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync(message);
return;
}
if (WantsJsonError(context.Request)) {
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonSerializer.Serialize(new {
success = false,
error = message
}));
return;
}
context.Response.Redirect("/Error/500");
}
private static bool WantsJsonError(HttpRequest request) {
if (string.Equals(request.Headers["X-Requested-With"], "XMLHttpRequest", StringComparison.OrdinalIgnoreCase)) {
return true;
}
if (request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase)) {
return true;
}
var accept = request.Headers.Accept.ToString();
return accept.Contains("application/json", StringComparison.OrdinalIgnoreCase) &&
!accept.Contains("text/html", StringComparison.OrdinalIgnoreCase);
}
private static bool IsTextControlSigningRequest(HttpRequest request, Exception exception) {
var path = request.Path.Value ?? string.Empty;
if (path.Contains("SignDocument", StringComparison.OrdinalIgnoreCase)) {
return true;
}
return string.Equals(exception?.Source, "TXTextControl.Web.MVC.DocumentViewer", StringComparison.OrdinalIgnoreCase);
}
private static string GetUserErrorMessage(Exception exception) {
if (string.Equals(exception?.Source, "TXTextControl.Web.MVC.DocumentViewer", StringComparison.OrdinalIgnoreCase)) {
return "The signing session could not be completed. Please reload the signing page and try again.";
}
if (exception is InvalidOperationException && !string.IsNullOrWhiteSpace(exception.Message)) {
return exception.Message;
}
if (exception is UnauthorizedAccessException) {
return "You do not have access to this item.";
}
return "Something went wrong while processing your request. Please try again.";
}
}
}