public string GenerateFromUuid(string uuid) { PwEntry entry = null; PwUuid id = new PwUuid(MemUtil.HexStringToByteArray(uuid)); var configOpt = new ConfigOpt(_host.CustomConfig); if (configOpt.SearchInAllOpenedDatabases) { foreach (PwDocument doc in _host.MainWindow.DocumentManager.Documents) { if (doc.Database.IsOpen) { entry = doc.Database.RootGroup.FindEntry(id, true); if (entry != null) { break; } } } } else { entry = _host.Database.RootGroup.FindEntry(id, true); } if (entry == null) { return(string.Empty); } string TotpSettings = _ext.GetTotpSettings(entry); if (TotpSettings == null) { return(string.Empty); } try { return(Totp.Generate(TotpSettings)); } catch (Exception) { return(string.Empty); } }
internal Response GetLoginsHandler(Request req) { if (!req.TryDecrypt()) { return(new ErrorResponse(req, ErrorType.CannotDecryptMessage)); } var msg = req.Message; var id = msg.GetString("id"); var url = msg.GetString("url"); var submitUrl = msg.GetString("submitUrl"); Uri hostUri; Uri submitUri; if (!string.IsNullOrEmpty(url)) { hostUri = new Uri(url); } else { return(new ErrorResponse(req, ErrorType.NoUrlProvided)); } if (!string.IsNullOrEmpty(submitUrl)) { submitUri = new Uri(submitUrl); } else { submitUri = hostUri; } var resp = req.GetResponse(); resp.Message.Add("id", id); var items = FindMatchingEntries(url, null); if (items.ToList().Count > 0) { bool filter(PwEntry e) { var c = _ext.GetEntryConfig(e); var title = e.Strings.ReadSafe(PwDefs.TitleField); var entryUrl = e.Strings.ReadSafe(PwDefs.UrlField); if (c != null) { return((title != hostUri.Host && entryUrl != hostUri.Host && !c.Allow.Contains(hostUri.Host)) || (submitUri.Host != null && !c.Allow.Contains(submitUri.Host) && submitUri.Host != title && submitUri.Host != entryUrl)); } return((title != hostUri.Host && entryUrl != hostUri.Host) || (submitUri.Host != null && title != submitUri.Host && entryUrl != submitUri.Host)); } var configOpt = new ConfigOpt(_host.CustomConfig); var config = _ext.GetConfigEntry(true); var autoAllowS = config.Strings.ReadSafe("Auto Allow"); var autoAllow = !string.IsNullOrWhiteSpace(autoAllowS); autoAllow = autoAllow || configOpt.AlwaysAllowAccess; var needPrompting = from e in items where filter(e.entry) select e; if (needPrompting.ToList().Count > 0 && !autoAllow) { var win = _host.MainWindow; using (var f = new AccessControlForm()) { win.Invoke((MethodInvoker) delegate { f.Icon = win.Icon; f.Plugin = _ext; f.Entries = (from e in items where filter(e.entry) select e.entry).ToList(); //f.Entries = needPrompting.ToList(); f.Host = submitUri.Host ?? hostUri.Host; f.Load += delegate { f.Activate(); }; f.ShowDialog(win); if (f.Remember && (f.Allowed || f.Denied)) { foreach (var e in needPrompting) { var c = _ext.GetEntryConfig(e.entry) ?? new EntryConfig(); var set = f.Allowed ? c.Allow : c.Deny; set.Add(hostUri.Host); if (submitUri.Host != null && submitUri.Host != hostUri.Host) { set.Add(submitUri.Host); } _ext.SetEntryConfig(e.entry, c); } } if (!f.Allowed) { items = items.Except(needPrompting); } }); } } foreach (var entryDatabase in items) { string entryUrl = String.Copy(entryDatabase.entry.Strings.ReadSafe(PwDefs.UrlField)); if (String.IsNullOrEmpty(entryUrl)) { entryUrl = entryDatabase.entry.Strings.ReadSafe(PwDefs.TitleField); } entryUrl = entryUrl.ToLower(); entryDatabase.entry.UsageCount = (ulong)LevenshteinDistance(submitUri.ToString().ToLower(), entryUrl); } var itemsList = items.ToList(); if (configOpt.SpecificMatchingOnly) { itemsList = (from e in itemsList orderby e.entry.UsageCount ascending select e).ToList(); ulong lowestDistance = itemsList.Count > 0 ? itemsList[0].entry.UsageCount : 0; itemsList = (from e in itemsList where e.entry.UsageCount == lowestDistance orderby e.entry.UsageCount select e).ToList(); } if (configOpt.SortResultByUsername) { var items2 = from e in itemsList orderby e.entry.UsageCount ascending, _ext.GetUserPass(e)[0] ascending select e; itemsList = items2.ToList(); } else { var items2 = from e in itemsList orderby e.entry.UsageCount ascending, e.entry.Strings.ReadSafe(PwDefs.TitleField) ascending select e; itemsList = items2.ToList(); } var entries = new JArray(itemsList.Select(item => { var up = _ext.GetUserPass(item); var TotpSettings = _ext.GetTotpSettings(item.entry); JArray fldArr = null; var fields = GetFields(configOpt, item); if (fields != null) { fldArr = new JArray(fields.Select(f => new JObject { { f.Key, f.Value } })); } string fldTotp = null; if (TotpSettings != null) { fldTotp = Totp.Generate(TotpSettings); } return(new JObject { { "name", item.entry.Strings.ReadSafe(PwDefs.TitleField) }, { "login", up[0] }, { "password", up[1] }, { "uuid", item.entry.Uuid.ToHexString() }, { "totp", fldTotp }, { "stringFields", fldArr } }); })); resp.Message.Add("count", itemsList.Count); resp.Message.Add("entries", entries); if (itemsList.Count > 0) { var names = (from e in itemsList select e.entry.Strings.ReadSafe(PwDefs.TitleField)).Distinct(); var n = String.Join("\n ", names); if (configOpt.ReceiveCredentialNotification) { _ext.ShowNotification(String.Format("{0}: {1} is receiving credentials for:\n {2}", req.GetString("id"), hostUri.Host, n)); } } return(resp); } resp.Message.Add("count", 0); resp.Message.Add("entries", new JArray()); return(resp); }