AuthorizationContext.cs 7.2 KB

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