AuthService.cs 7.8 KB

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