Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 06b4c7d

Browse files
authored
Merge pull request #845 from github/fixes/809-refactor-logins
Refactor Logins, Part Uno
2 parents c7f35bc + 7252f9e commit 06b4c7d

File tree

72 files changed

+1736
-626
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1736
-626
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<ProjectConfiguration>
22
<Settings>
3+
<PreventSigningOfAssembly>True</PreventSigningOfAssembly>
34
<PreviouslyBuiltSuccessfully>True</PreviouslyBuiltSuccessfully>
45
</Settings>
56
</ProjectConfiguration>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<ProjectConfiguration>
22
<Settings>
3+
<PreventSigningOfAssembly>True</PreventSigningOfAssembly>
34
<PreviouslyBuiltSuccessfully>True</PreviouslyBuiltSuccessfully>
45
</Settings>
56
</ProjectConfiguration>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<ProjectConfiguration>
22
<Settings>
3+
<PreventSigningOfAssembly>True</PreventSigningOfAssembly>
34
<PreviouslyBuiltSuccessfully>True</PreviouslyBuiltSuccessfully>
45
</Settings>
56
</ProjectConfiguration>

.ncrunch/GitHub.App.v3.ncrunchproject

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<ProjectConfiguration>
22
<Settings>
3+
<PreloadReferencedAssemblies>False</PreloadReferencedAssemblies>
34
<PreventSigningOfAssembly>True</PreventSigningOfAssembly>
45
<PreviouslyBuiltSuccessfully>True</PreviouslyBuiltSuccessfully>
56
</Settings>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Open the `GitHubVS.sln` solution with Visual Studio 2015.
4141
To be able to use the GitHub API, you'll need to:
4242

4343
- [Register a new developer application](https://github.com/settings/developers) in your profile.
44-
- Open [src/GitHub.App/Api/ApiClientConfiguration.cs](src/GitHub.App/Api/ApiClientConfiguration.cs) and fill out the clientId/clientSecret fields for your application.
44+
- Open [src/GitHub.Api/ApiClientConfiguration_User.cs](src/GitHub.Api/ApiClientConfiguration_User.cs) and fill out the clientId/clientSecret fields for your application. **Note this has recently changed location, so you may need to re-do this**
4545

4646
Build using Visual Studio 2015 or:
4747

script

src/CredentialManagement/CredentialManagement.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<NuGetPackageImportStamp>
1515
</NuGetPackageImportStamp>
1616
<TargetFrameworkProfile />
17-
<BuildType Condition="Exists('..\..\script\src\ApiClientConfiguration.cs')">Internal</BuildType>
17+
<BuildType Condition="Exists('..\..\script\src\ApiClientConfiguration_User.cs')">Internal</BuildType>
1818
<OutputPath>bin\$(Configuration)\</OutputPath>
1919
</PropertyGroup>
2020
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

src/GitHub.App/Api/ApiClientConfiguration.cs renamed to src/GitHub.Api/ApiClientConfiguration_User.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using System;
2+
13
namespace GitHub.Api
24
{
3-
public partial class ApiClient : IApiClient
5+
static partial class ApiClientConfiguration
46
{
57
const string clientId = "YOUR CLIENT ID HERE";
68
const string clientSecret = "YOUR CLIENT SECRET HERE";
79

8-
partial void Configure()
10+
static partial void Configure()
911
{
1012
ClientId = clientId;
1113
ClientSecret = clientSecret;

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<AssemblyName>GitHub.Api</AssemblyName>
1212
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14-
<BuildType Condition="Exists('..\..\script\src\ApiClientConfiguration.cs')">Internal</BuildType>
14+
<BuildType Condition="Exists('..\..\script\src\ApiClientConfiguration_User.cs')">Internal</BuildType>
1515
<OutputPath>bin\$(Configuration)\</OutputPath>
1616
<TargetFrameworkProfile />
1717
</PropertyGroup>
@@ -55,7 +55,17 @@
5555
<Reference Include="System.Xml" />
5656
</ItemGroup>
5757
<ItemGroup>
58+
<Compile Include="ApiClientConfiguration.cs"/>
59+
<Compile Include="..\..\script\src\ApiClientConfiguration_User.cs" Condition="$(Buildtype) == 'Internal'">
60+
<Link>ApiClientConfiguration_User.cs</Link>
61+
</Compile>
62+
<Compile Include="ApiClientConfiguration_User.cs" Condition="$(Buildtype) != 'Internal'"/>
63+
<Compile Include="ILoginCache.cs" />
64+
<Compile Include="ILoginManager.cs" />
65+
<Compile Include="ITwoFactorChallengeHandler.cs" />
66+
<Compile Include="LoginManager.cs" />
5867
<Compile Include="SimpleCredentialStore.cs" />
68+
<Compile Include="WindowsLoginCache.cs" />
5969
<None Include="..\..\script\Key.snk" Condition="$(Buildtype) == 'Internal'">
6070
<Link>Key.snk</Link>
6171
</None>

0 commit comments

Comments
 (0)