forked from evemondevteam/evemon
/
SSOWebServerHttpListener.cs
215 lines (204 loc) · 8.06 KB
/
SSOWebServerHttpListener.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using EVEMon.Common.Constants;
using EVEMon.Common.Helpers;
using EVEMon.Common.Threading;
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace EVEMon.Common.Service
{
/// <summary>
/// A simple web server that is used to receive callback information from SSO using
/// HttpListener.
///
/// SSO was really meant for web apps so this is the best we can do...
/// </summary>
public class SSOWebServerHttpListener : ISSOWebServer
{
// A random port would be nice, but the API requires a fixed callback URL
public const int PORT = 4916;
// Used for initializing the responses properly
private static readonly object RESPONSE_LOCK = new object();
// Time before idle HTTP connections are closed
private static readonly TimeSpan TIMEOUT_IDLE = TimeSpan.FromSeconds(10.0);
// Time before connections are closed while waiting for read data
private static readonly TimeSpan TIMEOUT_READ = TimeSpan.FromSeconds(3.0);
// Time before connections are closed while waiting for write data
private static readonly TimeSpan TIMEOUT_WRITE = TimeSpan.FromSeconds(2.0);
// Encoded responses for client requests
private static byte[] responseOK = null;
private static byte[] response404 = null;
// Initializes the text responses sent to the client
private static void InitResponses()
{
lock (RESPONSE_LOCK)
{
if (response404 == null || responseOK == null)
{
response404 = Encoding.UTF8.GetBytes(Properties.Resources.CallbackFail);
responseOK = Encoding.UTF8.GetBytes(Properties.Resources.CallbackOK);
}
}
}
// The TCP listener used to receive requests
// We expect few requests, so we can get away with a single thread
private readonly HttpListener listener;
public SSOWebServerHttpListener()
{
if (!HttpListener.IsSupported)
throw new InvalidOperationException("HTTP listener not supported");
listener = new HttpListener();
// Calculate prefix, must end with slash according to HttpListener documentation
string prefix = string.Format(NetworkConstants.SSORedirect, PORT);
if (!prefix.EndsWith("/"))
prefix += "/";
listener.Prefixes.Add(prefix);
// Where would the exception go otherwise?
listener.IgnoreWriteExceptions = true;
// Set up the desired timeouts
listener.TimeoutManager.IdleConnection = TIMEOUT_IDLE;
listener.TimeoutManager.DrainEntityBody = TIMEOUT_WRITE;
listener.TimeoutManager.EntityBody = TIMEOUT_READ;
listener.TimeoutManager.HeaderWait = TIMEOUT_READ;
listener.TimeoutManager.RequestQueue = TIMEOUT_WRITE;
InitResponses();
}
/// <summary>
/// Asynchronously waits for an auth code in the background.
/// </summary>
/// <param name="state">The SSO state used.</param>
/// <param name="callback">The callback which will be invoked when the code is
/// received, reception fails, or the server is stopped.</param>
public void BeginWaitForCode(string state, Action<Task<string>> callback)
{
if (string.IsNullOrEmpty(state))
throw new ArgumentNullException("state");
WaitForCodeAsync(state).ContinueWith((result) => Dispatcher.Invoke(() =>
callback?.Invoke(result)));
}
public void Dispose()
{
listener.Stop();
listener.Close();
}
/// <summary>
/// Responds to the client which requests the specified URL.
/// </summary>
/// <param name="state">The SSO state used.</param>
/// <param name="output">The response where the output will be sent.</param>
/// <param name="queryParams">The arguments from the query.</param>
/// <returns></returns>
private async Task<string> SendReponseAsync(string state, HttpListenerResponse output,
NameValueCollection queryParams)
{
string code = "";
byte[] response;
HttpStatusCode responseCode;
// Check for matching state in response
var stateParams = queryParams.GetValues("state");
if (stateParams != null && stateParams.Length == 1 && stateParams[0] == state)
{
var codeParams = queryParams.GetValues("code");
// Take the first one, only should be one
if (codeParams != null && codeParams.Length > 0)
code = codeParams[0];
}
// Choose the right response
if (string.IsNullOrEmpty(code))
{
response = response404;
responseCode = HttpStatusCode.NotFound;
}
else
{
response = responseOK;
responseCode = HttpStatusCode.OK;
}
// Send the response
using (var stream = output.OutputStream)
{
int len = response.Length;
// HTTP response code
output.StatusCode = (int)responseCode;
// Supply the length
output.ContentLength64 = len;
// Supply content type and encoding
output.ContentType = "text/html";
output.ContentEncoding = Encoding.UTF8;
await stream.WriteAsync(response, 0, len);
await stream.FlushAsync();
}
return code;
}
/// <summary>
/// Starts the web server.
/// </summary>
public void Start()
{
try
{
listener.Start();
}
catch (HttpListenerException e)
{
ExceptionHandler.LogException(e, true);
throw new IOException("Error when starting server", e);
}
}
/// <summary>
/// Stops the web server.
/// </summary>
public void Stop()
{
try
{
listener.Stop();
}
catch (HttpListenerException e)
{
ExceptionHandler.LogException(e, true);
}
}
/// <summary>
/// Waits for the auth code asynchronously; the reported state must match the argument.
/// </summary>
/// <param name="state">The SSO state.</param>
/// <returns>The token received, or null if none was received.</returns>
public async Task<string> WaitForCodeAsync(string state)
{
// Blank states are bad
if (string.IsNullOrEmpty(state))
throw new ArgumentNullException("state");
string code = string.Empty;
try
{
do
{
// Accept client
var context = await listener.GetContextAsync().ConfigureAwait(false);
using (var output = context.Response)
{
// Check for state in the URL
string query = context.Request.Url.Query;
if (query == null)
query = "";
var queryParams = HttpUtility.ParseQueryString(query);
code = await SendReponseAsync(state, output, queryParams);
}
} while (string.IsNullOrEmpty(code));
}
catch (ObjectDisposedException)
{
// Happens normally while shutting down
}
catch (HttpListenerException e)
{
throw new IOException("Error when waiting for auth code", e);
}
return code;
}
}
}