|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Net.NetworkInformation; |
| 6 | +using System.Reflection; |
| 7 | +using System.Security.Cryptography; |
| 8 | +using System.Text; |
| 9 | +using GitHub.Primitives; |
| 10 | + |
| 11 | +namespace GitHub.Api |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Holds the configuration for API clients. |
| 15 | + /// </summary> |
| 16 | + public static partial class ApiClientConfiguration |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Initializes static members of the <see cref="ApiClientConfiguration"/> class. |
| 20 | + /// </summary> |
| 21 | + static ApiClientConfiguration() |
| 22 | + { |
| 23 | + Configure(); |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Gets the application's OAUTH client ID. |
| 28 | + /// </summary> |
| 29 | + public static string ClientId { get; private set; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets the application's OAUTH client secret. |
| 33 | + /// </summary> |
| 34 | + public static string ClientSecret { get; private set; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Gets a note that will be stored with the OAUTH token. |
| 38 | + /// </summary> |
| 39 | + public static string AuthorizationNote |
| 40 | + { |
| 41 | + get { return Info.ApplicationInfo.ApplicationDescription + " on " + GetMachineNameSafe(); } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the machine fingerprint that will be registered with the OAUTH token, allowing |
| 46 | + /// multiple authorizations to be created for a single user. |
| 47 | + /// </summary> |
| 48 | + public static string MachineFingerprint |
| 49 | + { |
| 50 | + get |
| 51 | + { |
| 52 | + return GetSha256Hash(Info.ApplicationInfo.ApplicationDescription + ":" + GetMachineIdentifier()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + static partial void Configure(); |
| 57 | + |
| 58 | + static string GetMachineIdentifier() |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + // adapted from http://stackoverflow.com/a/1561067 |
| 63 | + var fastedValidNetworkInterface = NetworkInterface.GetAllNetworkInterfaces() |
| 64 | + .OrderBy(nic => nic.Speed) |
| 65 | + .Where(nic => nic.OperationalStatus == OperationalStatus.Up) |
| 66 | + .Select(nic => nic.GetPhysicalAddress().ToString()) |
| 67 | + .FirstOrDefault(address => address.Length > 12); |
| 68 | + |
| 69 | + return fastedValidNetworkInterface ?? GetMachineNameSafe(); |
| 70 | + } |
| 71 | + catch (Exception) |
| 72 | + { |
| 73 | + //log.Info("Could not retrieve MAC address. Fallback to using machine name.", e); |
| 74 | + return GetMachineNameSafe(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + static string GetMachineNameSafe() |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + return Dns.GetHostName(); |
| 83 | + } |
| 84 | + catch (Exception) |
| 85 | + { |
| 86 | + //log.Info("Failed to retrieve host name using `DNS.GetHostName`.", e); |
| 87 | + |
| 88 | + try |
| 89 | + { |
| 90 | + return Environment.MachineName; |
| 91 | + } |
| 92 | + catch (Exception) |
| 93 | + { |
| 94 | + //log.Info("Failed to retrieve host name using `Environment.MachineName`.", ex); |
| 95 | + return "(unknown)"; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + static string GetSha256Hash(string input) |
| 101 | + { |
| 102 | + try |
| 103 | + { |
| 104 | + using (var sha256 = SHA256.Create()) |
| 105 | + { |
| 106 | + var bytes = Encoding.UTF8.GetBytes(input); |
| 107 | + var hash = sha256.ComputeHash(bytes); |
| 108 | + |
| 109 | + return string.Join("", hash.Select(b => b.ToString("x2", CultureInfo.InvariantCulture))); |
| 110 | + } |
| 111 | + } |
| 112 | + catch (Exception) |
| 113 | + { |
| 114 | + //log.Error("IMPOSSIBLE! Generating Sha256 hash caused an exception.", e); |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments