AuthService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 authAttributes)
  39. {
  40. ValidateUser(request, authAttributes);
  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. public AuthorizationInfo Authenticate(HttpRequest request)
  49. {
  50. var auth = _authorizationContext.GetAuthorizationInfo(request);
  51. if (auth?.User == null)
  52. {
  53. return null;
  54. }
  55. if (auth.User.HasPermission(PermissionKind.IsDisabled))
  56. {
  57. throw new SecurityException("User account has been disabled.");
  58. }
  59. return auth;
  60. }
  61. private User ValidateUser(IRequest request, IAuthenticationAttributes authAttributes)
  62. {
  63. // This code is executed before the service
  64. var auth = _authorizationContext.GetAuthorizationInfo(request);
  65. if (!IsExemptFromAuthenticationToken(authAttributes, request))
  66. {
  67. ValidateSecurityToken(request, auth.Token);
  68. }
  69. if (authAttributes.AllowLocalOnly && !request.IsLocal)
  70. {
  71. throw new SecurityException("Operation not found.");
  72. }
  73. var user = auth.User;
  74. if (user == null && auth.UserId != Guid.Empty)
  75. {
  76. throw new AuthenticationException("User with Id " + auth.UserId + " not found");
  77. }
  78. if (user != null)
  79. {
  80. ValidateUserAccess(user, request, authAttributes);
  81. }
  82. var info = GetTokenInfo(request);
  83. if (!IsExemptFromRoles(auth, authAttributes, request, info))
  84. {
  85. var roles = authAttributes.GetRoles();
  86. ValidateRoles(roles, user);
  87. }
  88. if (!string.IsNullOrEmpty(auth.DeviceId) &&
  89. !string.IsNullOrEmpty(auth.Client) &&
  90. !string.IsNullOrEmpty(auth.Device))
  91. {
  92. _sessionManager.LogSessionActivity(
  93. auth.Client,
  94. auth.Version,
  95. auth.DeviceId,
  96. auth.Device,
  97. request.RemoteIp,
  98. user);
  99. }
  100. return user;
  101. }
  102. private void ValidateUserAccess(
  103. User user,
  104. IRequest request,
  105. IAuthenticationAttributes authAttributes)
  106. {
  107. if (user.HasPermission(PermissionKind.IsDisabled))
  108. {
  109. throw new SecurityException("User account has been disabled.");
  110. }
  111. if (!user.HasPermission(PermissionKind.EnableRemoteAccess) && !_networkManager.IsInLocalNetwork(request.RemoteIp))
  112. {
  113. throw new SecurityException("User account has been disabled.");
  114. }
  115. if (!user.HasPermission(PermissionKind.IsAdministrator)
  116. && !authAttributes.EscapeParentalControl
  117. && !user.IsParentalScheduleAllowed())
  118. {
  119. request.Response.Headers.Add("X-Application-Error-Code", "ParentalControl");
  120. throw new SecurityException("This user account is not allowed access at this time.");
  121. }
  122. }
  123. private bool IsExemptFromAuthenticationToken(IAuthenticationAttributes authAttribtues, IRequest request)
  124. {
  125. if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
  126. {
  127. return true;
  128. }
  129. if (authAttribtues.AllowLocal && request.IsLocal)
  130. {
  131. return true;
  132. }
  133. if (authAttribtues.AllowLocalOnly && request.IsLocal)
  134. {
  135. return true;
  136. }
  137. if (authAttribtues.IgnoreLegacyAuth)
  138. {
  139. return true;
  140. }
  141. return false;
  142. }
  143. private bool IsExemptFromRoles(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues, IRequest request, AuthenticationInfo tokenInfo)
  144. {
  145. if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
  146. {
  147. return true;
  148. }
  149. if (authAttribtues.AllowLocal && request.IsLocal)
  150. {
  151. return true;
  152. }
  153. if (authAttribtues.AllowLocalOnly && request.IsLocal)
  154. {
  155. return true;
  156. }
  157. if (string.IsNullOrEmpty(auth.Token))
  158. {
  159. return true;
  160. }
  161. if (tokenInfo != null && tokenInfo.UserId.Equals(Guid.Empty))
  162. {
  163. return true;
  164. }
  165. return false;
  166. }
  167. private static void ValidateRoles(string[] roles, User user)
  168. {
  169. if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase))
  170. {
  171. if (user == null || !user.HasPermission(PermissionKind.IsAdministrator))
  172. {
  173. throw new SecurityException("User does not have admin access.");
  174. }
  175. }
  176. if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase))
  177. {
  178. if (user == null || !user.HasPermission(PermissionKind.EnableContentDeletion))
  179. {
  180. throw new SecurityException("User does not have delete access.");
  181. }
  182. }
  183. if (roles.Contains("download", StringComparer.OrdinalIgnoreCase))
  184. {
  185. if (user == null || !user.HasPermission(PermissionKind.EnableContentDownloading))
  186. {
  187. throw new SecurityException("User does not have download access.");
  188. }
  189. }
  190. }
  191. private static AuthenticationInfo GetTokenInfo(IRequest request)
  192. {
  193. request.Items.TryGetValue("OriginalAuthenticationInfo", out var info);
  194. return info as AuthenticationInfo;
  195. }
  196. private void ValidateSecurityToken(IRequest request, string token)
  197. {
  198. if (string.IsNullOrEmpty(token))
  199. {
  200. throw new AuthenticationException("Access token is required.");
  201. }
  202. var info = GetTokenInfo(request);
  203. if (info == null)
  204. {
  205. throw new AuthenticationException("Access token is invalid or expired.");
  206. }
  207. }
  208. }
  209. }