AuthService.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Connect;
  3. using MediaBrowser.Controller.Devices;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Controller.Security;
  8. using MediaBrowser.Controller.Session;
  9. using System;
  10. using System.Linq;
  11. using MediaBrowser.Model.Services;
  12. using MediaBrowser.Common.Net;
  13. namespace Emby.Server.Implementations.HttpServer.Security
  14. {
  15. public class AuthService : IAuthService
  16. {
  17. private readonly IServerConfigurationManager _config;
  18. public AuthService(IUserManager userManager, IAuthorizationContext authorizationContext, IServerConfigurationManager config, IConnectManager connectManager, ISessionManager sessionManager, INetworkManager networkManager)
  19. {
  20. AuthorizationContext = authorizationContext;
  21. _config = config;
  22. SessionManager = sessionManager;
  23. ConnectManager = connectManager;
  24. UserManager = userManager;
  25. NetworkManager = networkManager;
  26. }
  27. public IUserManager UserManager { get; private set; }
  28. public IAuthorizationContext AuthorizationContext { get; private set; }
  29. public IConnectManager ConnectManager { get; private set; }
  30. public ISessionManager SessionManager { get; private set; }
  31. public INetworkManager NetworkManager { get; private set; }
  32. /// <summary>
  33. /// Redirect the client to a specific URL if authentication failed.
  34. /// If this property is null, simply `401 Unauthorized` is returned.
  35. /// </summary>
  36. public string HtmlRedirect { get; set; }
  37. public void Authenticate(IRequest request, IAuthenticationAttributes authAttribtues)
  38. {
  39. ValidateUser(request, authAttribtues);
  40. }
  41. private void ValidateUser(IRequest request, IAuthenticationAttributes authAttribtues)
  42. {
  43. // This code is executed before the service
  44. var auth = AuthorizationContext.GetAuthorizationInfo(request);
  45. if (!IsExemptFromAuthenticationToken(auth, authAttribtues, request))
  46. {
  47. ValidateSecurityToken(request, auth.Token);
  48. }
  49. if (authAttribtues.AllowLocalOnly && !request.IsLocal)
  50. {
  51. throw new SecurityException("Operation not found.");
  52. }
  53. var user = auth.User;
  54. if (user == null & !auth.UserId.Equals(Guid.Empty))
  55. {
  56. throw new SecurityException("User with Id " + auth.UserId + " not found");
  57. }
  58. if (user != null)
  59. {
  60. ValidateUserAccess(user, request, authAttribtues, auth);
  61. }
  62. var info = GetTokenInfo(request);
  63. if (!IsExemptFromRoles(auth, authAttribtues, request, info))
  64. {
  65. var roles = authAttribtues.GetRoles();
  66. ValidateRoles(roles, user);
  67. }
  68. if (!string.IsNullOrEmpty(auth.DeviceId) &&
  69. !string.IsNullOrEmpty(auth.Client) &&
  70. !string.IsNullOrEmpty(auth.Device))
  71. {
  72. SessionManager.LogSessionActivity(auth.Client,
  73. auth.Version,
  74. auth.DeviceId,
  75. auth.Device,
  76. request.RemoteIp,
  77. user);
  78. }
  79. }
  80. private void ValidateUserAccess(User user, IRequest request,
  81. IAuthenticationAttributes authAttribtues,
  82. AuthorizationInfo auth)
  83. {
  84. if (user.Policy.IsDisabled)
  85. {
  86. throw new SecurityException("User account has been disabled.")
  87. {
  88. SecurityExceptionType = SecurityExceptionType.Unauthenticated
  89. };
  90. }
  91. if (!user.Policy.EnableRemoteAccess && !NetworkManager.IsInLocalNetwork(request.RemoteIp))
  92. {
  93. throw new SecurityException("User account has been disabled.")
  94. {
  95. SecurityExceptionType = SecurityExceptionType.Unauthenticated
  96. };
  97. }
  98. if (!user.Policy.IsAdministrator &&
  99. !authAttribtues.EscapeParentalControl &&
  100. !user.IsParentalScheduleAllowed())
  101. {
  102. request.Response.AddHeader("X-Application-Error-Code", "ParentalControl");
  103. throw new SecurityException("This user account is not allowed access at this time.")
  104. {
  105. SecurityExceptionType = SecurityExceptionType.ParentalControl
  106. };
  107. }
  108. }
  109. private bool IsExemptFromAuthenticationToken(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues, IRequest request)
  110. {
  111. if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
  112. {
  113. return true;
  114. }
  115. if (authAttribtues.AllowLocal && request.IsLocal)
  116. {
  117. return true;
  118. }
  119. if (authAttribtues.AllowLocalOnly && request.IsLocal)
  120. {
  121. return true;
  122. }
  123. return false;
  124. }
  125. private bool IsExemptFromRoles(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues, IRequest request, AuthenticationInfo tokenInfo)
  126. {
  127. if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
  128. {
  129. return true;
  130. }
  131. if (authAttribtues.AllowLocal && request.IsLocal)
  132. {
  133. return true;
  134. }
  135. if (authAttribtues.AllowLocalOnly && request.IsLocal)
  136. {
  137. return true;
  138. }
  139. if (string.IsNullOrEmpty(auth.Token))
  140. {
  141. return true;
  142. }
  143. if (tokenInfo != null && tokenInfo.UserId.Equals(Guid.Empty))
  144. {
  145. return true;
  146. }
  147. return false;
  148. }
  149. private void ValidateRoles(string[] roles, User user)
  150. {
  151. if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase))
  152. {
  153. if (user == null || !user.Policy.IsAdministrator)
  154. {
  155. throw new SecurityException("User does not have admin access.")
  156. {
  157. SecurityExceptionType = SecurityExceptionType.Unauthenticated
  158. };
  159. }
  160. }
  161. if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase))
  162. {
  163. if (user == null || !user.Policy.EnableContentDeletion)
  164. {
  165. throw new SecurityException("User does not have delete access.")
  166. {
  167. SecurityExceptionType = SecurityExceptionType.Unauthenticated
  168. };
  169. }
  170. }
  171. if (roles.Contains("download", StringComparer.OrdinalIgnoreCase))
  172. {
  173. if (user == null || !user.Policy.EnableContentDownloading)
  174. {
  175. throw new SecurityException("User does not have download access.")
  176. {
  177. SecurityExceptionType = SecurityExceptionType.Unauthenticated
  178. };
  179. }
  180. }
  181. }
  182. private AuthenticationInfo GetTokenInfo(IRequest request)
  183. {
  184. object info;
  185. request.Items.TryGetValue("OriginalAuthenticationInfo", out info);
  186. return info as AuthenticationInfo;
  187. }
  188. private void ValidateSecurityToken(IRequest request, string token)
  189. {
  190. if (string.IsNullOrEmpty(token))
  191. {
  192. throw new SecurityException("Access token is required.");
  193. }
  194. var info = GetTokenInfo(request);
  195. if (info == null)
  196. {
  197. throw new SecurityException("Access token is invalid or expired.");
  198. }
  199. //if (!string.IsNullOrEmpty(info.UserId))
  200. //{
  201. // var user = _userManager.GetUserById(info.UserId);
  202. // if (user == null || user.Configuration.IsDisabled)
  203. // {
  204. // throw new SecurityException("User account has been disabled.");
  205. // }
  206. //}
  207. }
  208. }
  209. }