AuthService.cs 7.8 KB

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