2
0

AuthService.cs 8.3 KB

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