AuthorizationContext.cs 7.3 KB

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