AuthorizationContext.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. var req = new ServiceRequest((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 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. // TODO: Remove these checks for IsNullOrWhiteSpace
  85. if (string.IsNullOrWhiteSpace(info.Client))
  86. {
  87. info.Client = tokenInfo.AppName;
  88. }
  89. if (string.IsNullOrWhiteSpace(info.Device))
  90. {
  91. info.Device = tokenInfo.DeviceName;
  92. }
  93. if (string.IsNullOrWhiteSpace(info.DeviceId))
  94. {
  95. info.DeviceId = tokenInfo.DeviceId;
  96. }
  97. if (string.IsNullOrWhiteSpace(info.Version))
  98. {
  99. info.Version = tokenInfo.AppVersion;
  100. }
  101. }
  102. else
  103. {
  104. var user = _connectManager.GetUserFromExchangeToken(token);
  105. if (user != null)
  106. {
  107. info.UserId = user.Id.ToString("N");
  108. }
  109. }
  110. httpReq.Items["OriginalAuthenticationInfo"] = tokenInfo;
  111. }
  112. httpReq.Items["AuthorizationInfo"] = info;
  113. return info;
  114. }
  115. /// <summary>
  116. /// Gets the auth.
  117. /// </summary>
  118. /// <param name="httpReq">The HTTP req.</param>
  119. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  120. private Dictionary<string, string> GetAuthorizationDictionary(IServiceRequest httpReq)
  121. {
  122. var auth = httpReq.Headers["X-Emby-Authorization"];
  123. if (string.IsNullOrWhiteSpace(auth))
  124. {
  125. auth = httpReq.Headers["Authorization"];
  126. }
  127. return GetAuthorization(auth);
  128. }
  129. /// <summary>
  130. /// Gets the authorization.
  131. /// </summary>
  132. /// <param name="authorizationHeader">The authorization header.</param>
  133. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  134. private Dictionary<string, string> GetAuthorization(string authorizationHeader)
  135. {
  136. if (authorizationHeader == null) return null;
  137. var parts = authorizationHeader.Split(new[] { ' ' }, 2);
  138. // There should be at least to parts
  139. if (parts.Length != 2) return null;
  140. var acceptedNames = new[] { "MediaBrowser", "Emby" };
  141. // It has to be a digest request
  142. if (!acceptedNames.Contains(parts[0] ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  143. {
  144. return null;
  145. }
  146. // Remove uptil the first space
  147. authorizationHeader = parts[1];
  148. parts = authorizationHeader.Split(',');
  149. var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  150. foreach (var item in parts)
  151. {
  152. var param = item.Trim().Split(new[] { '=' }, 2);
  153. if (param.Length == 2)
  154. {
  155. var value = NormalizeValue(param[1].Trim(new[] { '"' }));
  156. result.Add(param[0], value);
  157. }
  158. }
  159. return result;
  160. }
  161. private string NormalizeValue(string value)
  162. {
  163. if (string.IsNullOrWhiteSpace(value))
  164. {
  165. return value;
  166. }
  167. return System.Net.WebUtility.HtmlEncode(value);
  168. }
  169. }
  170. }