-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebServiceExtensions.cs
More file actions
319 lines (280 loc) · 13.4 KB
/
WebServiceExtensions.cs
File metadata and controls
319 lines (280 loc) · 13.4 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
using Aquiis.Core.Interfaces;
using Aquiis.Core.Interfaces.Services;
using Aquiis.Application; // ✅ Application facade
using Aquiis.Application.Services;
using Aquiis.Infrastructure.Data; // For SqlCipherConnectionInterceptor
using Aquiis.SimpleStart.Data;
using Aquiis.SimpleStart.Entities;
using Aquiis.SimpleStart.Services; // For ElectronPathService, WebPathService
using Aquiis.Infrastructure.Services; // For DatabaseUnlockState
using Aquiis.Infrastructure.Interfaces; // For IKeychainService
using Microsoft.Data.Sqlite;
namespace Aquiis.SimpleStart.Extensions;
/// <summary>
/// Extension methods for configuring Web-specific services for SimpleStart.
/// </summary>
public static class WebServiceExtensions
{
// Toggle for verbose logging (useful for troubleshooting encryption setup)
private const bool EnableVerboseLogging = false;
/// <summary>
/// Adds all Web-specific infrastructure services including database and identity.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configuration">The application configuration.</param>
/// <returns>The service collection for chaining.</returns>
public static IServiceCollection AddWebServices(
this IServiceCollection services,
IConfiguration configuration)
{
// Register path service
services.AddScoped<IPathService, WebPathService>();
// ✅ SECURITY: Get connection string from environment variable first (production),
// then fall back to configuration (development)
var connectionString = Environment.GetEnvironmentVariable("DATABASE_CONNECTION_STRING")
?? configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException(
"Connection string not found. " +
"Set DATABASE_CONNECTION_STRING environment variable or configure DefaultConnection in appsettings.json");
// Check if database is encrypted and retrieve password if needed
var encryptionPassword = GetEncryptionPasswordIfNeeded(connectionString);
// Pre-derive raw AES key from passphrase (once at startup) so each connection open
// uses PRAGMA key = "x'hex'" and skips PBKDF2(256000), saving ~20–50 ms per connection.
if (!string.IsNullOrEmpty(encryptionPassword))
encryptionPassword = PrepareEncryptionKey(encryptionPassword, connectionString);
// Register unlock state before any DbContext registration
var unlockState = new DatabaseUnlockState
{
NeedsUnlock = encryptionPassword == null && IsDatabaseEncrypted(connectionString),
DatabasePath = ExtractDatabasePath(connectionString),
ConnectionString = connectionString
};
services.AddSingleton(unlockState);
Console.WriteLine($"Database unlock state: NeedsUnlock={unlockState.NeedsUnlock}, DatabasePath={unlockState.DatabasePath}");
// Register encryption status as singleton for use during startup
services.AddSingleton(new EncryptionDetectionResult
{
IsEncrypted = !string.IsNullOrEmpty(encryptionPassword)
});
// If unlock needed, we still register services (so DI doesn't fail)
// but they won't be able to access database until password is provided
if (unlockState.NeedsUnlock)
{
Console.WriteLine("Database unlock required - services will be registered but database inaccessible until unlock");
}
// CRITICAL: Create interceptor instance BEFORE any DbContext registration
// This single instance will be used by all DbContexts
SqlCipherConnectionInterceptor? interceptor = null;
if (!string.IsNullOrEmpty(encryptionPassword))
{
interceptor = new SqlCipherConnectionInterceptor(encryptionPassword);
// Clear connection pools to ensure no connections bypass the interceptor
SqliteConnection.ClearAllPools();
// Set WAL mode and synchronous=NORMAL once on a writable connection.
// These are persistent database settings — no need to repeat on every connection open.
// This MUST happen here (not in the interceptor) because EF Core also opens read-only
// connections (e.g. SqliteDatabaseCreator.Exists()), and PRAGMA journal_mode = WAL
// returns SQLITE_READONLY (8) on a read-only connection.
using var setupConn = new SqliteConnection(connectionString);
setupConn.Open();
using var setupCmd = setupConn.CreateCommand();
setupCmd.CommandText = $"PRAGMA key = \"{encryptionPassword}\";";
setupCmd.ExecuteNonQuery();
setupCmd.CommandText = "PRAGMA journal_mode = WAL;";
setupCmd.ExecuteNonQuery();
setupCmd.CommandText = "PRAGMA synchronous = NORMAL;";
setupCmd.ExecuteNonQuery();
setupConn.Close();
SqliteConnection.ClearAllPools();
}
// ✅ Register Application layer (includes Infrastructure internally) with encryption interceptor
services.AddApplication(connectionString, encryptionPassword, interceptor);
// Register Identity database context (SimpleStart-specific) with encryption interceptor
services.AddDbContext<SimpleStartDbContext>((serviceProvider, options) =>
{
options.UseSqlite(connectionString);
if (interceptor != null)
{
options.AddInterceptors(interceptor);
}
});
// CRITICAL: Clear connection pools again after DbContext registration
if (!string.IsNullOrEmpty(encryptionPassword))
{
SqliteConnection.ClearAllPools();
}
// Register DatabaseService now that both contexts are available
services.AddScoped<IDatabaseService>(sp =>
new DatabaseService(
sp.GetRequiredService<Aquiis.Infrastructure.Data.ApplicationDbContext>(),
sp.GetRequiredService<SimpleStartDbContext>(),
sp.GetRequiredService<Microsoft.Extensions.Logging.ILogger<DatabaseService>>()));
services.AddDatabaseDeveloperPageExceptionFilter();
// Configure Identity with Web-specific settings
services.AddIdentity<ApplicationUser, IdentityRole>(options => {
// For web app, require confirmed email
options.SignIn.RequireConfirmedAccount = true;
// ✅ SECURITY: Strong password policy (12+ chars, special characters required)
options.Password.RequireDigit = true;
options.Password.RequiredLength = 12;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = true;
options.Password.RequiredUniqueChars = 4; // Prevent patterns like "aaa111!!!"
})
.AddEntityFrameworkStores<SimpleStartDbContext>()
.AddDefaultTokenProviders();
// Configure cookie authentication for Web
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = "/Account/AccessDenied";
});
return services;
}
/// <summary>
/// Detects if database is encrypted and retrieves password from keychain if needed
/// </summary>
/// <returns>Encryption password, or null if database is not encrypted</returns>
internal static string? GetEncryptionPasswordIfNeeded(string connectionString)
{
try
{
// Extract database path from connection string
var builder = new SqliteConnectionStringBuilder(connectionString);
var dbPath = builder.DataSource;
if (!File.Exists(dbPath))
{
// Database doesn't exist yet, not encrypted
return null;
}
// Try to open as plaintext
try
{
using (var conn = new SqliteConnection(connectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT COUNT(*) FROM sqlite_master;";
cmd.ExecuteScalar();
}
}
// Success - database is not encrypted
return null;
}
catch (SqliteException ex) when (ex.SqliteErrorCode == 26) // "file is not a database"
{
// Database is encrypted - try to get password from keychain
var keychain = OperatingSystem.IsWindows()
? (IKeychainService)new WindowsKeychainService("SimpleStart-Web")
: new LinuxKeychainService("SimpleStart-Web"); // Pass app name to prevent keychain conflicts
var password = keychain.RetrieveKey();
if (string.IsNullOrEmpty(password))
{
Console.WriteLine("Database is encrypted but password not in keychain - will prompt user");
return null; // Signal that unlock is needed
}
// CRITICAL: Clear connection pool to prevent reuse of unencrypted connections
SqliteConnection.ClearAllPools();
return password;
}
}
catch (InvalidOperationException)
{
throw; // Re-throw our custom messages
}
catch (Exception ex)
{
// Log but don't fail - assume database is not encrypted
Console.WriteLine($"Warning: Could not check database encryption status: {ex.Message}");
return null;
}
}
/// <summary>
/// Helper method to check if database is encrypted
/// </summary>
private static bool IsDatabaseEncrypted(string connectionString)
{
var builder = new SqliteConnectionStringBuilder(connectionString);
var dbPath = builder.DataSource;
if (!File.Exists(dbPath)) return false;
try
{
using var conn = new SqliteConnection(connectionString);
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM sqlite_master;";
cmd.ExecuteScalar();
return false; // Opened successfully = not encrypted
}
catch (SqliteException ex) when (ex.SqliteErrorCode == 26)
{
return true; // Error 26 = encrypted
}
catch
{
return false; // Other errors = assume not encrypted
}
}
/// <summary>
/// Helper method to extract database path from connection string
/// </summary>
private static string ExtractDatabasePath(string connectionString)
{
var builder = new SqliteConnectionStringBuilder(connectionString);
return builder.DataSource;
}
/// <summary>
/// Pre-derives the AES-256 key from <paramref name="password"/> using SQLCipher 4's PBKDF2
/// parameters (HMAC-SHA512, 256 000 iterations, 32-byte output). The salt is read from the
/// first 16 bytes of the database file — the same salt SQLCipher embedded when the database
/// was originally encrypted. The returned value is in SQLCipher's raw-key format
/// <c>x'hexbytes'</c>, which the interceptor passes directly as <c>PRAGMA key</c>,
/// skipping all PBKDF2 work on every subsequent connection open.
///
/// Falls back to the original passphrase string if the file cannot be read or is too small
/// (e.g. first-run before the database exists), in which case the interceptor's passphrase
/// path handles key derivation as usual.
/// </summary>
private static string PrepareEncryptionKey(string password, string connectionString)
{
try
{
var dbPath = ExtractDatabasePath(connectionString);
if (!File.Exists(dbPath) || new FileInfo(dbPath).Length < 16)
return password; // DB not yet created — passphrase path is fine
// SQLCipher stores its PBKDF2 salt in the first 16 bytes of the database file
var salt = new byte[16];
using (var fs = new FileStream(dbPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
if (fs.Read(salt, 0, 16) < 16) return password;
}
// Derive using the same parameters SQLCipher 4 uses by default
var keyBytes = System.Security.Cryptography.Rfc2898DeriveBytes.Pbkdf2(
System.Text.Encoding.UTF8.GetBytes(password),
salt,
256000,
System.Security.Cryptography.HashAlgorithmName.SHA512,
32); // 256-bit AES key
return "x'" + Convert.ToHexString(keyBytes) + "'";
}
catch
{
return password; // Any I/O or crypto error — fall back to passphrase
}
}
}
/// <summary>
/// Tracks whether database encryption was detected during startup
/// </summary>
public class EncryptionDetectionResult
{
public bool IsEncrypted { get; set; }
}