BaseApiService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections.Generic;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Connectivity;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Server.Implementations.HttpServer;
  6. using ServiceStack.Common.Web;
  7. using ServiceStack.ServiceHost;
  8. using System;
  9. namespace MediaBrowser.Api
  10. {
  11. /// <summary>
  12. /// Class BaseApiService
  13. /// </summary>
  14. [RequestFilter]
  15. public class BaseApiService : BaseRestService
  16. {
  17. }
  18. /// <summary>
  19. /// Class RequestFilterAttribute
  20. /// </summary>
  21. public class RequestFilterAttribute : Attribute, IHasRequestFilter
  22. {
  23. //This property will be resolved by the IoC container
  24. /// <summary>
  25. /// Gets or sets the user manager.
  26. /// </summary>
  27. /// <value>The user manager.</value>
  28. public IUserManager UserManager { get; set; }
  29. /// <summary>
  30. /// Gets or sets the logger.
  31. /// </summary>
  32. /// <value>The logger.</value>
  33. public ILogger Logger { get; set; }
  34. /// <summary>
  35. /// The request filter is executed before the service.
  36. /// </summary>
  37. /// <param name="request">The http request wrapper</param>
  38. /// <param name="response">The http response wrapper</param>
  39. /// <param name="requestDto">The request DTO</param>
  40. public void RequestFilter(IHttpRequest request, IHttpResponse response, object requestDto)
  41. {
  42. //This code is executed before the service
  43. var auth = GetAuthorization(request);
  44. if (auth != null && auth.ContainsKey("UserId"))
  45. {
  46. var user = UserManager.GetUserById(new Guid(auth["UserId"]));
  47. ClientType clientType;
  48. Enum.TryParse(auth["Client"] ?? string.Empty, out clientType);
  49. UserManager.LogUserActivity(user, clientType, auth["DeviceId"], auth["Device"] ?? string.Empty);
  50. }
  51. }
  52. /// <summary>
  53. /// Gets the auth.
  54. /// </summary>
  55. /// <param name="httpReq">The HTTP req.</param>
  56. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  57. public static Dictionary<string, string> GetAuthorization(IHttpRequest httpReq)
  58. {
  59. var auth = httpReq.Headers[HttpHeaders.Authorization];
  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 (!string.Equals(parts[0], "MediaBrowser", StringComparison.OrdinalIgnoreCase))
  66. {
  67. return null;
  68. }
  69. // Remove uptil the first space
  70. auth = auth.Substring(auth.IndexOf(' '));
  71. parts = auth.Split(',');
  72. var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  73. foreach (var item in parts)
  74. {
  75. var param = item.Trim().Split(new[] { '=' }, 2);
  76. result.Add(param[0], param[1].Trim(new[] { '"' }));
  77. }
  78. return result;
  79. }
  80. /// <summary>
  81. /// A new shallow copy of this filter is used on every request.
  82. /// </summary>
  83. /// <returns>IHasRequestFilter.</returns>
  84. public IHasRequestFilter Copy()
  85. {
  86. return this;
  87. }
  88. /// <summary>
  89. /// Order in which Request Filters are executed.
  90. /// &lt;0 Executed before global request filters
  91. /// &gt;0 Executed after global request filters
  92. /// </summary>
  93. /// <value>The priority.</value>
  94. public int Priority
  95. {
  96. get { return 0; }
  97. }
  98. }
  99. }