This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathApiClientConfiguration.cs
More file actions
131 lines (117 loc) · 4.22 KB
/
ApiClientConfiguration.cs
File metadata and controls
131 lines (117 loc) · 4.22 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
namespace GitHub.Api
{
/// <summary>
/// Holds the configuration for API clients.
/// </summary>
public static partial class ApiClientConfiguration
{
/// <summary>
/// Initializes static members of the <see cref="ApiClientConfiguration"/> class.
/// </summary>
static ApiClientConfiguration()
{
Configure();
}
/// <summary>
/// Gets the application's OAUTH client ID.
/// </summary>
public static string ClientId { get; private set; }
/// <summary>
/// Gets the application's OAUTH client secret.
/// </summary>
public static string ClientSecret { get; private set; }
/// <summary>
/// Gets the minimum scopes required by the application.
/// </summary>
public static IReadOnlyList<string> MinimumScopes { get; } = new[] { "user", "repo", "gist", "write:public_key" };
/// <summary>
/// Gets the ideal scopes requested by the application.
/// </summary>
public static IReadOnlyList<string> RequestedScopes { get; } = new[] { "user", "repo", "gist", "write:public_key", "read:org" };
/// <summary>
/// Gets a note that will be stored with the OAUTH token.
/// </summary>
public static string AuthorizationNote
{
get { return Info.ApplicationInfo.ApplicationDescription + " on " + GetMachineNameSafe(); }
}
/// <summary>
/// Gets the machine fingerprint that will be registered with the OAUTH token, allowing
/// multiple authorizations to be created for a single user.
/// </summary>
public static string MachineFingerprint
{
get
{
return GetSha256Hash(
Info.ApplicationInfo.ApplicationDescription + ":" +
GetMachineIdentifier() + ":" +
GetMachineNameSafe());
}
}
static partial void Configure();
static string GetMachineIdentifier()
{
try
{
// adapted from http://stackoverflow.com/a/1561067
var fastestValidNetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
.OrderByDescending(nic => nic.Speed)
.Where(nic => nic.OperationalStatus == OperationalStatus.Up)
.Select(nic => nic.GetPhysicalAddress().ToString())
.FirstOrDefault(address => address.Length >= 12);
return fastestValidNetworkInterface ?? GetMachineNameSafe();
}
catch (Exception)
{
//log.Info("Could not retrieve MAC address. Fallback to using machine name.", e);
return GetMachineNameSafe();
}
}
static string GetMachineNameSafe()
{
try
{
return Dns.GetHostName();
}
catch (Exception)
{
//log.Info("Failed to retrieve host name using `DNS.GetHostName`.", e);
try
{
return Environment.MachineName;
}
catch (Exception)
{
//log.Info("Failed to retrieve host name using `Environment.MachineName`.", ex);
return "(unknown)";
}
}
}
static string GetSha256Hash(string input)
{
try
{
using (var sha256 = SHA256.Create())
{
var bytes = Encoding.UTF8.GetBytes(input);
var hash = sha256.ComputeHash(bytes);
return string.Join("", hash.Select(b => b.ToString("x2", CultureInfo.InvariantCulture)));
}
}
catch (Exception)
{
//log.Error("IMPOSSIBLE! Generating Sha256 hash caused an exception.", e);
return null;
}
}
}
}