AuthService.cs 7.8 KB

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