AuthService.cs 7.8 KB

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