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