AuthorizationRequestFilterAttribute.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Session;
  4. using MediaBrowser.Model.Logging;
  5. using ServiceStack.Common.Web;
  6. using ServiceStack.ServiceHost;
  7. using System;
  8. using System.Collections.Generic;
  9. namespace MediaBrowser.Api
  10. {
  11. public class AuthorizationRequestFilterAttribute : Attribute, IHasRequestFilter
  12. {
  13. //This property will be resolved by the IoC container
  14. /// <summary>
  15. /// Gets or sets the user manager.
  16. /// </summary>
  17. /// <value>The user manager.</value>
  18. public IUserManager UserManager { get; set; }
  19. public ISessionManager SessionManager { get; set; }
  20. /// <summary>
  21. /// Gets or sets the logger.
  22. /// </summary>
  23. /// <value>The logger.</value>
  24. public ILogger Logger { get; set; }
  25. /// <summary>
  26. /// The request filter is executed before the service.
  27. /// </summary>
  28. /// <param name="request">The http request wrapper</param>
  29. /// <param name="response">The http response wrapper</param>
  30. /// <param name="requestDto">The request DTO</param>
  31. public void RequestFilter(IHttpRequest request, IHttpResponse response, object requestDto)
  32. {
  33. //This code is executed before the service
  34. var auth = GetAuthorization(request);
  35. if (auth != null)
  36. {
  37. User user = null;
  38. if (auth.ContainsKey("UserId"))
  39. {
  40. var userId = auth["UserId"];
  41. if (!string.IsNullOrEmpty(userId))
  42. {
  43. user = UserManager.GetUserById(new Guid(userId));
  44. }
  45. }
  46. string deviceId;
  47. string device;
  48. string client;
  49. string version;
  50. auth.TryGetValue("DeviceId", out deviceId);
  51. auth.TryGetValue("Device", out device);
  52. auth.TryGetValue("Client", out client);
  53. auth.TryGetValue("Version", out version);
  54. if (!string.IsNullOrEmpty(client) && !string.IsNullOrEmpty(deviceId) && !string.IsNullOrEmpty(device) && !string.IsNullOrEmpty(version))
  55. {
  56. SessionManager.LogConnectionActivity(client, version, deviceId, device, user);
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Gets the auth.
  62. /// </summary>
  63. /// <param name="httpReq">The HTTP req.</param>
  64. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  65. public static Dictionary<string, string> GetAuthorization(IHttpRequest httpReq)
  66. {
  67. var auth = httpReq.Headers[HttpHeaders.Authorization];
  68. return GetAuthorization(auth);
  69. }
  70. /// <summary>
  71. /// Gets the authorization.
  72. /// </summary>
  73. /// <param name="httpReq">The HTTP req.</param>
  74. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  75. public static AuthorizationInfo GetAuthorization(IRequestContext httpReq)
  76. {
  77. var header = httpReq.GetHeader("Authorization");
  78. var auth = GetAuthorization(header);
  79. string userId;
  80. string deviceId;
  81. string device;
  82. string client;
  83. string version;
  84. auth.TryGetValue("UserId", out userId);
  85. auth.TryGetValue("DeviceId", out deviceId);
  86. auth.TryGetValue("Device", out device);
  87. auth.TryGetValue("Client", out client);
  88. auth.TryGetValue("Version", out version);
  89. return new AuthorizationInfo
  90. {
  91. Client = client,
  92. Device = device,
  93. DeviceId = deviceId,
  94. UserId = userId,
  95. Version = version
  96. };
  97. }
  98. /// <summary>
  99. /// Gets the authorization.
  100. /// </summary>
  101. /// <param name="authorizationHeader">The authorization header.</param>
  102. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  103. private static Dictionary<string, string> GetAuthorization(string authorizationHeader)
  104. {
  105. if (authorizationHeader == null) return null;
  106. var parts = authorizationHeader.Split(' ');
  107. // There should be at least to parts
  108. if (parts.Length < 2) return null;
  109. // It has to be a digest request
  110. if (!string.Equals(parts[0], "MediaBrowser", StringComparison.OrdinalIgnoreCase))
  111. {
  112. return null;
  113. }
  114. // Remove uptil the first space
  115. authorizationHeader = authorizationHeader.Substring(authorizationHeader.IndexOf(' '));
  116. parts = authorizationHeader.Split(',');
  117. var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  118. foreach (var item in parts)
  119. {
  120. var param = item.Trim().Split(new[] { '=' }, 2);
  121. result.Add(param[0], param[1].Trim(new[] { '"' }));
  122. }
  123. return result;
  124. }
  125. /// <summary>
  126. /// A new shallow copy of this filter is used on every request.
  127. /// </summary>
  128. /// <returns>IHasRequestFilter.</returns>
  129. public IHasRequestFilter Copy()
  130. {
  131. return this;
  132. }
  133. /// <summary>
  134. /// Order in which Request Filters are executed.
  135. /// &lt;0 Executed before global request filters
  136. /// &gt;0 Executed after global request filters
  137. /// </summary>
  138. /// <value>The priority.</value>
  139. public int Priority
  140. {
  141. get { return 0; }
  142. }
  143. }
  144. public class AuthorizationInfo
  145. {
  146. public string UserId;
  147. public string DeviceId;
  148. public string Device;
  149. public string Client;
  150. public string Version;
  151. }
  152. }