AuthorizationContext.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using MediaBrowser.Controller.Net;
  2. using MediaBrowser.Controller.Security;
  3. using ServiceStack.Web;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace MediaBrowser.Server.Implementations.HttpServer.Security
  8. {
  9. public class AuthorizationContext : IAuthorizationContext
  10. {
  11. private readonly IAuthenticationRepository _authRepo;
  12. public AuthorizationContext(IAuthenticationRepository authRepo)
  13. {
  14. _authRepo = authRepo;
  15. }
  16. public AuthorizationInfo GetAuthorizationInfo(object requestContext)
  17. {
  18. var req = new ServiceStackServiceRequest((IRequest)requestContext);
  19. return GetAuthorizationInfo(req);
  20. }
  21. public AuthorizationInfo GetAuthorizationInfo(IServiceRequest requestContext)
  22. {
  23. object cached;
  24. if (requestContext.Items.TryGetValue("AuthorizationInfo", out cached))
  25. {
  26. return (AuthorizationInfo)cached;
  27. }
  28. return GetAuthorization(requestContext);
  29. }
  30. /// <summary>
  31. /// Gets the authorization.
  32. /// </summary>
  33. /// <param name="httpReq">The HTTP req.</param>
  34. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  35. private AuthorizationInfo GetAuthorization(IServiceRequest httpReq)
  36. {
  37. var auth = GetAuthorizationDictionary(httpReq);
  38. string userId = null;
  39. string deviceId = null;
  40. string device = null;
  41. string client = null;
  42. string version = null;
  43. if (auth != null)
  44. {
  45. // TODO: Remove this
  46. auth.TryGetValue("UserId", out userId);
  47. auth.TryGetValue("DeviceId", out deviceId);
  48. auth.TryGetValue("Device", out device);
  49. auth.TryGetValue("Client", out client);
  50. auth.TryGetValue("Version", out version);
  51. }
  52. var token = httpReq.Headers["X-MediaBrowser-Token"];
  53. if (string.IsNullOrWhiteSpace(token))
  54. {
  55. token = httpReq.QueryString["api_key"];
  56. }
  57. // Hack until iOS is updated
  58. // TODO: Remove
  59. if (string.IsNullOrWhiteSpace(client))
  60. {
  61. var userAgent = httpReq.Headers["User-Agent"] ?? string.Empty;
  62. if (userAgent.IndexOf("mediabrowserios", StringComparison.OrdinalIgnoreCase) != -1 ||
  63. userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
  64. userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1)
  65. {
  66. client = "iOS";
  67. }
  68. else if (userAgent.IndexOf("crKey", StringComparison.OrdinalIgnoreCase) != -1)
  69. {
  70. client = "Chromecast";
  71. }
  72. }
  73. // Hack until iOS is updated
  74. // TODO: Remove
  75. if (string.IsNullOrWhiteSpace(device))
  76. {
  77. var userAgent = httpReq.Headers["User-Agent"] ?? string.Empty;
  78. if (userAgent.IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) != -1)
  79. {
  80. device = "iPhone";
  81. }
  82. else if (userAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) != -1)
  83. {
  84. device = "iPad";
  85. }
  86. else if (userAgent.IndexOf("crKey", StringComparison.OrdinalIgnoreCase) != -1)
  87. {
  88. device = "Chromecast";
  89. }
  90. }
  91. var info = new AuthorizationInfo
  92. {
  93. Client = client,
  94. Device = device,
  95. DeviceId = deviceId,
  96. UserId = userId,
  97. Version = version,
  98. Token = token
  99. };
  100. if (!string.IsNullOrWhiteSpace(token))
  101. {
  102. var result = _authRepo.Get(new AuthenticationInfoQuery
  103. {
  104. AccessToken = token
  105. });
  106. var tokenInfo = result.Items.FirstOrDefault();
  107. if (tokenInfo != null)
  108. {
  109. info.UserId = tokenInfo.UserId;
  110. // TODO: Remove these checks for IsNullOrWhiteSpace
  111. if (string.IsNullOrWhiteSpace(info.Client))
  112. {
  113. info.Client = tokenInfo.AppName;
  114. }
  115. if (string.IsNullOrWhiteSpace(info.Device))
  116. {
  117. info.Device = tokenInfo.DeviceName;
  118. }
  119. if (string.IsNullOrWhiteSpace(info.DeviceId))
  120. {
  121. info.DeviceId = tokenInfo.DeviceId;
  122. }
  123. }
  124. httpReq.Items["OriginalAuthenticationInfo"] = tokenInfo;
  125. }
  126. httpReq.Items["AuthorizationInfo"] = info;
  127. return info;
  128. }
  129. /// <summary>
  130. /// Gets the auth.
  131. /// </summary>
  132. /// <param name="httpReq">The HTTP req.</param>
  133. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  134. private Dictionary<string, string> GetAuthorizationDictionary(IServiceRequest httpReq)
  135. {
  136. var auth = httpReq.Headers["Authorization"];
  137. return GetAuthorization(auth);
  138. }
  139. /// <summary>
  140. /// Gets the authorization.
  141. /// </summary>
  142. /// <param name="authorizationHeader">The authorization header.</param>
  143. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  144. private Dictionary<string, string> GetAuthorization(string authorizationHeader)
  145. {
  146. if (authorizationHeader == null) return null;
  147. var parts = authorizationHeader.Split(new[] { ' ' }, 2);
  148. // There should be at least to parts
  149. if (parts.Length != 2) return null;
  150. // It has to be a digest request
  151. if (!string.Equals(parts[0], "MediaBrowser", StringComparison.OrdinalIgnoreCase))
  152. {
  153. return null;
  154. }
  155. // Remove uptil the first space
  156. authorizationHeader = parts[1];
  157. parts = authorizationHeader.Split(',');
  158. var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  159. foreach (var item in parts)
  160. {
  161. var param = item.Trim().Split(new[] { '=' }, 2);
  162. if (param.Length == 2)
  163. {
  164. result.Add(param[0], param[1].Trim(new[] { '"' }));
  165. }
  166. }
  167. return result;
  168. }
  169. }
  170. }