-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOauthCallbackListener.cs
executable file
·54 lines (43 loc) · 1.59 KB
/
OauthCallbackListener.cs
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
using System;
using System.Net;
using System.Threading.Tasks;
using Hearthstone_Deck_Tracker.Utility.Logging;
namespace TwitchPredictionsBG
{
// For now we can only authenticate user with only `response_type == code`
class OauthCallbackListener
{
public static async Task HandleIncomingConnection(HttpListener listener, Action<string> callback)
{
bool run = true;
Log.Info($"Starting Web Server");
while (run)
{
// Will wait here until we hear from a connection
HttpListenerContext ctx = await listener.GetContextAsync();
// Peel out the requests and response objects
HttpListenerRequest req = ctx.Request;
HttpListenerResponse resp = ctx.Response;
Log.Info(req.QueryString.ToString());
string code = req.QueryString.Get("code");
if (code != null)
{
Log.Info($"Got Code");
callback(code);
run = false;
}
resp.Close();
}
}
public static void ServeCallback(string url, Action<string> callback)
{
var listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
Task listenTask = HandleIncomingConnection(listener, callback);
listenTask.GetAwaiter().GetResult();
// Close the listener
listener.Close();
}
}
}