AuthorizationContext.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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-Emby-Token"];
  56. if (string.IsNullOrWhiteSpace(token))
  57. {
  58. token = httpReq.Headers["X-MediaBrowser-Token"];
  59. }
  60. if (string.IsNullOrWhiteSpace(token))
  61. {
  62. token = httpReq.QueryString["api_key"];
  63. }
  64. var info = new AuthorizationInfo
  65. {
  66. Client = client,
  67. Device = device,
  68. DeviceId = deviceId,
  69. UserId = userId,
  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.FirstOrDefault();
  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. }
  97. else
  98. {
  99. var user = _connectManager.GetUserFromExchangeToken(token);
  100. if (user != null)
  101. {
  102. info.UserId = user.Id.ToString("N");
  103. }
  104. }
  105. httpReq.Items["OriginalAuthenticationInfo"] = tokenInfo;
  106. }
  107. httpReq.Items["AuthorizationInfo"] = info;
  108. return info;
  109. }
  110. /// <summary>
  111. /// Gets the auth.
  112. /// </summary>
  113. /// <param name="httpReq">The HTTP req.</param>
  114. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  115. private Dictionary<string, string> GetAuthorizationDictionary(IServiceRequest httpReq)
  116. {
  117. var auth = httpReq.Headers["X-Emby-Authorization"];
  118. if (string.IsNullOrWhiteSpace(auth))
  119. {
  120. auth = httpReq.Headers["Authorization"];
  121. }
  122. return GetAuthorization(auth);
  123. }
  124. /// <summary>
  125. /// Gets the authorization.
  126. /// </summary>
  127. /// <param name="authorizationHeader">The authorization header.</param>
  128. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  129. private Dictionary<string, string> GetAuthorization(string authorizationHeader)
  130. {
  131. if (authorizationHeader == null) return null;
  132. var parts = authorizationHeader.Split(new[] { ' ' }, 2);
  133. // There should be at least to parts
  134. if (parts.Length != 2) return null;
  135. // It has to be a digest request
  136. if (!string.Equals(parts[0], "MediaBrowser", StringComparison.OrdinalIgnoreCase))
  137. {
  138. return null;
  139. }
  140. // Remove uptil the first space
  141. authorizationHeader = parts[1];
  142. parts = authorizationHeader.Split(',');
  143. var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  144. foreach (var item in parts)
  145. {
  146. var param = item.Trim().Split(new[] { '=' }, 2);
  147. if (param.Length == 2)
  148. {
  149. var value = NormalizeValue (param[1].Trim(new[] { '"' }));
  150. result.Add(param[0], value);
  151. }
  152. }
  153. return result;
  154. }
  155. private string NormalizeValue(string value)
  156. {
  157. if (string.IsNullOrWhiteSpace (value))
  158. {
  159. return value;
  160. }
  161. return System.Net.WebUtility.HtmlEncode(value);
  162. }
  163. }
  164. }