AuthorizationContext.cs 4.9 KB

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