2
0

AuthService.cs 8.2 KB

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