forked from duosecurity/duo_api_csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duo.cs
740 lines (671 loc) · 29.8 KB
/
Duo.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
* Copyright (c) 2018 Duo Security
* All rights reserved
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Text;
using System.Web.Script.Serialization;
using System.Web;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
namespace Duo
{
public class DuoApi
{
public string DEFAULT_AGENT = "DuoAPICSharp/1.0";
private const int INITIAL_BACKOFF_MS = 1000;
private const int MAX_BACKOFF_MS = 32000;
private const int BACKOFF_FACTOR = 2;
private const int RATE_LIMIT_HTTP_CODE = 429;
private string ikey;
private string skey;
private string host;
private string url_scheme;
private string user_agent;
private SleepService sleepService;
private RandomService randomService;
/// <param name="ikey">Duo integration key</param>
/// <param name="skey">Duo secret key</param>
/// <param name="host">Application secret key</param>
public DuoApi(string ikey, string skey, string host)
: this(ikey, skey, host, null)
{
}
/// <param name="ikey">Duo integration key</param>
/// <param name="skey">Duo secret key</param>
/// <param name="host">Application secret key</param>
/// <param name="user_agent">HTTP client User-Agent</param>
public DuoApi(string ikey, string skey, string host, string user_agent)
: this(ikey, skey, host, user_agent, "https", new ThreadSleepService(), new SystemRandomService())
{
}
protected DuoApi(string ikey, string skey, string host, string user_agent, string url_scheme,
SleepService sleepService, RandomService randomService)
{
this.ikey = ikey;
this.skey = skey;
this.host = host;
this.url_scheme = url_scheme;
this.sleepService = sleepService;
this.randomService = randomService;
if (String.IsNullOrEmpty(user_agent))
{
this.user_agent = FormatUserAgent(DEFAULT_AGENT);
}
else
{
this.user_agent = user_agent;
}
}
public static string FinishCanonicalize(string p)
{
// Signatures require upper-case hex digits.
p = Regex.Replace(p,
"(%[0-9A-Fa-f][0-9A-Fa-f])",
c => c.Value.ToUpperInvariant());
// Escape only the expected characters.
p = Regex.Replace(p,
"([!'()*])",
c => "%" + Convert.ToByte(c.Value[0]).ToString("X"));
p = p.Replace("%7E", "~");
// UrlEncode converts space (" ") to "+". The
// signature algorithm requires "%20" instead. Actual
// + has already been replaced with %2B.
p = p.Replace("+", "%20");
return p;
}
public static string CanonicalizeParams(Dictionary<string, string> parameters)
{
var ret = new List<String>();
foreach (KeyValuePair<string, string> pair in parameters)
{
string p = String.Format("{0}={1}",
HttpUtility.UrlEncode(pair.Key),
HttpUtility.UrlEncode(pair.Value));
p = FinishCanonicalize(p);
ret.Add(p);
}
ret.Sort(StringComparer.Ordinal);
return string.Join("&", ret.ToArray());
}
// handle value as an object eg. next_offset = ["123", "fdajkld"]
public static string CanonicalizeParams(Dictionary<string, object> parameters)
{
var ret = new List<String>();
foreach (KeyValuePair<string, object> pair in parameters)
{
string p = "";
if (pair.Value.GetType() == typeof(string[]))
{
string[] values = (string[])pair.Value;
string value1 = values[0];
string value2 = values[1];
p = String.Format("{0}={1}&{2}={3}",
HttpUtility.UrlEncode(pair.Key),
HttpUtility.UrlEncode(value1),
HttpUtility.UrlEncode(pair.Key),
HttpUtility.UrlEncode(value2));
}
else
{
string val = (string)pair.Value;
p = String.Format("{0}={1}",
HttpUtility.UrlEncode(pair.Key),
HttpUtility.UrlEncode(val));
}
p = FinishCanonicalize(p);
ret.Add(p);
}
ret.Sort(StringComparer.Ordinal);
return string.Join("&", ret.ToArray());
}
protected string CanonicalizeRequest(string method,
string path,
string canon_params,
string date)
{
string[] lines = {
date,
method.ToUpperInvariant(),
this.host.ToLower(),
path,
canon_params,
};
string canon = String.Join("\n",
lines);
return canon;
}
public string Sign(string method,
string path,
string canon_params,
string date)
{
string canon = this.CanonicalizeRequest(method,
path,
canon_params,
date);
string sig = this.HmacSign(canon);
string auth = this.ikey + ':' + sig;
return "Basic " + DuoApi.Encode64(auth);
}
public string ApiCall(string method,
string path,
Dictionary<string, string> parameters)
{
HttpStatusCode statusCode;
return ApiCall(method, path, parameters, 0, DateTime.UtcNow, out statusCode);
}
/// <param name="timeout">The request timeout, in milliseconds.
/// Specify 0 to use the system-default timeout. Use caution if
/// you choose to specify a custom timeout - some API
/// calls (particularly in the Auth APIs) will not
/// return a response until an out-of-band authentication process
/// has completed. In some cases, this may take as much as a
/// small number of minutes.</param>
public string ApiCall(string method,
string path,
Dictionary<string, string> parameters,
int timeout,
out HttpStatusCode statusCode)
{
return ApiCall(method, path, parameters, 0, DateTime.UtcNow, out statusCode);
}
/// <param name="date">The current date and time, used to authenticate
/// the API request. Typically, you should specify DateTime.UtcNow,
/// but if you do not wish to rely on the system-wide clock, you may
/// determine the current date/time by some other means.</param>
/// <param name="timeout">The request timeout, in milliseconds.
/// Specify 0 to use the system-default timeout. Use caution if
/// you choose to specify a custom timeout - some API
/// calls (particularly in the Auth APIs) will not
/// return a response until an out-of-band authentication process
/// has completed. In some cases, this may take as much as a
/// small number of minutes.</param>
public string ApiCall(string method,
string path,
Dictionary<string, string> parameters,
int timeout,
DateTime date,
out HttpStatusCode statusCode)
{
string canon_params = DuoApi.CanonicalizeParams(parameters);
string query = "";
if (! method.Equals("POST") && ! method.Equals("PUT"))
{
if (parameters.Count > 0)
{
query = "?" + canon_params;
}
}
string url = string.Format("{0}://{1}{2}{3}",
this.url_scheme,
this.host,
path,
query);
string date_string = DuoApi.DateToRFC822(date);
string auth = this.Sign(method, path, canon_params, date_string);
HttpWebResponse response = AttemptRetriableHttpRequest(
method, url, auth, date_string, canon_params, timeout);
StreamReader reader
= new StreamReader(response.GetResponseStream());
statusCode = response.StatusCode;
return reader.ReadToEnd();
}
private HttpWebRequest PrepareHttpRequest(String method, String url, String auth, String date,
String cannonParams, int timeout)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method;
request.Accept = "application/json";
request.Headers.Add("Authorization", auth);
request.Headers.Add("X-Duo-Date", date);
request.UserAgent = this.user_agent;
// If no proxy, check for and use WinHTTP proxy as autoconfig won't pick this up when run from a service
if (!HasProxyServer(request))
request.Proxy = GetWinhttpProxy();
if (method.Equals("POST") || method.Equals("PUT"))
{
byte[] data = Encoding.UTF8.GetBytes(cannonParams);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
}
}
if (timeout > 0)
{
request.Timeout = timeout;
}
return request;
}
private HttpWebResponse AttemptRetriableHttpRequest(
String method, String url, String auth, String date, String cannonParams, int timeout)
{
int backoffMs = INITIAL_BACKOFF_MS;
while(true) {
// Do the request and process the result.
HttpWebRequest request = PrepareHttpRequest(method, url, auth, date, cannonParams, timeout);
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
if (response == null)
{
throw;
}
}
if (response.StatusCode != (HttpStatusCode)RATE_LIMIT_HTTP_CODE || backoffMs > MAX_BACKOFF_MS) {
return response;
}
sleepService.Sleep(backoffMs + randomService.GetInt(1001));
backoffMs *= BACKOFF_FACTOR;
}
}
/// <param name="date">The current date and time, used to authenticate
/// the API request. Typically, you should specify DateTime.UtcNow,
/// but if you do not wish to rely on the system-wide clock, you may
/// determine the current date/time by some other means.</param>
/// <param name="timeout">The request timeout, in milliseconds.
/// Specify 0 to use the system-default timeout. Use caution if
/// you choose to specify a custom timeout - some API
/// calls (particularly in the Auth APIs) will not
/// return a complete JSON response.</param>
/// raises if JSON response indicates an error
private Dictionary<string, object> BaseJSONApiCall(string method,
string path,
Dictionary<string, string> parameters,
int timeout,
DateTime date)
{
HttpStatusCode statusCode;
string res = this.ApiCall(method, path, parameters, timeout, date, out statusCode);
var jss = new JavaScriptSerializer();
try
{
var dict = jss.Deserialize<Dictionary<string, object>>(res);
if (dict["stat"] as string == "OK")
{
return dict;
}
else
{
int? check = dict["code"] as int?;
int code;
if (check.HasValue)
{
code = check.Value;
}
else
{
code = 0;
}
String message_detail = "";
if (dict.ContainsKey("message_detail"))
{
message_detail = dict["message_detail"] as string;
}
throw new ApiException(code,
(int)statusCode,
dict["message"] as string,
message_detail);
}
}
catch (ApiException)
{
throw;
}
catch (Exception e)
{
throw new BadResponseException((int)statusCode, e);
}
}
public T JSONApiCall<T>(string method,
string path,
Dictionary<string, string> parameters)
where T : class
{
return JSONApiCall<T>(method, path, parameters, 0, DateTime.UtcNow);
}
/// <param name="timeout">The request timeout, in milliseconds.
/// Specify 0 to use the system-default timeout. Use caution if
/// you choose to specify a custom timeout - some API
/// calls (particularly in the Auth APIs) will not
/// return a response until an out-of-band authentication process
/// has completed. In some cases, this may take as much as a
/// small number of minutes.</param>
public T JSONApiCall<T>(string method,
string path,
Dictionary<string, string> parameters,
int timeout)
where T : class
{
return JSONApiCall<T>(method, path, parameters, timeout, DateTime.UtcNow);
}
/// <param name="date">The current date and time, used to authenticate
/// the API request. Typically, you should specify DateTime.UtcNow,
/// but if you do not wish to rely on the system-wide clock, you may
/// determine the current date/time by some other means.</param>
/// <param name="timeout">The request timeout, in milliseconds.
/// Specify 0 to use the system-default timeout. Use caution if
/// you choose to specify a custom timeout - some API
/// calls (particularly in the Auth APIs) will not
/// return a response until an out-of-band authentication process
/// has completed. In some cases, this may take as much as a
/// small number of minutes.</param>
public T JSONApiCall<T>(string method,
string path,
Dictionary<string, string> parameters,
int timeout,
DateTime date)
where T : class
{
var dict = BaseJSONApiCall(method, path, parameters, timeout, date);
return dict["response"] as T;
}
public Dictionary<string, object> JSONPagingApiCall(string method,
string path,
Dictionary<string, string> parameters,
int offset,
int limit)
{
return JSONPagingApiCall(method, path, parameters, offset, limit, 0, DateTime.UtcNow);
}
/// <param name="date">The current date and time, used to authenticate
/// the API request. Typically, you should specify DateTime.UtcNow,
/// but if you do not wish to rely on the system-wide clock, you may
/// determine the current date/time by some other means.</param>
/// <param name="timeout">The request timeout, in milliseconds.
/// Specify 0 to use the system-default timeout. Use caution if
/// you choose to specify a custom timeout - some API
/// calls (particularly in the Auth APIs) will not
/// return a response until an out-of-band authentication process
/// has completed. In some cases, this may take as much as a
/// small number of minutes.</param>
/// <param name="offset">The offset of the first record in the next
/// page of results within the total result set.</param>
/// <param name="limit">The number of records you would like returned
/// with this request. The service is free to return a different
/// number. You should use the 'next_offset' from the returned
/// metadata to pick the offset for the next page.</param>
/// return a JSON dictionary with top level keys: stat, response, metadata.
/// The actual requested data is in 'response'. 'metadata' contains a
/// 'next_offset' key which should be used to fetch the next page.
public Dictionary<string, object> JSONPagingApiCall(string method,
string path,
Dictionary<string, string> parameters,
int offset,
int limit,
int timeout,
DateTime date)
{
// copy parameters so we don't cause any side-effects
parameters = new Dictionary<string, string>(parameters);
parameters["offset"] = offset.ToString(); // overrides caller value
parameters["limit"] = limit.ToString();
return this.BaseJSONApiCall(method, path, parameters, timeout, date);
}
/// Helper to format a User-Agent string with some information about
/// the operating system / .NET runtime
/// <param name="product_name">e.g. "FooClient/1.0"</param>
public static string FormatUserAgent(string product_name)
{
return String.Format(
"{0} ({1}; .NET {2})", product_name, System.Environment.OSVersion,
System.Environment.Version);
}
#region Private Methods
private string HmacSign(string data)
{
byte[] key_bytes = ASCIIEncoding.ASCII.GetBytes(this.skey);
HMACSHA512 hmac = new HMACSHA512(key_bytes);
byte[] data_bytes = ASCIIEncoding.ASCII.GetBytes(data);
hmac.ComputeHash(data_bytes);
string hex = BitConverter.ToString(hmac.Hash);
return hex.Replace("-", "").ToLower();
}
private static string Encode64(string plaintext)
{
byte[] plaintext_bytes = ASCIIEncoding.ASCII.GetBytes(plaintext);
string encoded = System.Convert.ToBase64String(plaintext_bytes);
return encoded;
}
private static string DateToRFC822(DateTime date)
{
// Can't use the "zzzz" format because it adds a ":"
// between the offset's hours and minutes.
string date_string = date.ToString(
"ddd, dd MMM yyyy HH:mm:ss", CultureInfo.InvariantCulture);
int offset = TimeZoneInfo.Local.GetUtcOffset(date).Hours;
string zone;
// + or -, then 0-pad, then offset, then more 0-padding.
if (offset < 0)
{
offset *= -1;
zone = "-";
}
else
{
zone = "+";
}
zone += offset.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0');
date_string += " " + zone.PadRight(5, '0');
return date_string;
}
/// <summary>
/// Gets the WinHTTP proxy.
/// </summary>
/// <remarks>
/// Normally, C# picks up these proxy settings by default, but when run under the SYSTEM account, it does not.
/// </remarks>
/// <returns></returns>
private static System.Net.WebProxy GetWinhttpProxy()
{
string[] proxyServerNames = null;
string primaryProxyServer = null;
string[] bypassHostnames = null;
bool enableLocalBypass = false;
System.Net.WebProxy winhttpProxy = null;
// Has a proxy been configured?
// No. Is a WinHTTP proxy set?
int internetHandle = WinHttpOpen("DuoTest", WinHttp_Access_Type.WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, null, null, 0);
if (internetHandle != 0)
{
// Yes, use it. This is normal when run under the SYSTEM account and a WinHTTP proxy is configured. When run as a normal user,
// the Proxy property will already be configured correctly. To resolve this for SYSTEM, manually read proxy settings and configure.
var proxyInfo = new WINHTTP_PROXY_INFO();
WinHttpGetDefaultProxyConfiguration(proxyInfo);
if (proxyInfo.lpszProxy != null)
{
if (proxyInfo.lpszProxy != null)
{
proxyServerNames = proxyInfo.lpszProxy.Split(new char[] { ' ', '\t', ';' });
if ((proxyServerNames == null) || (proxyServerNames.Length == 0))
primaryProxyServer = proxyInfo.lpszProxy;
else
primaryProxyServer = proxyServerNames[0];
}
if (proxyInfo.lpszProxyBypass != null)
{
bypassHostnames = proxyInfo.lpszProxyBypass.Split(new char[] { ' ', '\t', ';' });
if ((bypassHostnames == null) || (bypassHostnames.Length == 0))
bypassHostnames = new string[] { proxyInfo.lpszProxyBypass };
if (bypassHostnames != null)
enableLocalBypass = bypassHostnames.Contains("local", StringComparer.InvariantCultureIgnoreCase);
}
if (primaryProxyServer != null)
winhttpProxy = new System.Net.WebProxy(proxyServerNames[0], enableLocalBypass, bypassHostnames);
}
WinHttpCloseHandle(internetHandle);
internetHandle = 0;
}
else
{
throw new Exception(String.Format("WinHttp init failed {0}", System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
}
return winhttpProxy;
}
/// <summary>
/// Determines if the specified web request is using a proxy server.
/// </summary>
/// <remarks>
/// If no proxy is set, the Proxy member is typically non-null and set to an object type that includes but hides IWebProxy with no address,
/// so it cannot be inspected. Resolving this requires reflection to extract the hidden webProxy object and check it's Address member.
/// </remarks>
/// <param name="requestObject">Request to check</param>
/// <returns>TRUE if a proxy is in use, else FALSE</returns>
public static bool HasProxyServer(HttpWebRequest requestObject)
{
WebProxy actualProxy = null;
bool hasProxyServer = false;
if (requestObject.Proxy != null)
{
// WebProxy is described as the base class for IWebProxy, so we should always see this type as the field is initialized by the framework.
if (!(requestObject.Proxy is WebProxy))
{
var webProxyField = requestObject.Proxy.GetType().GetField("webProxy", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
if (webProxyField != null)
actualProxy = webProxyField.GetValue(requestObject.Proxy) as WebProxy;
}
else
{
actualProxy = requestObject.Proxy as WebProxy;
}
hasProxyServer = (actualProxy.Address != null);
}
else
{
hasProxyServer = false;
}
return hasProxyServer;
}
#endregion Private Methods
#region Private DllImport
private enum WinHttp_Access_Type
{
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY = 0,
WINHTTP_ACCESS_TYPE_NO_PROXY = 1,
WINHTTP_ACCESS_TYPE_NAMED_PROXY = 3,
/// <summary>
/// Undocumented; supported on Win8.1+ per NET framework source.
/// </summary>
WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY = 4
}
[StructLayout(LayoutKind.Sequential)]
private class WINHTTP_PROXY_INFO
{
public int dwAccessType;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszProxy;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszProxyBypass;
}
[DllImport("winhttp.dll", CharSet = CharSet.Unicode)]
private static extern bool WinHttpGetDefaultProxyConfiguration([In, Out] WINHTTP_PROXY_INFO proxyInfo);
[DllImport("winhttp.dll", CharSet = CharSet.Unicode)]
private static extern int WinHttpOpen([MarshalAs(UnmanagedType.LPWStr)] string pwszUserAgent,
WinHttp_Access_Type dwAccessType,
[MarshalAs(UnmanagedType.LPWStr)] string pwszProxyName,
[MarshalAs(UnmanagedType.LPWStr)] string pwszProxyBypass,
int dwFlags);
[DllImport("winhttp.dll", CharSet = CharSet.Unicode)]
private static extern bool WinHttpCloseHandle(int hInternet);
#endregion Private DllImport
}
[Serializable]
public class DuoException : Exception
{
public int HttpStatus { get; private set; }
public DuoException(int http_status, string message, Exception inner)
: base(message, inner)
{
this.HttpStatus = http_status;
}
protected DuoException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext ctxt)
: base(info, ctxt)
{ }
}
[Serializable]
public class ApiException : DuoException
{
public int Code { get; private set; }
public string ApiMessage { get; private set; }
public string ApiMessageDetail { get; private set; }
public ApiException(int code,
int http_status,
string api_message,
string api_message_detail)
: base(http_status, FormatMessage(code, api_message, api_message_detail), null)
{
this.Code = code;
this.ApiMessage = api_message;
this.ApiMessageDetail = api_message_detail;
}
protected ApiException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext ctxt)
: base(info, ctxt)
{ }
private static string FormatMessage(int code,
string api_message,
string api_message_detail)
{
return String.Format(
"Duo API Error {0}: '{1}' ('{2}')", code, api_message, api_message_detail);
}
}
[Serializable]
public class BadResponseException : DuoException
{
public BadResponseException(int http_status, Exception inner)
: base(http_status, FormatMessage(http_status, inner), inner)
{ }
protected BadResponseException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext ctxt)
: base(info, ctxt)
{ }
private static string FormatMessage(int http_status, Exception inner)
{
string inner_message = "(null)";
if (inner != null)
{
inner_message = String.Format("'{0}'", inner.Message);
}
return String.Format(
"Got error {0} with HTTP Status {1}", inner_message, http_status);
}
}
public interface SleepService
{
void Sleep(int ms);
}
public interface RandomService
{
int GetInt(int maxInt);
}
class ThreadSleepService : SleepService
{
public void Sleep(int ms)
{
System.Threading.Thread.Sleep(ms);
}
}
class SystemRandomService : RandomService
{
private Random rand = new Random();
public int GetInt(int maxInt)
{
return rand.Next(maxInt);
}
}
}