jellyfin/Emby.Server.Implementations/HttpServer/Security/AuthService.cs

240 lines
8.1 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
2014-11-15 03:31:03 +01:00
using MediaBrowser.Controller.Entities;
2014-07-02 07:16:59 +02:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
2014-11-15 03:31:03 +01:00
using MediaBrowser.Controller.Security;
2014-11-15 16:51:49 +01:00
using MediaBrowser.Controller.Session;
2017-09-03 20:38:26 +02:00
using MediaBrowser.Model.Services;
2016-11-04 02:18:51 +01:00
namespace Emby.Server.Implementations.HttpServer.Security
{
public class AuthService : IAuthService
{
2014-07-08 03:41:03 +02:00
private readonly IServerConfigurationManager _config;
2018-12-28 00:27:57 +01:00
public AuthService(IUserManager userManager, IAuthorizationContext authorizationContext, IServerConfigurationManager config, ISessionManager sessionManager, INetworkManager networkManager)
2014-07-02 07:16:59 +02:00
{
AuthorizationContext = authorizationContext;
2014-07-08 03:41:03 +02:00
_config = config;
2014-11-15 16:51:49 +01:00
SessionManager = sessionManager;
2014-07-02 07:16:59 +02:00
UserManager = userManager;
2018-09-12 19:26:21 +02:00
NetworkManager = networkManager;
2014-07-02 07:16:59 +02:00
}
public IUserManager UserManager { get; private set; }
public IAuthorizationContext AuthorizationContext { get; private set; }
2014-11-15 16:51:49 +01:00
public ISessionManager SessionManager { get; private set; }
2018-09-12 19:26:21 +02:00
public INetworkManager NetworkManager { get; private set; }
2014-07-02 07:16:59 +02:00
/// <summary>
/// Redirect the client to a specific URL if authentication failed.
/// If this property is null, simply `401 Unauthorized` is returned.
/// </summary>
public string HtmlRedirect { get; set; }
2018-09-12 19:26:21 +02:00
public void Authenticate(IRequest request, IAuthenticationAttributes authAttribtues)
{
2014-11-15 03:31:03 +01:00
ValidateUser(request, authAttribtues);
}
2018-09-12 19:26:21 +02:00
private void ValidateUser(IRequest request, IAuthenticationAttributes authAttribtues)
{
2014-10-07 01:58:46 +02:00
// This code is executed before the service
2014-11-15 03:31:03 +01:00
var auth = AuthorizationContext.GetAuthorizationInfo(request);
2014-07-02 07:16:59 +02:00
if (!IsExemptFromAuthenticationToken(authAttribtues, request))
2014-07-02 07:16:59 +02:00
{
2018-12-11 01:27:54 +01:00
ValidateSecurityToken(request, auth.Token);
2014-07-08 03:41:03 +02:00
}
2014-07-02 07:16:59 +02:00
2018-09-12 19:26:21 +02:00
if (authAttribtues.AllowLocalOnly && !request.IsLocal)
{
throw new SecurityException("Operation not found.");
}
2014-07-02 07:16:59 +02:00
2018-09-12 19:26:21 +02:00
var user = auth.User;
if (user == null & !auth.UserId.Equals(Guid.Empty))
2014-07-15 03:25:58 +02:00
{
2014-11-15 03:31:03 +01:00
throw new SecurityException("User with Id " + auth.UserId + " not found");
2014-07-15 03:25:58 +02:00
}
if (user != null)
2014-07-08 03:41:03 +02:00
{
2014-12-29 21:18:48 +01:00
ValidateUserAccess(user, request, authAttribtues, auth);
2014-07-02 07:16:59 +02:00
}
2014-07-08 03:41:03 +02:00
2015-02-07 04:25:23 +01:00
var info = GetTokenInfo(request);
2015-02-06 06:39:07 +01:00
2017-09-03 20:38:26 +02:00
if (!IsExemptFromRoles(auth, authAttribtues, request, info))
2014-11-15 03:31:03 +01:00
{
2017-08-30 20:52:29 +02:00
var roles = authAttribtues.GetRoles();
2014-11-15 03:31:03 +01:00
ValidateRoles(roles, user);
}
2014-11-15 16:51:49 +01:00
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(auth.DeviceId) &&
!string.IsNullOrEmpty(auth.Client) &&
!string.IsNullOrEmpty(auth.Device))
2014-11-15 16:51:49 +01:00
{
SessionManager.LogSessionActivity(auth.Client,
auth.Version,
auth.DeviceId,
auth.Device,
request.RemoteIp,
user);
}
2014-11-15 03:31:03 +01:00
}
2017-09-03 20:38:26 +02:00
private void ValidateUserAccess(User user, IRequest request,
2014-12-29 21:18:48 +01:00
IAuthenticationAttributes authAttribtues,
AuthorizationInfo auth)
{
if (user.Policy.IsDisabled)
{
throw new SecurityException("User account has been disabled.")
{
SecurityExceptionType = SecurityExceptionType.Unauthenticated
};
}
2018-09-12 19:26:21 +02:00
if (!user.Policy.EnableRemoteAccess && !NetworkManager.IsInLocalNetwork(request.RemoteIp))
{
throw new SecurityException("User account has been disabled.")
{
SecurityExceptionType = SecurityExceptionType.Unauthenticated
};
}
2014-12-29 21:18:48 +01:00
if (!user.Policy.IsAdministrator &&
!authAttribtues.EscapeParentalControl &&
!user.IsParentalScheduleAllowed())
{
2017-09-03 20:38:26 +02:00
request.Response.AddHeader("X-Application-Error-Code", "ParentalControl");
2014-12-29 21:18:48 +01:00
throw new SecurityException("This user account is not allowed access at this time.")
{
SecurityExceptionType = SecurityExceptionType.ParentalControl
};
}
}
private bool IsExemptFromAuthenticationToken(IAuthenticationAttributes authAttribtues, IRequest request)
2014-11-15 03:31:03 +01:00
{
2016-02-18 19:27:46 +01:00
if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
2014-11-15 03:31:03 +01:00
{
return true;
}
2017-09-03 20:38:26 +02:00
if (authAttribtues.AllowLocal && request.IsLocal)
{
return true;
}
2018-09-12 19:26:21 +02:00
if (authAttribtues.AllowLocalOnly && request.IsLocal)
{
return true;
}
2017-09-03 20:38:26 +02:00
2016-02-18 19:27:46 +01:00
return false;
2014-11-15 03:31:03 +01:00
}
2017-09-03 20:38:26 +02:00
private bool IsExemptFromRoles(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues, IRequest request, AuthenticationInfo tokenInfo)
2014-11-15 03:31:03 +01:00
{
2016-02-18 19:27:46 +01:00
if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
2014-11-15 03:31:03 +01:00
{
return true;
}
2017-09-03 20:38:26 +02:00
if (authAttribtues.AllowLocal && request.IsLocal)
{
return true;
}
2018-09-12 19:26:21 +02:00
if (authAttribtues.AllowLocalOnly && request.IsLocal)
{
return true;
}
if (string.IsNullOrEmpty(auth.Token))
2015-02-06 06:39:07 +01:00
{
return true;
}
2018-09-12 19:26:21 +02:00
if (tokenInfo != null && tokenInfo.UserId.Equals(Guid.Empty))
2015-02-06 06:39:07 +01:00
{
return true;
}
2014-11-15 03:31:03 +01:00
return false;
}
2014-10-29 00:17:55 +01:00
private static void ValidateRoles(string[] roles, User user)
2014-11-15 03:31:03 +01:00
{
2014-09-14 19:42:23 +02:00
if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase))
{
2014-12-20 07:06:27 +01:00
if (user == null || !user.Policy.IsAdministrator)
2014-09-14 19:42:23 +02:00
{
2014-11-15 03:31:03 +01:00
throw new SecurityException("User does not have admin access.")
{
SecurityExceptionType = SecurityExceptionType.Unauthenticated
};
2014-09-14 19:42:23 +02:00
}
}
2014-11-15 03:31:03 +01:00
if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase))
{
2014-12-20 07:06:27 +01:00
if (user == null || !user.Policy.EnableContentDeletion)
2014-11-15 03:31:03 +01:00
{
throw new SecurityException("User does not have delete access.")
{
SecurityExceptionType = SecurityExceptionType.Unauthenticated
};
}
}
2015-02-06 06:39:07 +01:00
if (roles.Contains("download", StringComparer.OrdinalIgnoreCase))
{
if (user == null || !user.Policy.EnableContentDownloading)
{
throw new SecurityException("User does not have download access.")
{
SecurityExceptionType = SecurityExceptionType.Unauthenticated
};
}
}
}
private static AuthenticationInfo GetTokenInfo(IRequest request)
2015-02-07 04:25:23 +01:00
{
request.Items.TryGetValue("OriginalAuthenticationInfo", out var info);
2015-02-07 04:25:23 +01:00
return info as AuthenticationInfo;
}
2017-09-03 20:38:26 +02:00
private void ValidateSecurityToken(IRequest request, string token)
{
2018-09-12 19:26:21 +02:00
if (string.IsNullOrEmpty(token))
{
2015-10-05 00:04:56 +02:00
throw new SecurityException("Access token is required.");
}
2015-02-07 04:25:23 +01:00
var info = GetTokenInfo(request);
2014-11-15 03:31:03 +01:00
if (info == null)
{
2014-11-15 03:31:03 +01:00
throw new SecurityException("Access token is invalid or expired.");
}
2018-09-12 19:26:21 +02:00
//if (!string.IsNullOrEmpty(info.UserId))
2014-11-15 03:31:03 +01:00
//{
// var user = _userManager.GetUserById(info.UserId);
2014-11-15 03:31:03 +01:00
// if (user == null || user.Configuration.IsDisabled)
// {
// throw new SecurityException("User account has been disabled.");
// }
//}
}
}
}