PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceStack/Host/HttpRequestAuthentication.cs

http://github.com/ServiceStack/ServiceStack
C# | 138 lines | 118 code | 17 blank | 3 comment | 21 complexity | b752cb9e6690a2f4205d6a2471bdb7f3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using ServiceStack.Web;
  5. namespace ServiceStack.Host
  6. {
  7. public static class HttpRequestAuthentication
  8. {
  9. public static string GetAuthorization(this IRequest httpReq)
  10. {
  11. var auth = httpReq.Items.TryGetValue(Keywords.Authorization, out var oAuth)
  12. ? oAuth as string
  13. : null;
  14. if (!string.IsNullOrEmpty(auth))
  15. return auth;
  16. auth = httpReq.Authorization;
  17. return string.IsNullOrEmpty(auth) ? null : auth;
  18. }
  19. public static string GetBearerToken(this IRequest httpReq)
  20. {
  21. if (httpReq.Dto is IHasBearerToken dto && dto.BearerToken != null)
  22. return dto.BearerToken;
  23. var auth = httpReq.GetAuthorization();
  24. if (string.IsNullOrEmpty(auth))
  25. return null;
  26. var pos = auth.IndexOf(' ');
  27. if (pos < 0)
  28. return null;
  29. var ret = auth.StartsWith("Bearer", StringComparison.OrdinalIgnoreCase)
  30. ? auth.Substring(pos + 1)
  31. : null;
  32. if (!string.IsNullOrEmpty(ret))
  33. return ret;
  34. return null;
  35. }
  36. public static string GetBasicAuth(this IRequest httpReq)
  37. {
  38. var auth = httpReq.GetAuthorization();
  39. if (auth == null)
  40. return null;
  41. var pos = auth.IndexOf(' ');
  42. return pos >= 0 && string.Equals("Basic", auth.Substring(0, pos), StringComparison.OrdinalIgnoreCase)
  43. ? auth.Substring(pos + 1)
  44. : null;
  45. }
  46. public static KeyValuePair<string, string>? GetBasicAuthUserAndPassword(this IRequest httpReq)
  47. {
  48. var userPassBase64 = httpReq.GetBasicAuth();
  49. if (userPassBase64 == null)
  50. return null;
  51. var userPass = Encoding.UTF8.GetString(Convert.FromBase64String(userPassBase64));
  52. var pos = userPass.IndexOf(':');
  53. if (pos < 0)
  54. return null;
  55. return new KeyValuePair<string, string>(userPass.Substring(0, pos), userPass.Substring(pos + 1));
  56. }
  57. public static Dictionary<string,string> GetDigestAuth(this IRequest httpReq)
  58. {
  59. var auth = httpReq.GetAuthorization();
  60. if (auth == null) return null;
  61. var parts = auth.Split(' ');
  62. // There should be at least to parts
  63. if (parts.Length < 2) return null;
  64. // It has to be a digest request
  65. if (parts[0].ToLowerInvariant() != "digest") return null;
  66. // Remove up til the first space
  67. auth = auth.Substring(auth.IndexOf(' '));
  68. int i = 0;
  69. int line = 0;
  70. bool inQuotes = false;
  71. bool escape = false;
  72. var prts = new List<string> { "" };
  73. auth = auth.Trim(' ', ',');
  74. while (i < auth.Length)
  75. {
  76. if (auth[i]=='"' && !escape)
  77. inQuotes = !inQuotes;
  78. if (auth[i] == ',' && !inQuotes && !escape)
  79. {
  80. i++;
  81. prts.Add("");
  82. line++;
  83. }
  84. else
  85. {
  86. escape = auth[i]=='\\';
  87. prts[line] += auth[i];
  88. i++;
  89. }
  90. }
  91. parts = prts.ToArray();
  92. try
  93. {
  94. var result = new Dictionary<string, string>();
  95. foreach (var item in parts)
  96. {
  97. var param = item.Trim().Split(new[] { '=' },2);
  98. result.Add(param[0],param[1].Trim('"'));
  99. }
  100. result.Add("method", httpReq.Verb);
  101. result.Add("userhostaddress", httpReq.UserHostAddress);
  102. return result;
  103. }
  104. catch (Exception) {}
  105. return null;
  106. }
  107. public static string GetCookieValue(this IRequest httpReq, string cookieName)
  108. {
  109. httpReq.Cookies.TryGetValue(cookieName, out var cookie);
  110. return cookie?.Value;
  111. }
  112. public static string GetItemStringValue(this IRequest httpReq, string itemName)
  113. {
  114. if (!httpReq.Items.TryGetValue(itemName, out var val)) return null;
  115. return val as string;
  116. }
  117. }
  118. }