AuthorizationRequestFilterAttribute.cs 5.8 KB

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