AuthorizationRequestFilterAttribute.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. using System.Net.Http.Headers;
  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(IRequest request, IResponse response, object requestDto)
  32. {
  33. //This code is executed before the service
  34. var auth = GetAuthorizationDictionary(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.LogSessionActivity(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. 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. }