AuthorizationContext.cs 6.1 KB

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