private static List <ApiKey> getKeyRing() { List <ApiKey> keyRing = new List <ApiKey>(); log.Debug("Getting keyring."); string html = ""; try { html = new Extensions.OgcsWebClient().DownloadString(keyringURL); } catch (System.Exception ex) { log.Error("Failed to retrieve keyring data: " + ex.Message); } if (!string.IsNullOrEmpty(html)) { html = html.Replace("\n", ""); MatchCollection keyRecords = findText(html, @"<table><thead>.*?<tbody>(<tr>.*?</tr>)</tbody></table>"); if (keyRecords.Count == 0) { log.Warn("Could not find table of keys."); return(keyRing); } foreach (String record in keyRecords[0].Captures[0].Value.Split(new string[] { "<tr>" }, StringSplitOptions.None).Skip(2)) { MatchCollection keyAttributes = findText(record, @"<td.*?>(.*?)</td>"); if (keyAttributes.Count > 0) { try { keyRing.Add(new ApiKey(keyAttributes)); } catch { } } } } log.Debug("There are " + keyRing.Count + " keys."); return(keyRing); }
public static void Initialise() { if (Program.StartedWithSquirrelArgs && !(Environment.GetCommandLineArgs()[1].ToLower().Equals("--squirrel-firstrun"))) { return; } if (Program.InDeveloperMode) { return; } //Note, logging isn't actually initialised yet, so log4net won't log any lines within this function String cloudCredsURL = "https://raw.githubusercontent.com/phw198/OutlookGoogleCalendarSync/master/docs/keyring.md"; String html = null; String line = null; String placeHolder = "###"; String cloudID = null; String cloudKey = null; log.Debug("Getting credential attributes"); try { try { html = new Extensions.OgcsWebClient().DownloadString(cloudCredsURL); html = html.Replace("\n", ""); } catch (System.Exception ex) { log.Error("Failed to retrieve data: " + ex.Message); } if (string.IsNullOrEmpty(html)) { throw new ApplicationException("Not able to retrieve error reporting credentials."); } Regex rgx = new Regex(@"### Error Reporting.*\|ID\|(.*)\|\|Key\|(.*?)\|", RegexOptions.IgnoreCase); MatchCollection keyRecords = rgx.Matches(html); if (keyRecords.Count == 1) { cloudID = keyRecords[0].Groups[1].ToString(); cloudKey = keyRecords[0].Groups[2].ToString(); } else { throw new ApplicationException("Unexpected parse of error reporting credentials."); } List <String> newLines = new List <string>(); StreamReader sr = new StreamReader(templateCredFile); while ((line = sr.ReadLine()) != null) { if (line.IndexOf(placeHolder) > 0) { if (line.IndexOf("private_key_id") > 0) { line = line.Replace(placeHolder, cloudID); } else if (line.IndexOf("private_key") > 0) { line = line.Replace(placeHolder, cloudKey); } } newLines.Add(line); } try { File.WriteAllLines(credFile, newLines.ToArray()); } catch (System.IO.IOException ex) { if (OGCSexception.GetErrorCode(ex) == "0x80070020") { log.Warn("ErrorReporting.json is being used by another process (perhaps multiple instances of OGCS are being started on system startup?)"); } else { throw; } } Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credFile); } catch (ApplicationException ex) { log.Warn(ex.Message); Initialised = false; //} catch (System.Exception ex) { //Logging isn't initialised yet, so don't catch this error - let it crash out so user is aware and hopefully reports it! //System.Windows.Forms.OgcsMessageBox.Show(ex.Message); //log.Debug("Failed to initialise error reporting."); //OGCSexception.Analyse(ex); } }
private void getGaccountEmail(String accessToken) { String jsonString = ""; log.Debug("Retrieving email address associated with Google account."); try { jsonString = new Extensions.OgcsWebClient().DownloadString("https://www.googleapis.com/oauth2/v2/userinfo?fields=email&access_token=" + accessToken); JObject jo = Newtonsoft.Json.Linq.JObject.Parse(jsonString); JToken jtEmail = jo["email"]; String email = jtEmail.ToString(); if (Settings.Instance.GaccountEmail != email) { if (!String.IsNullOrEmpty(Settings.Instance.GaccountEmail)) { log.Debug("Looks like the Google account username value has been tampering with? :-O"); } Settings.Instance.GaccountEmail = email; Forms.Main.Instance.SetControlPropertyThreadSafe(Forms.Main.Instance.tbConnectedAcc, "Text", email); log.Debug("Updating Google account username: "******"Inner exception: " + ex.InnerException.Message); } if (ex.Response != null) { log.Debug("Reading response."); System.IO.Stream stream = ex.Response.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(stream); log.Error(sr.ReadToEnd()); } if (OGCSexception.GetErrorCode(ex) == "0x80131509") { log.Warn(ex.Message); System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(@"\b(403|Forbidden|Prohibited|Insufficient Permission)\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase); if (rgx.IsMatch(ex.Message)) { if (Settings.Instance.UsingPersonalAPIkeys()) { String msg = "If you are using your own API keys, you must also enable the Google+ API."; OgcsMessageBox.Show(msg, "Missing API Service", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); throw new System.ApplicationException(msg); } else { if (getEmailAttempts > 1) { log.Error("Failed to retrieve Google account username."); log.Debug("Using previously retrieved username: "******"", (ex.InnerException != null ? ex.InnerException : ex)); throw; } } OGCSexception.Analyse(ex); if (ex.Message.ToLower().Contains("access denied")) { Forms.Main.Instance.Console.Update("Failed to obtain Calendar access from Google - it's possible your access has been revoked." + "<br/>Try disconnecting your Google account and reauthorising OGCS.", Console.Markup.error); } else if (ex.Message.ToLower().Contains("prohibited") && Settings.Instance.UsingPersonalAPIkeys()) { Forms.Main.Instance.Console.Update("If you are using your own API keys, you must also enable the Google+ API.", Console.Markup.warning); } throw; } catch (System.Exception ex) { log.Debug("JSON: " + jsonString); log.Error("Failed to retrieve Google account username."); OGCSexception.Analyse(ex); log.Debug("Using previously retrieved username: " + Settings.Instance.GaccountEmail_masked()); } }