2
0

AuthService.cs 7.9 KB

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