private static bool validNewAuthToken(string host, string token) { if (host.Length <= 0) { GUI.user.RaiseError("Host field is required."); return(false); } if (token.Length <= 0) { GUI.user.RaiseError("Token field is required."); return(false); } if (Uri.CheckHostName(host) == UriHostNameType.Unknown) { GUI.user.RaiseError("{0} is not a valid host name.", host); return(false); } string oldToken; if (Win32Registry.TryGetAuthToken(host, out oldToken)) { GUI.user.RaiseError("{0} already has an authentication token.", host); return(false); } return(true); }
public NetAsyncDownloaderDownloadPart(Net.DownloadTarget target, string path = null) { this.url = target.url; this.fallbackUrl = target.fallbackUrl; this.triedFallback = false; this.path = path ?? Path.GetTempFileName(); size = bytesLeft = target.size; lastProgressUpdateTime = DateTime.Now; agent.Headers.Add("User-Agent", Net.UserAgentString); // Tell the server what kind of files we want if (!string.IsNullOrEmpty(target.mimeType)) { log.InfoFormat("Setting MIME type {0}", target.mimeType); agent.Headers.Add("Accept", target.mimeType); } // Check whether to use an auth token for this host string token; if (Win32Registry.TryGetAuthToken(this.url.Host, out token) && !string.IsNullOrEmpty(token)) { log.InfoFormat("Using auth token for {0}", this.url.Host); // Send our auth token to the GitHub API (or whoever else needs one) agent.Headers.Add("Authorization", $"token {token}"); } }
private static bool validNewAuthToken(string host, string token) { if (host.Length <= 0) { GUI.user.RaiseError(Properties.Resources.AddAuthTokenHostRequired); return(false); } if (token.Length <= 0) { GUI.user.RaiseError(Properties.Resources.AddAuthTokenTokenRequired); return(false); } if (Uri.CheckHostName(host) == UriHostNameType.Unknown) { GUI.user.RaiseError(Properties.Resources.AddAuthTokenInvalidHost, host); return(false); } string oldToken; if (Win32Registry.TryGetAuthToken(host, out oldToken)) { GUI.user.RaiseError(Properties.Resources.AddAuthTokenDupHost, host); return(false); } return(true); }
public static string DownloadText(Uri url, string authToken = "") { log.DebugFormat("About to download {0}", url.OriginalString); try { WebClient agent = MakeDefaultHttpClient(); // Check whether to use an auth token for this host if (!string.IsNullOrEmpty(authToken) || (Win32Registry.TryGetAuthToken(url.Host, out authToken) && !string.IsNullOrEmpty(authToken))) { log.InfoFormat("Using auth token for {0}", url.Host); // Send our auth token to the GitHub API (or whoever else needs one) agent.Headers.Add("Authorization", $"token {authToken}"); } return(agent.DownloadString(url.OriginalString)); } catch (Exception) { try { log.InfoFormat("Download failed, trying with curlsharp..."); var content = string.Empty; var client = Curl.CreateEasy(url.OriginalString, delegate(byte[] buf, int size, int nmemb, object extraData) { content += Encoding.UTF8.GetString(buf); return(size * nmemb); }); using (client) { var result = client.Perform(); if (result != CurlCode.Ok) { throw new Exception("Curl download failed with error " + result); } log.DebugFormat("Download from {0}:\r\n\r\n{1}", url, content); return(content); } } catch (Exception e) { throw new Kraken("Downloading using cURL failed", e); } } }
private void RefreshAuthTokensListBox() { AuthTokensListBox.Items.Clear(); foreach (string host in Win32Registry.GetAuthTokenHosts()) { string token; if (Win32Registry.TryGetAuthToken(host, out token)) { AuthTokensListBox.Items.Add(string.Format("{0} | {1}", host, token)); } } }
private void ResetAgent() { agent = new WebClient(); agent.Headers.Add("User-Agent", Net.UserAgentString); // Tell the server what kind of files we want if (!string.IsNullOrEmpty(mimeType)) { log.InfoFormat("Setting MIME type {0}", mimeType); agent.Headers.Add("Accept", mimeType); } // Check whether to use an auth token for this host string token; if (Win32Registry.TryGetAuthToken(this.url.Host, out token) && !string.IsNullOrEmpty(token)) { log.InfoFormat("Using auth token for {0}", this.url.Host); // Send our auth token to the GitHub API (or whoever else needs one) agent.Headers.Add("Authorization", $"token {token}"); } // Forward progress and completion events to our listeners agent.DownloadProgressChanged += (sender, args) => { if (Progress != null) { Progress(sender, args); } }; agent.DownloadFileCompleted += (sender, args) => { if (Done != null) { Done(sender, args); } }; }