Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 354c1f3

Browse files
Getting it to work
1 parent 43b63f0 commit 354c1f3

File tree

13 files changed

+258
-180
lines changed

13 files changed

+258
-180
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ protected virtual void InitializeUI() {}
320320
protected virtual void InitializationComplete() {}
321321

322322
private bool disposed = false;
323+
private IOAuthCallbackManager oAuthCallbackManager;
324+
323325
protected virtual void Dispose(bool disposing)
324326
{
325327
if (disposing)
@@ -358,6 +360,20 @@ public void Dispose()
358360
public ISettings SystemSettings { get { return Environment.SystemSettings; } }
359361
public ISettings UserSettings { get { return Environment.UserSettings; } }
360362
public IUsageTracker UsageTracker { get; protected set; }
363+
364+
public IOAuthCallbackManager OAuthCallbackManager
365+
{
366+
get
367+
{
368+
if (oAuthCallbackManager == null)
369+
{
370+
oAuthCallbackManager = new OAuthCallbackManager();
371+
}
372+
373+
return oAuthCallbackManager;
374+
}
375+
}
376+
361377
public bool IsBusy { get { return isBusy; } }
362378
protected TaskScheduler UIScheduler { get; private set; }
363379
protected SynchronizationContext SynchronizationContext { get; private set; }

src/GitHub.Api/Application/IApplicationManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ public interface IApplicationManager : IDisposable
1616
ITaskManager TaskManager { get; }
1717
IGitClient GitClient { get; }
1818
IUsageTracker UsageTracker { get; }
19+
IOAuthCallbackManager OAuthCallbackManager { get; }
1920
bool IsBusy { get; }
2021
void Run();
2122
void InitializeRepository();
2223
event Action<IProgress> OnProgress;
2324
void SetupGit(GitInstaller.GitInstallationState state);
2425
void RestartRepository();
2526
}
26-
}
27+
}

src/GitHub.Api/Authentication/OAuthCallbackListener.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using System.Web;
8+
using GitHub.Logging;
9+
10+
namespace GitHub.Unity
11+
{
12+
public interface IOAuthCallbackManager
13+
{
14+
event Action<string, string> OnCallback;
15+
bool IsRunning { get; }
16+
void Start();
17+
void Stop();
18+
}
19+
20+
public class OAuthCallbackManager : IOAuthCallbackManager
21+
{
22+
const int CallbackPort = 42424;
23+
public static readonly Uri CallbackUrl = new Uri($"http://localhost:{CallbackPort}/callback");
24+
25+
private static readonly ILogging logger = LogHelper.GetLogger<OAuthCallbackManager>();
26+
private static readonly object _lock = new object();
27+
28+
29+
private readonly CancellationTokenSource cancelSource;
30+
31+
private HttpListener httpListener;
32+
public bool IsRunning { get; private set; }
33+
34+
public event Action<string, string> OnCallback;
35+
36+
public OAuthCallbackManager()
37+
{
38+
cancelSource = new CancellationTokenSource();
39+
}
40+
41+
public void Start()
42+
{
43+
if (!IsRunning)
44+
{
45+
lock(_lock)
46+
{
47+
if (!IsRunning)
48+
{
49+
logger.Trace("Starting");
50+
51+
httpListener = new HttpListener();
52+
httpListener.Prefixes.Add(CallbackUrl.AbsoluteUri + "/");
53+
httpListener.Start();
54+
Task.Factory.StartNew(Listen, cancelSource.Token);
55+
IsRunning = true;
56+
}
57+
}
58+
}
59+
}
60+
61+
public void Stop()
62+
{
63+
logger.Debug("Stop");
64+
}
65+
66+
private void Listen()
67+
{
68+
try
69+
{
70+
using (httpListener)
71+
{
72+
using (cancelSource.Token.Register(httpListener.Stop))
73+
{
74+
while (true)
75+
{
76+
var context = httpListener.GetContext();
77+
var queryParts = HttpUtility.ParseQueryString(context.Request.Url.Query);
78+
79+
var state = queryParts["state"];
80+
var code = queryParts["code"];
81+
82+
logger.Trace("OnCallback: {0}", state);
83+
if (OnCallback != null)
84+
{
85+
OnCallback(state, code);
86+
}
87+
88+
context.Response.StatusCode = 200;
89+
context.Response.OutputStream.Flush();
90+
context.Response.Close();
91+
}
92+
}
93+
}
94+
}
95+
catch (Exception ex)
96+
{
97+
logger.Warning(ex, "OAuthCallbackManager Error");
98+
}
99+
finally
100+
{
101+
IsRunning = false;
102+
httpListener = null;
103+
}
104+
}
105+
}
106+
}

src/GitHub.Api/GitHub.Api.45.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<Compile Include="Application\LoginResult.cs" />
7474
<Compile Include="Application\ApplicationConfiguration.cs" />
7575
<Compile Include="Application\Organization.cs" />
76-
<Compile Include="Authentication\OAuthCallbackListener.cs" />
76+
<Compile Include="Authentication\OAuthCallbackManager.cs" />
7777
<Compile Include="Cache\CacheContainer.cs" />
7878
<Compile Include="Cache\CacheInterfaces.cs" />
7979
<Compile Include="Cache\CachingClasses.cs" />

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<Compile Include="Application\LoginResult.cs" />
8585
<Compile Include="Application\ApplicationConfiguration.cs" />
8686
<Compile Include="Application\Organization.cs" />
87-
<Compile Include="Authentication\OAuthCallbackListener.cs" />
87+
<Compile Include="Authentication\OAuthCallbackManager.cs" />
8888
<Compile Include="Cache\CacheContainer.cs" />
8989
<Compile Include="Cache\CacheInterfaces.cs" />
9090
<Compile Include="Cache\CachingClasses.cs" />
@@ -338,23 +338,9 @@
338338
</Target>
339339
-->
340340
<Target Name="AfterBuild">
341-
<Copy
342-
SourceFiles="PlatformResources\linux\git-lfs.zip;PlatformResources\linux\git-lfs.json"
343-
DestinationFolder="$(SolutionDir)\src\UnityExtension\Assets\Editor\GitHub.Unity\PlatformResources\linux"
344-
SkipUnchangedFiles="true"
345-
/>
346-
347-
<Copy
348-
SourceFiles="PlatformResources\mac\git-lfs.zip;PlatformResources\mac\git-lfs.json"
349-
DestinationFolder="$(SolutionDir)\src\UnityExtension\Assets\Editor\GitHub.Unity\PlatformResources\mac"
350-
SkipUnchangedFiles="true"
351-
/>
352-
353-
<Copy
354-
SourceFiles="PlatformResources\windows\git.zip;PlatformResources\windows\git.json;PlatformResources\windows\git-lfs.zip;PlatformResources\windows\git-lfs.json"
355-
DestinationFolder="$(SolutionDir)\src\UnityExtension\Assets\Editor\GitHub.Unity\PlatformResources\windows"
356-
SkipUnchangedFiles="true"
357-
/>
341+
<Copy SourceFiles="PlatformResources\linux\git-lfs.zip;PlatformResources\linux\git-lfs.json" DestinationFolder="$(SolutionDir)\src\UnityExtension\Assets\Editor\GitHub.Unity\PlatformResources\linux" SkipUnchangedFiles="true" />
342+
<Copy SourceFiles="PlatformResources\mac\git-lfs.zip;PlatformResources\mac\git-lfs.json" DestinationFolder="$(SolutionDir)\src\UnityExtension\Assets\Editor\GitHub.Unity\PlatformResources\mac" SkipUnchangedFiles="true" />
343+
<Copy SourceFiles="PlatformResources\windows\git.zip;PlatformResources\windows\git.json;PlatformResources\windows\git-lfs.zip;PlatformResources\windows\git-lfs.json" DestinationFolder="$(SolutionDir)\src\UnityExtension\Assets\Editor\GitHub.Unity\PlatformResources\windows" SkipUnchangedFiles="true" />
358344
</Target>
359345
<Import Project="..\..\common\build.targets" />
360346
</Project>

src/GitHub.Api/Platform/IEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ public interface IEnvironment
4242
ISettings SystemSettings { get; }
4343
ISettings UserSettings { get; }
4444
}
45-
}
45+
}

src/UnityExtension/Assets/Editor/GitHub.Unity/Services/AuthenticationService.cs

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,17 @@
55

66
namespace GitHub.Unity
77
{
8-
class AuthenticationService: IDisposable
8+
class AuthenticationService
99
{
10-
private readonly ITaskManager taskManager;
11-
private static readonly ILogging logger = LogHelper.GetLogger<AuthenticationService>();
12-
1310
private readonly IApiClient client;
1411

1512
private LoginResult loginResultData;
16-
private IOAuthCallbackListener oauthCallbackListener;
17-
private CancellationTokenSource oauthCallbackCancellationToken;
18-
private string oauthCallbackState;
1913

2014
public AuthenticationService(UriString host, IKeychain keychain,
2115
IProcessManager processManager, ITaskManager taskManager,
2216
IEnvironment environment
2317
)
2418
{
25-
this.taskManager = taskManager;
2619
client = host == null
2720
? new ApiClient(keychain, processManager, taskManager, environment)
2821
: new ApiClient(host, keychain, processManager, taskManager, environment);
@@ -63,56 +56,14 @@ public void GetServerMeta(Action<GitHubHostMeta> serverMeta, Action<string> erro
6356
});
6457
}
6558

66-
public Uri StartOAuthListener(Action onSuccess, Action<string> onError)
67-
{
68-
if (oauthCallbackListener == null)
69-
{
70-
logger.Trace("Start OAuthCallbackListener");
71-
oauthCallbackListener = new OAuthCallbackListener();
72-
oauthCallbackCancellationToken = new CancellationTokenSource();
73-
74-
oauthCallbackState = Guid.NewGuid().ToString();
75-
oauthCallbackListener.Listen(
76-
oauthCallbackState,
77-
oauthCallbackCancellationToken.Token,
78-
code => {
79-
logger.Trace("OAuthCallbackListener Response: {0}", code);
80-
81-
client.CreateOAuthToken(code, (b, s) => {
82-
if (b)
83-
{
84-
onSuccess();
85-
}
86-
else
87-
{
88-
onError(s);
89-
}
90-
});
91-
});
92-
}
93-
94-
return GetLoginUrl(oauthCallbackState);
95-
}
96-
97-
public void StopOAuthListener()
98-
{
99-
if (oauthCallbackCancellationToken != null)
100-
{
101-
oauthCallbackCancellationToken.Cancel();
102-
}
103-
104-
oauthCallbackListener = null;
105-
oauthCallbackCancellationToken = null;
106-
}
107-
108-
private Uri GetLoginUrl(string state)
59+
public Uri GetLoginUrl(string state)
10960
{
11061
var query = new StringBuilder();
11162

11263
query.Append("client_id=");
11364
query.Append(Uri.EscapeDataString(ApplicationInfo.ClientId));
11465
query.Append("&redirect_uri=");
115-
query.Append(Uri.EscapeDataString("http://localhost:42424/callback"));
66+
query.Append(Uri.EscapeDataString(OAuthCallbackManager.CallbackUrl.ToString()));
11667
query.Append("&scope=");
11768
query.Append(Uri.EscapeDataString("user,repo"));
11869
query.Append("&state=");
@@ -126,10 +77,9 @@ private Uri GetLoginUrl(string state)
12677
return uriBuilder.Uri;
12778
}
12879

129-
public void Dispose()
80+
public void LoginWithOAuthCode(string code, Action<bool, string> result)
13081
{
131-
if (oauthCallbackCancellationToken != null)
132-
oauthCallbackCancellationToken.Dispose();
82+
client.CreateOAuthToken(code, result);
13383
}
13484
}
13585
}

0 commit comments

Comments
 (0)