AuthService.cs 8.1 KB

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