AuthService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 authAttribtues)
  35. {
  36. ValidateUser(request, authAttribtues);
  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 authAttribtues)
  58. {
  59. // This code is executed before the service
  60. var auth = _authorizationContext.GetAuthorizationInfo(request);
  61. if (!IsExemptFromAuthenticationToken(authAttribtues, request))
  62. {
  63. ValidateSecurityToken(request, auth.Token);
  64. }
  65. if (authAttribtues.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, authAttribtues, auth);
  77. }
  78. var info = GetTokenInfo(request);
  79. if (!IsExemptFromRoles(auth, authAttribtues, request, info))
  80. {
  81. var roles = authAttribtues.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. AuthorizationInfo auth)
  103. {
  104. if (user.HasPermission(PermissionKind.IsDisabled))
  105. {
  106. throw new SecurityException("User account has been disabled.");
  107. }
  108. if (!user.HasPermission(PermissionKind.EnableRemoteAccess) && !_networkManager.IsInLocalNetwork(request.RemoteIp))
  109. {
  110. throw new SecurityException("User account has been disabled.");
  111. }
  112. if (!user.HasPermission(PermissionKind.IsAdministrator)
  113. && !authAttributes.EscapeParentalControl
  114. && !user.IsParentalScheduleAllowed())
  115. {
  116. request.Response.Headers.Add("X-Application-Error-Code", "ParentalControl");
  117. throw new SecurityException("This user account is not allowed access at this time.");
  118. }
  119. }
  120. private bool IsExemptFromAuthenticationToken(IAuthenticationAttributes authAttribtues, IRequest request)
  121. {
  122. if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
  123. {
  124. return true;
  125. }
  126. if (authAttribtues.AllowLocal && request.IsLocal)
  127. {
  128. return true;
  129. }
  130. if (authAttribtues.AllowLocalOnly && request.IsLocal)
  131. {
  132. return true;
  133. }
  134. return false;
  135. }
  136. private bool IsExemptFromRoles(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues, IRequest request, AuthenticationInfo tokenInfo)
  137. {
  138. if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
  139. {
  140. return true;
  141. }
  142. if (authAttribtues.AllowLocal && request.IsLocal)
  143. {
  144. return true;
  145. }
  146. if (authAttribtues.AllowLocalOnly && request.IsLocal)
  147. {
  148. return true;
  149. }
  150. if (string.IsNullOrEmpty(auth.Token))
  151. {
  152. return true;
  153. }
  154. if (tokenInfo != null && tokenInfo.UserId.Equals(Guid.Empty))
  155. {
  156. return true;
  157. }
  158. return false;
  159. }
  160. private static void ValidateRoles(string[] roles, User user)
  161. {
  162. if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase))
  163. {
  164. if (user == null || !user.HasPermission(PermissionKind.IsAdministrator))
  165. {
  166. throw new SecurityException("User does not have admin access.");
  167. }
  168. }
  169. if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase))
  170. {
  171. if (user == null || !user.HasPermission(PermissionKind.EnableContentDeletion))
  172. {
  173. throw new SecurityException("User does not have delete access.");
  174. }
  175. }
  176. if (roles.Contains("download", StringComparer.OrdinalIgnoreCase))
  177. {
  178. if (user == null || !user.HasPermission(PermissionKind.EnableContentDownloading))
  179. {
  180. throw new SecurityException("User does not have download access.");
  181. }
  182. }
  183. }
  184. private static AuthenticationInfo GetTokenInfo(IRequest request)
  185. {
  186. request.Items.TryGetValue("OriginalAuthenticationInfo", out var info);
  187. return info as AuthenticationInfo;
  188. }
  189. private void ValidateSecurityToken(IRequest request, string token)
  190. {
  191. if (string.IsNullOrEmpty(token))
  192. {
  193. throw new AuthenticationException("Access token is required.");
  194. }
  195. var info = GetTokenInfo(request);
  196. if (info == null)
  197. {
  198. throw new AuthenticationException("Access token is invalid or expired.");
  199. }
  200. // if (!string.IsNullOrEmpty(info.UserId))
  201. //{
  202. // var user = _userManager.GetUserById(info.UserId);
  203. // if (user == null || user.Configuration.IsDisabled)
  204. // {
  205. // throw new SecurityException("User account has been disabled.");
  206. // }
  207. //}
  208. }
  209. }
  210. }