2019-11-01 10:38:54 -07:00
|
|
|
#pragma warning disable CS1591
|
2019-12-10 16:13:57 -07:00
|
|
|
#pragma warning disable SA1600
|
2019-11-01 10:38:54 -07:00
|
|
|
|
2019-01-13 12:20:41 -07:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2019-11-23 08:31:02 -07:00
|
|
|
using Emby.Server.Implementations.SocketSharp;
|
2019-01-13 12:20:41 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-01-06 13:50:43 -07:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-11-14 19:31:03 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-07-01 22:16:59 -07:00
|
|
|
using MediaBrowser.Controller.Net;
|
2014-11-14 19:31:03 -07:00
|
|
|
using MediaBrowser.Controller.Security;
|
2014-11-15 08:51:49 -07:00
|
|
|
using MediaBrowser.Controller.Session;
|
2017-09-03 11:38:26 -07:00
|
|
|
using MediaBrowser.Model.Services;
|
2019-11-23 08:31:02 -07:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2014-07-01 21:57:18 -07:00
|
|
|
|
2016-11-03 18:18:51 -07:00
|
|
|
namespace Emby.Server.Implementations.HttpServer.Security
|
2014-07-01 21:57:18 -07:00
|
|
|
{
|
|
|
|
public class AuthService : IAuthService
|
|
|
|
{
|
2019-11-23 08:31:02 -07:00
|
|
|
private readonly ILogger<AuthService> _logger;
|
2019-07-28 14:53:19 -07:00
|
|
|
private readonly IAuthorizationContext _authorizationContext;
|
|
|
|
private readonly ISessionManager _sessionManager;
|
2014-07-07 18:41:03 -07:00
|
|
|
private readonly IServerConfigurationManager _config;
|
2019-07-28 14:53:19 -07:00
|
|
|
private readonly INetworkManager _networkManager;
|
2014-07-07 18:41:03 -07:00
|
|
|
|
2019-07-28 14:53:19 -07:00
|
|
|
public AuthService(
|
2019-11-24 10:25:43 -07:00
|
|
|
ILogger<AuthService> logger,
|
2019-07-28 14:53:19 -07:00
|
|
|
IAuthorizationContext authorizationContext,
|
|
|
|
IServerConfigurationManager config,
|
|
|
|
ISessionManager sessionManager,
|
|
|
|
INetworkManager networkManager)
|
2014-07-01 22:16:59 -07:00
|
|
|
{
|
2019-11-24 10:25:43 -07:00
|
|
|
_logger = logger;
|
2019-07-28 14:53:19 -07:00
|
|
|
_authorizationContext = authorizationContext;
|
2014-07-07 18:41:03 -07:00
|
|
|
_config = config;
|
2019-07-28 14:53:19 -07:00
|
|
|
_sessionManager = sessionManager;
|
|
|
|
_networkManager = networkManager;
|
2014-07-01 22:16:59 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public void Authenticate(IRequest request, IAuthenticationAttributes authAttribtues)
|
2014-07-01 21:57:18 -07:00
|
|
|
{
|
2014-11-14 19:31:03 -07:00
|
|
|
ValidateUser(request, authAttribtues);
|
2014-07-01 21:57:18 -07:00
|
|
|
}
|
|
|
|
|
2019-11-23 08:31:02 -07:00
|
|
|
public User Authenticate(HttpRequest request, IAuthenticationAttributes authAttributes)
|
|
|
|
{
|
|
|
|
var req = new WebSocketSharpRequest(request, null, request.Path, _logger);
|
|
|
|
var user = ValidateUser(req, authAttributes);
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
private User ValidateUser(IRequest request, IAuthenticationAttributes authAttribtues)
|
2014-07-01 21:57:18 -07:00
|
|
|
{
|
2014-10-06 16:58:46 -07:00
|
|
|
// This code is executed before the service
|
2019-07-28 14:53:19 -07:00
|
|
|
var auth = _authorizationContext.GetAuthorizationInfo(request);
|
2014-07-01 22:16:59 -07:00
|
|
|
|
2019-03-25 14:25:32 -07:00
|
|
|
if (!IsExemptFromAuthenticationToken(authAttribtues, request))
|
2014-07-01 22:16:59 -07:00
|
|
|
{
|
2018-12-10 17:27:54 -07:00
|
|
|
ValidateSecurityToken(request, auth.Token);
|
2014-07-07 18:41:03 -07:00
|
|
|
}
|
2014-07-01 22:16:59 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (authAttribtues.AllowLocalOnly && !request.IsLocal)
|
|
|
|
{
|
|
|
|
throw new SecurityException("Operation not found.");
|
|
|
|
}
|
2014-07-01 22:16:59 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var user = auth.User;
|
|
|
|
|
2019-09-20 03:42:08 -07:00
|
|
|
if (user == null && auth.UserId != Guid.Empty)
|
2014-07-14 18:25:58 -07:00
|
|
|
{
|
2014-11-14 19:31:03 -07:00
|
|
|
throw new SecurityException("User with Id " + auth.UserId + " not found");
|
2014-07-14 18:25:58 -07:00
|
|
|
}
|
|
|
|
|
2014-10-14 17:05:09 -07:00
|
|
|
if (user != null)
|
2014-07-07 18:41:03 -07:00
|
|
|
{
|
2014-12-29 13:18:48 -07:00
|
|
|
ValidateUserAccess(user, request, authAttribtues, auth);
|
2014-07-01 22:16:59 -07:00
|
|
|
}
|
2014-07-07 18:41:03 -07:00
|
|
|
|
2015-02-06 20:25:23 -07:00
|
|
|
var info = GetTokenInfo(request);
|
2015-02-05 22:39:07 -07:00
|
|
|
|
2017-09-03 11:38:26 -07:00
|
|
|
if (!IsExemptFromRoles(auth, authAttribtues, request, info))
|
2014-11-14 19:31:03 -07:00
|
|
|
{
|
2017-08-30 11:52:29 -07:00
|
|
|
var roles = authAttribtues.GetRoles();
|
2014-11-14 19:31:03 -07:00
|
|
|
|
|
|
|
ValidateRoles(roles, user);
|
|
|
|
}
|
2014-11-15 08:51:49 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!string.IsNullOrEmpty(auth.DeviceId) &&
|
|
|
|
!string.IsNullOrEmpty(auth.Client) &&
|
|
|
|
!string.IsNullOrEmpty(auth.Device))
|
2014-11-15 08:51:49 -07:00
|
|
|
{
|
2019-07-28 14:53:19 -07:00
|
|
|
_sessionManager.LogSessionActivity(auth.Client,
|
2014-11-15 08:51:49 -07:00
|
|
|
auth.Version,
|
|
|
|
auth.DeviceId,
|
|
|
|
auth.Device,
|
|
|
|
request.RemoteIp,
|
|
|
|
user);
|
|
|
|
}
|
2019-11-23 08:31:02 -07:00
|
|
|
|
|
|
|
return user;
|
2014-11-14 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
2019-07-28 14:53:19 -07:00
|
|
|
private void ValidateUserAccess(
|
|
|
|
User user,
|
|
|
|
IRequest request,
|
2014-12-29 13:18:48 -07:00
|
|
|
IAuthenticationAttributes authAttribtues,
|
|
|
|
AuthorizationInfo auth)
|
|
|
|
{
|
|
|
|
if (user.Policy.IsDisabled)
|
|
|
|
{
|
|
|
|
throw new SecurityException("User account has been disabled.")
|
|
|
|
{
|
|
|
|
SecurityExceptionType = SecurityExceptionType.Unauthenticated
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-28 14:53:19 -07:00
|
|
|
if (!user.Policy.EnableRemoteAccess && !_networkManager.IsInLocalNetwork(request.RemoteIp))
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
throw new SecurityException("User account has been disabled.")
|
|
|
|
{
|
|
|
|
SecurityExceptionType = SecurityExceptionType.Unauthenticated
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-28 14:53:19 -07:00
|
|
|
if (!user.Policy.IsAdministrator
|
|
|
|
&& !authAttribtues.EscapeParentalControl
|
|
|
|
&& !user.IsParentalScheduleAllowed())
|
2014-12-29 13:18:48 -07:00
|
|
|
{
|
2019-07-28 14:53:19 -07:00
|
|
|
request.Response.Headers.Add("X-Application-Error-Code", "ParentalControl");
|
2014-12-29 13:18:48 -07:00
|
|
|
|
|
|
|
throw new SecurityException("This user account is not allowed access at this time.")
|
|
|
|
{
|
|
|
|
SecurityExceptionType = SecurityExceptionType.ParentalControl
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:25:32 -07:00
|
|
|
private bool IsExemptFromAuthenticationToken(IAuthenticationAttributes authAttribtues, IRequest request)
|
2014-11-14 19:31:03 -07:00
|
|
|
{
|
2016-02-18 11:27:46 -07:00
|
|
|
if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
|
2014-11-14 19:31:03 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-03 11:38:26 -07:00
|
|
|
if (authAttribtues.AllowLocal && request.IsLocal)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
if (authAttribtues.AllowLocalOnly && request.IsLocal)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-03 11:38:26 -07:00
|
|
|
|
2016-02-18 11:27:46 -07:00
|
|
|
return false;
|
2014-11-14 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
2017-09-03 11:38:26 -07:00
|
|
|
private bool IsExemptFromRoles(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues, IRequest request, AuthenticationInfo tokenInfo)
|
2014-11-14 19:31:03 -07:00
|
|
|
{
|
2016-02-18 11:27:46 -07:00
|
|
|
if (!_config.Configuration.IsStartupWizardCompleted && authAttribtues.AllowBeforeStartupWizard)
|
2014-11-14 19:31:03 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-03 11:38:26 -07:00
|
|
|
if (authAttribtues.AllowLocal && request.IsLocal)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (authAttribtues.AllowLocalOnly && request.IsLocal)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(auth.Token))
|
2015-02-05 22:39:07 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (tokenInfo != null && tokenInfo.UserId.Equals(Guid.Empty))
|
2015-02-05 22:39:07 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-28 16:17:55 -07:00
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static void ValidateRoles(string[] roles, User user)
|
2014-11-14 19:31:03 -07:00
|
|
|
{
|
2014-09-14 10:42:23 -07:00
|
|
|
if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
2014-12-19 23:06:27 -07:00
|
|
|
if (user == null || !user.Policy.IsAdministrator)
|
2014-09-14 10:42:23 -07:00
|
|
|
{
|
2014-11-14 19:31:03 -07:00
|
|
|
throw new SecurityException("User does not have admin access.")
|
|
|
|
{
|
|
|
|
SecurityExceptionType = SecurityExceptionType.Unauthenticated
|
|
|
|
};
|
2014-09-14 10:42:23 -07:00
|
|
|
}
|
|
|
|
}
|
2019-07-28 14:53:19 -07:00
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase))
|
2014-07-01 21:57:18 -07:00
|
|
|
{
|
2014-12-19 23:06:27 -07:00
|
|
|
if (user == null || !user.Policy.EnableContentDeletion)
|
2014-11-14 19:31:03 -07:00
|
|
|
{
|
|
|
|
throw new SecurityException("User does not have delete access.")
|
|
|
|
{
|
|
|
|
SecurityExceptionType = SecurityExceptionType.Unauthenticated
|
|
|
|
};
|
|
|
|
}
|
2014-07-01 21:57:18 -07:00
|
|
|
}
|
2019-07-28 14:53:19 -07:00
|
|
|
|
2015-02-05 22:39:07 -07: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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2014-07-01 21:57:18 -07:00
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static AuthenticationInfo GetTokenInfo(IRequest request)
|
2015-02-06 20:25:23 -07:00
|
|
|
{
|
2019-01-13 13:46:33 -07:00
|
|
|
request.Items.TryGetValue("OriginalAuthenticationInfo", out var info);
|
2015-02-06 20:25:23 -07:00
|
|
|
return info as AuthenticationInfo;
|
|
|
|
}
|
|
|
|
|
2017-09-03 11:38:26 -07:00
|
|
|
private void ValidateSecurityToken(IRequest request, string token)
|
2014-07-01 21:57:18 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (string.IsNullOrEmpty(token))
|
2014-07-01 21:57:18 -07:00
|
|
|
{
|
2015-10-04 15:04:56 -07:00
|
|
|
throw new SecurityException("Access token is required.");
|
2014-07-01 21:57:18 -07:00
|
|
|
}
|
|
|
|
|
2015-02-06 20:25:23 -07:00
|
|
|
var info = GetTokenInfo(request);
|
2014-11-14 19:31:03 -07:00
|
|
|
|
|
|
|
if (info == null)
|
2014-07-01 21:57:18 -07:00
|
|
|
{
|
2014-11-14 19:31:03 -07:00
|
|
|
throw new SecurityException("Access token is invalid or expired.");
|
2014-07-01 21:57:18 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
//if (!string.IsNullOrEmpty(info.UserId))
|
2014-11-14 19:31:03 -07:00
|
|
|
//{
|
|
|
|
// var user = _userManager.GetUserById(info.UserId);
|
2014-07-01 21:57:18 -07:00
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
// if (user == null || user.Configuration.IsDisabled)
|
|
|
|
// {
|
|
|
|
// throw new SecurityException("User account has been disabled.");
|
|
|
|
// }
|
|
|
|
//}
|
2014-07-01 21:57:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|