-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
573 lines (512 loc) · 19.5 KB
/
Copy pathMain.cs
File metadata and controls
573 lines (512 loc) · 19.5 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
using System.Diagnostics;
using System.Windows.Controls;
using Flow.Launcher.Plugin.ProtonPass.Models;
using Flow.Launcher.Plugin.ProtonPass.Services;
using Flow.Launcher.Plugin.ProtonPass.Settings;
namespace Flow.Launcher.Plugin.ProtonPass;
public class Main : IAsyncPlugin, IContextMenu, ISettingProvider, IResultUpdated
{
private PluginInitContext _context = null!;
private ProtonPassSettings _settings = null!;
private SettingsViewModel _settingsViewModel = null!;
private ProtonPassService _protonPassService = null!;
private Dictionary<string, List<ItemEntry>> _vaultCache = [];
private Dictionary<string, string> _vaultNameById = [];
private List<VaultInfo> _vaults = [];
private string? _defaultVaultId;
private DateTime _lastLoginPrompt = DateTime.MinValue;
private CancellationTokenSource? _totpCts;
private Timer? _totpTimer;
private Query? _lastQuery;
private List<Result>? _lastResults;
private TotpGenerator? _totpGenerator;
public event ResultUpdatedEventHandler? ResultsUpdated;
public async Task InitAsync(PluginInitContext context)
{
_context = context;
_settings = context.API.LoadSettingJsonStorage<ProtonPassSettings>();
_settingsViewModel = new SettingsViewModel(_settings);
_protonPassService = new ProtonPassService(context);
_context.API.LogInfo(nameof(Main), $"DefaultVault setting: '{_settings.DefaultVault}'");
try
{
_vaults = await _protonPassService.GetVaultListAsync();
_context.API.LogInfo(nameof(Main), $"Fetched {_vaults.Count} vaults");
_vaultNameById = _vaults.ToDictionary(v => v.VaultId, v => v.Name);
if (_vaults.Count == 1)
{
_settings.DefaultVault = _vaults[0].Name;
_context.API.LogInfo(nameof(Main), $"Single vault, default overridden to '{_settings.DefaultVault}'");
}
var defaultVault = _vaults.FirstOrDefault(v =>
v.Name.Trim().Equals(_settings.DefaultVault.Trim(), StringComparison.OrdinalIgnoreCase));
if (defaultVault is null)
{
_context.API.LogInfo(nameof(Main),
$"DefaultVault '{_settings.DefaultVault}' did not match any vault (available: {string.Join(", ", _vaults.Select(v => v.Name))}), falling back to first vault");
defaultVault = _vaults.FirstOrDefault();
}
_defaultVaultId = defaultVault?.VaultId;
_context.API.LogInfo(nameof(Main), $"DefaultVaultId resolved to '{_defaultVaultId}' for vault '{defaultVault?.Name}'");
foreach (var vault in _vaults)
{
try
{
var items = await _protonPassService.GetItemListAsync(vault.Name);
_vaultCache[vault.VaultId] = items;
_context.API.LogInfo(nameof(Main), $"Cached {items.Count} items for vault '{vault.Name}'");
}
catch (Exception ex)
{
_context.API.LogInfo(nameof(Main), $"Failed to cache vault '{vault.Name}': {ex.Message}");
}
}
}
catch (Exception ex)
{
_context.API.LogInfo(nameof(Main), $"Init failed: {ex.Message}");
}
}
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
_totpCts?.Cancel();
_totpCts = null;
_totpGenerator = null;
_lastResults = null;
var searchTerm = query.Search;
var defaultVault = _vaults.FirstOrDefault(v =>
v.Name.Trim().Equals(_settings.DefaultVault.Trim(), StringComparison.OrdinalIgnoreCase))
?? _vaults.FirstOrDefault();
string? selectedVaultId = defaultVault?.VaultId;
var displayVaultName = defaultVault?.Name ?? _settings.DefaultVault;
var vaultPrefix = "";
// --- Parse @ prefix ---
if (searchTerm.StartsWith('@'))
{
string? vaultName = null;
var rest = "";
if (searchTerm.Length == 1 || searchTerm[1] == ' ')
{
// Just '@' or '@ ' — vault selection mode
if (_vaults.Count == 1)
{
var only = _vaults[0];
return
[
new Result
{
Title = $"Search in '{only.Name}' vault",
SubTitle = "Press Enter to select this vault",
IcoPath = "Images\\vault.png",
Score = Result.MaxScore,
Action = _ =>
{
_context.API.ChangeQuery(VaultSearchQuery(only.Name));
return false;
}
}
];
}
var vaultResults = new List<Result>();
foreach (var v in _vaults)
{
vaultResults.Add(new Result
{
Title = v.Name,
SubTitle = "Select vault",
IcoPath = "Images\\vault.png",
Action = _ =>
{
_context.API.ChangeQuery(VaultSearchQuery(v.Name));
return false;
}
});
}
vaultResults.Add(new Result
{
Title = "*",
SubTitle = "Search all vaults",
IcoPath = "Images\\vault.png",
Action = _ =>
{
_context.API.ChangeQuery("pp @* ");
return false;
}
});
return vaultResults;
}
if (searchTerm[1] == '"')
{
var closeQuote = searchTerm.IndexOf('"', 2);
if (closeQuote > 2)
{
vaultName = searchTerm[2..closeQuote];
rest = searchTerm[(closeQuote + 1)..].TrimStart();
}
}
else if (searchTerm[1] == '*')
{
vaultName = "*";
rest = searchTerm[2..].TrimStart();
}
else
{
var spaceIndex = searchTerm.IndexOf(' ', 1);
if (spaceIndex > 1)
{
vaultName = searchTerm[1..spaceIndex];
rest = searchTerm[(spaceIndex + 1)..];
}
else
{
vaultName = searchTerm[1..];
rest = "";
}
}
if (vaultName == "*")
{
selectedVaultId = null;
displayVaultName = "All vaults";
vaultPrefix = "@* ";
}
else if (vaultName != null)
{
var match = _vaults.FirstOrDefault(v =>
v.Name.Trim().Equals(vaultName.Trim(), StringComparison.OrdinalIgnoreCase));
if (match != null)
{
selectedVaultId = match.VaultId;
displayVaultName = match.Name;
vaultPrefix = vaultName.Contains(' ') ? $"@\"{vaultName}\" " : $"@{vaultName} ";
}
else
{
return [];
}
}
searchTerm = rest;
_context.API.LogInfo(nameof(Main), $"Vault override: '{displayVaultName}', searchTerm now '{searchTerm}'");
}
// --- Gather items from selected vault(s) ---
IEnumerable<ItemEntry> source;
if (selectedVaultId == null)
source = _vaultCache.SelectMany(kvp => kvp.Value);
else if (_vaultCache.TryGetValue(selectedVaultId, out var items))
source = items;
else
source = [];
var logins = source
.Where(i => i.ItemType == "login" && i.State == "Active")
.ToList();
var totalCached = _vaultCache.Values.Sum(l => l.Count);
// --- Empty original search — show refresh + list ---
if (string.IsNullOrWhiteSpace(query.Search))
{
var results = new List<Result>
{
new Result
{
Title = "Refresh Proton Pass Cache",
SubTitle = $"Currently {totalCached} items cached across {_vaults.Count} vault(s)",
IcoPath = "Images\\refresh.png",
Action = _ =>
{
if (!_protonPassService.IsAuthenticated())
{
ShowNotAuthenticated();
return true;
}
_context.API.ShowMsg("Refreshing...", "Proton Pass cache refresh triggered");
Task.Run(async () => await RefreshCacheAsync());
return true;
},
Score = Result.MaxScore
}
};
results.AddRange(logins.Take(_settings.MaxResults).Select(item => new Result
{
Title = item.Title,
SubTitle = $"Vault: {(_vaultNameById.TryGetValue(item.VaultId, out var n) ? n : displayVaultName)}",
IcoPath = "Images\\account.png",
AutoCompleteText = $"{vaultPrefix}{item.Title}",
Action = _ =>
{
_context.API.ChangeQuery($"pp {vaultPrefix}\"{item.Title}\"");
return false;
}
}));
return results;
}
// --- Parse exact match ---
bool exactMatch = searchTerm.StartsWith('"');
if (exactMatch)
{
searchTerm = searchTerm.Trim('"');
_context.API.LogInfo(nameof(Main), $"Exact match mode, searchTerm now '{searchTerm}'");
}
// --- Match against logins ---
List<ItemEntry> matches;
if (exactMatch)
matches = logins.Where(i => i.Title.Equals(searchTerm, StringComparison.OrdinalIgnoreCase)).ToList();
else if (string.IsNullOrWhiteSpace(searchTerm))
matches = logins;
else
matches = logins.Where(i => i.Title.Contains(searchTerm, StringComparison.OrdinalIgnoreCase)).ToList();
//_context.API.LogInfo(nameof(Main), $"Matches after filter: {matches.Count}");
if (matches.Count == 0)
return [];
if (matches.Count == 1)
{
_context.API.LogInfo(nameof(Main), $"Single match, fetching detail for '{matches[0].Title}'");
_totpCts = CancellationTokenSource.CreateLinkedTokenSource(token);
_lastQuery = query;
var results = await BuildDetailResults(matches[0]);
_lastResults = results;
if (_totpGenerator != null)
StartTotpTimer();
return results;
}
return matches.Take(_settings.MaxResults).Select(item => new Result
{
Title = item.Title,
SubTitle = $"Vault: {(_vaultNameById.TryGetValue(item.VaultId, out var n) ? n : displayVaultName)}",
IcoPath = "Images\\account.png",
AutoCompleteText = $"{vaultPrefix}{item.Title}",
Action = _ =>
{
_context.API.ChangeQuery($"pp {vaultPrefix}\"{item.Title}\"");
return false;
}
}).ToList();
}
private static string VaultSearchQuery(string vaultName)
{
var quoted = vaultName.Contains(' ') ? $"\"{vaultName}\"" : vaultName;
return $"pp @{quoted} ";
}
private async Task<List<Result>> BuildDetailResults(ItemEntry item)
{
var results = new List<Result>();
ItemViewResponse? detail;
try
{
detail = await _protonPassService.GetItemDetailAsync(item.Id, item.ShareId);
}
catch (PassCliNotAuthenticatedException ex)
{
_context.API.LogInfo(nameof(Main), $"Detail fetch failed - not authenticated: {ex.Message}");
ShowNotAuthenticated();
return results;
}
catch (Exception ex)
{
_context.API.LogInfo(nameof(Main), $"Detail fetch failed: {ex.Message}");
return results;
}
if (detail?.Item?.Content?.Content?.Login is not { } login)
{
_context.API.LogInfo(nameof(Main), "Detail response did not contain expected Login content");
return results;
}
if (!string.IsNullOrEmpty(login.Email))
{
results.Add(MakeCopyResult(login.Email, "Email", "Images\\email.png"));
}
if (!string.IsNullOrEmpty(login.Username))
{
results.Add(MakeCopyResult(login.Username, "Username", "Images\\username.png"));
}
if (!string.IsNullOrEmpty(login.Password))
{
var password = login.Password;
results.Add(new Result
{
Title = "****",
SubTitle = "Password",
IcoPath = "Images\\password.png",
Action = _ =>
{
CopyToClipboard(password, "Password");
return !_settings.KeepOpenAfterAction;
}
});
}
if (!string.IsNullOrEmpty(login.TotpUri))
{
_totpGenerator = new TotpGenerator(login.TotpUri);
var token = _totpGenerator.GenerateTotp();
var remaining = _totpGenerator.GetRemainingSeconds();
results.Add(new Result
{
Title = FormatTotpToken(token),
SubTitle = $"TOTP · expires in {remaining}s",
IcoPath = "Images\\password.png",
RecordKey = "totp_" + item.Id,
Action = _ =>
{
CopyToClipboard(token, "TOTP");
return !_settings.KeepOpenAfterAction;
}
});
}
if (login.Urls is not null)
{
foreach (var url in login.Urls)
{
if (!string.IsNullOrEmpty(url))
results.Add(MakeUrlResult(url, "Images\\url.png"));
}
}
if (detail.Item.Content.ExtraFields is not null)
{
foreach (var field in detail.Item.Content.ExtraFields)
{
var value = field.Content?.Text;
if (!string.IsNullOrEmpty(value))
results.Add(MakeCopyResult(value, field.FieldName, "Images\\protonpass.png"));
}
}
return results;
}
private void StartTotpTimer()
{
_totpTimer?.Dispose();
_totpTimer = new Timer(TotpTimerCallback, null, 0, 1000);
}
private void TotpTimerCallback(object? state)
{
if (_totpCts?.IsCancellationRequested == true || _totpGenerator == null || _lastResults == null)
return;
try
{
var token = _totpGenerator.GenerateTotp();
var remaining = _totpGenerator.GetRemainingSeconds();
var totpResult = _lastResults.FirstOrDefault(r => r.RecordKey?.StartsWith("totp_") == true);
if (totpResult == null)
return;
totpResult.Title = FormatTotpToken(token);
totpResult.SubTitle = $"TOTP · expires in {remaining}s";
void Fire() => ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs
{
Results = _lastResults,
Query = _lastQuery!,
Token = _totpCts?.Token ?? default
});
var dispatcher = System.Windows.Application.Current?.Dispatcher;
if (dispatcher?.CheckAccess() == true)
Fire();
else
dispatcher?.Invoke(Fire);
}
catch (Exception ex)
{
_context.API.LogInfo(nameof(Main), $"TOTP timer error: {ex.Message}");
}
}
private static string FormatTotpToken(string token)
{
return token.Length switch
{
6 => $"{token[..3]} {token[3..]}",
8 => $"{token[..3]} {token[3..6]} {token[6..]}",
_ => token
};
}
private void CopyToClipboard(string text, string label)
{
try
{
System.Windows.Clipboard.SetText(text);
}
catch (Exception ex)
{
_context.API.LogInfo(nameof(Main), $"Clipboard copy failed for '{label}': {ex.Message}");
_context.API.ShowMsg("Clipboard Error", $"Could not copy {label}. The clipboard may be in use by another application.");
}
}
private Result MakeCopyResult(string value, string label, string iconPath)
{
return new Result
{
Title = value,
SubTitle = label,
IcoPath = iconPath,
Action = _ =>
{
CopyToClipboard(value, label);
return !_settings.KeepOpenAfterAction;
}
};
}
private Result MakeUrlResult(string url, string iconPath)
{
return new Result
{
Title = url,
SubTitle = "URL",
IcoPath = iconPath,
Action = _ =>
{
_context.API.OpenUrl(url);
return !_settings.KeepOpenAfterAction;
}
};
}
private void ShowNotAuthenticated()
{
if ((DateTime.UtcNow - _lastLoginPrompt).TotalSeconds < 3)
{
_context.API.LogInfo(nameof(Main), "ShowNotAuthenticated suppressed (debounce)");
return;
}
_lastLoginPrompt = DateTime.UtcNow;
try
{
void Launch()
{
Process.Start(new ProcessStartInfo
{
FileName = "cmd",
Arguments = "/k pass-cli login",
UseShellExecute = true
});
}
var dispatcher = System.Windows.Application.Current?.Dispatcher;
if (dispatcher?.CheckAccess() == true)
Launch();
else
dispatcher?.Invoke(Launch);
}
catch (Exception ex)
{
_context.API.LogInfo(nameof(Main), $"Failed to launch cmd: {ex.Message}");
}
}
private async Task RefreshCacheAsync()
{
try
{
var total = 0;
foreach (var vault in _vaults)
{
var items = await _protonPassService.GetItemListAsync(vault.Name);
_vaultCache[vault.VaultId] = items;
total += items.Count;
}
_context.API.LogInfo(nameof(Main), $"Cache refreshed: {total} items across {_vaults.Count} vault(s)");
_context.API.ShowMsg("Proton Pass", $"Cache refreshed: {total} items across {_vaults.Count} vault(s)");
}
catch (Exception ex)
{
_context.API.LogInfo(nameof(Main), $"Cache refresh failed: {ex.Message}");
}
}
public List<Result> LoadContextMenus(Result selectedResult)
{
return [];
}
public Control CreateSettingPanel()
{
return new ProtonPassSettingsPanel(_settingsViewModel);
}
}