AuthService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. if (authAttribtues.IgnoreLegacyAuth)
  126. {
  127. return true;
  128. }
  129. return false;
  130. }
  131. private bool IsExemptFromRoles(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues, IRequest request, AuthenticationInfo tokenInfo)
  132. {
  133. if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
  134. {
  135. return true;
  136. }
  137. if (authAttribtues.AllowLocal && request.IsLocal)
  138. {
  139. return true;
  140. }
  141. if (authAttribtues.AllowLocalOnly && request.IsLocal)
  142. {
  143. return true;
  144. }
  145. if (string.IsNullOrEmpty(auth.Token))
  146. {
  147. return true;
  148. }
  149. if (tokenInfo != null && tokenInfo.UserId.Equals(Guid.Empty))
  150. {
  151. return true;
  152. }
  153. return false;
  154. }
  155. private static void ValidateRoles(string[] roles, User user)
  156. {
  157. if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase))
  158. {
  159. if (user == null || !user.HasPermission(PermissionKind.IsAdministrator))
  160. {
  161. throw new SecurityException("User does not have admin access.");
  162. }
  163. }
  164. if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase))
  165. {
  166. if (user == null || !user.HasPermission(PermissionKind.EnableContentDeletion))
  167. {
  168. throw new SecurityException("User does not have delete access.");
  169. }
  170. }
  171. if (roles.Contains("download", StringComparer.OrdinalIgnoreCase))
  172. {
  173. if (user == null || !user.HasPermission(PermissionKind.EnableContentDownloading))
  174. {
  175. throw new SecurityException("User does not have download access.");
  176. }
  177. }
  178. }
  179. private static AuthenticationInfo GetTokenInfo(IRequest request)
  180. {
  181. request.Items.TryGetValue("OriginalAuthenticationInfo", out var info);
  182. return info as AuthenticationInfo;
  183. }
  184. private void ValidateSecurityToken(IRequest request, string token)
  185. {
  186. if (string.IsNullOrEmpty(token))
  187. {
  188. throw new AuthenticationException("Access token is required.");
  189. }
  190. var info = GetTokenInfo(request);
  191. if (info == null)
  192. {
  193. throw new AuthenticationException("Access token is invalid or expired.");
  194. }
  195. //if (!string.IsNullOrEmpty(info.UserId))
  196. //{
  197. // var user = _userManager.GetUserById(info.UserId);
  198. // if (user == null || user.Configuration.IsDisabled)
  199. // {
  200. // throw new SecurityException("User account has been disabled.");
  201. // }
  202. //}
  203. }
  204. }
  205. }