AuthorizationContext.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. return new AuthorizationInfo
  40. {
  41. Client = client,
  42. Device = device,
  43. DeviceId = deviceId,
  44. UserId = userId,
  45. Version = version,
  46. Token = token
  47. };
  48. }
  49. /// <summary>
  50. /// Gets the auth.
  51. /// </summary>
  52. /// <param name="httpReq">The HTTP req.</param>
  53. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  54. private Dictionary<string, string> GetAuthorizationDictionary(IRequest httpReq)
  55. {
  56. var auth = httpReq.Headers["Authorization"];
  57. return GetAuthorization(auth);
  58. }
  59. /// <summary>
  60. /// Gets the authorization.
  61. /// </summary>
  62. /// <param name="authorizationHeader">The authorization header.</param>
  63. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  64. private Dictionary<string, string> GetAuthorization(string authorizationHeader)
  65. {
  66. if (authorizationHeader == null) return null;
  67. var parts = authorizationHeader.Split(new[] { ' ' }, 2);
  68. // There should be at least to parts
  69. if (parts.Length != 2) return null;
  70. // It has to be a digest request
  71. if (!string.Equals(parts[0], "MediaBrowser", StringComparison.OrdinalIgnoreCase))
  72. {
  73. return null;
  74. }
  75. // Remove uptil the first space
  76. authorizationHeader = parts[1];
  77. parts = authorizationHeader.Split(',');
  78. var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  79. foreach (var item in parts)
  80. {
  81. var param = item.Trim().Split(new[] { '=' }, 2);
  82. if (param.Length == 2)
  83. {
  84. result.Add(param[0], param[1].Trim(new[] { '"' }));
  85. }
  86. }
  87. return result;
  88. }
  89. }
  90. }