AuthorizationContext.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. namespace Emby.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. return GetAuthorizationInfo((IRequest)requestContext);
  22. }
  23. public AuthorizationInfo GetAuthorizationInfo(IRequest requestContext)
  24. {
  25. object cached;
  26. if (requestContext.Items.TryGetValue("AuthorizationInfo", out cached))
  27. {
  28. return (AuthorizationInfo)cached;
  29. }
  30. return GetAuthorization(requestContext);
  31. }
  32. /// <summary>
  33. /// Gets the authorization.
  34. /// </summary>
  35. /// <param name="httpReq">The HTTP req.</param>
  36. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  37. private AuthorizationInfo GetAuthorization(IRequest httpReq)
  38. {
  39. var auth = GetAuthorizationDictionary(httpReq);
  40. string deviceId = null;
  41. string device = null;
  42. string client = null;
  43. string version = null;
  44. string token = null;
  45. if (auth != null)
  46. {
  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. auth.TryGetValue("Token", out token);
  52. }
  53. if (string.IsNullOrWhiteSpace(token))
  54. {
  55. token = httpReq.Headers["X-Emby-Token"];
  56. }
  57. if (string.IsNullOrWhiteSpace(token))
  58. {
  59. token = httpReq.Headers["X-MediaBrowser-Token"];
  60. }
  61. if (string.IsNullOrWhiteSpace(token))
  62. {
  63. token = httpReq.QueryString["api_key"];
  64. }
  65. var info = new AuthorizationInfo
  66. {
  67. Client = client,
  68. Device = device,
  69. DeviceId = deviceId,
  70. Version = version,
  71. Token = token
  72. };
  73. if (!string.IsNullOrWhiteSpace(token))
  74. {
  75. var result = _authRepo.Get(new AuthenticationInfoQuery
  76. {
  77. AccessToken = token
  78. });
  79. var tokenInfo = result.Items.Length > 0 ? result.Items[0] : null;
  80. if (tokenInfo != null)
  81. {
  82. info.UserId = tokenInfo.UserId;
  83. // TODO: Remove these checks for IsNullOrWhiteSpace
  84. if (string.IsNullOrWhiteSpace(info.Client))
  85. {
  86. info.Client = tokenInfo.AppName;
  87. }
  88. if (string.IsNullOrWhiteSpace(info.Device))
  89. {
  90. info.Device = tokenInfo.DeviceName;
  91. }
  92. if (string.IsNullOrWhiteSpace(info.DeviceId))
  93. {
  94. info.DeviceId = tokenInfo.DeviceId;
  95. }
  96. if (string.IsNullOrWhiteSpace(info.Version))
  97. {
  98. info.Version = tokenInfo.AppVersion;
  99. }
  100. }
  101. else
  102. {
  103. var user = _connectManager.GetUserFromExchangeToken(token);
  104. if (user != null)
  105. {
  106. info.UserId = user.Id.ToString("N");
  107. }
  108. }
  109. httpReq.Items["OriginalAuthenticationInfo"] = tokenInfo;
  110. }
  111. httpReq.Items["AuthorizationInfo"] = info;
  112. return info;
  113. }
  114. /// <summary>
  115. /// Gets the auth.
  116. /// </summary>
  117. /// <param name="httpReq">The HTTP req.</param>
  118. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  119. private Dictionary<string, string> GetAuthorizationDictionary(IRequest httpReq)
  120. {
  121. var auth = httpReq.Headers["X-Emby-Authorization"];
  122. if (string.IsNullOrWhiteSpace(auth))
  123. {
  124. auth = httpReq.Headers["Authorization"];
  125. }
  126. return GetAuthorization(auth);
  127. }
  128. /// <summary>
  129. /// Gets the authorization.
  130. /// </summary>
  131. /// <param name="authorizationHeader">The authorization header.</param>
  132. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  133. private Dictionary<string, string> GetAuthorization(string authorizationHeader)
  134. {
  135. if (authorizationHeader == null) return null;
  136. var parts = authorizationHeader.Split(new[] { ' ' }, 2);
  137. // There should be at least to parts
  138. if (parts.Length != 2) return null;
  139. var acceptedNames = new[] { "MediaBrowser", "Emby" };
  140. // It has to be a digest request
  141. if (!acceptedNames.Contains(parts[0] ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  142. {
  143. return null;
  144. }
  145. // Remove uptil the first space
  146. authorizationHeader = parts[1];
  147. parts = authorizationHeader.Split(',');
  148. var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  149. foreach (var item in parts)
  150. {
  151. var param = item.Trim().Split(new[] { '=' }, 2);
  152. if (param.Length == 2)
  153. {
  154. var value = NormalizeValue(param[1].Trim(new[] { '"' }));
  155. result.Add(param[0], value);
  156. }
  157. }
  158. return result;
  159. }
  160. private string NormalizeValue(string value)
  161. {
  162. if (string.IsNullOrWhiteSpace(value))
  163. {
  164. return value;
  165. }
  166. return System.Net.WebUtility.HtmlEncode(value);
  167. }
  168. }
  169. }