2
0

AuthService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. 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 static 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. }
  158. if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase))
  159. {
  160. if (user == null || !user.Policy.EnableContentDeletion)
  161. {
  162. throw new SecurityException("User does not have delete access.");
  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. }
  172. }
  173. private static AuthenticationInfo GetTokenInfo(IRequest request)
  174. {
  175. request.Items.TryGetValue("OriginalAuthenticationInfo", out var info);
  176. return info as AuthenticationInfo;
  177. }
  178. private void ValidateSecurityToken(IRequest request, string token)
  179. {
  180. if (string.IsNullOrEmpty(token))
  181. {
  182. throw new AuthenticationException("Access token is required.");
  183. }
  184. var info = GetTokenInfo(request);
  185. if (info == null)
  186. {
  187. throw new AuthenticationException("Access token is invalid or expired.");
  188. }
  189. //if (!string.IsNullOrEmpty(info.UserId))
  190. //{
  191. // var user = _userManager.GetUserById(info.UserId);
  192. // if (user == null || user.Configuration.IsDisabled)
  193. // {
  194. // throw new SecurityException("User account has been disabled.");
  195. // }
  196. //}
  197. }
  198. }
  199. }