2014-10-28 16:17:55 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-11-02 20:38:43 -07:00
|
|
|
|
using MediaBrowser.Controller.Connect;
|
2014-12-29 13:18:48 -07:00
|
|
|
|
using MediaBrowser.Controller.Devices;
|
2014-11-14 19:31:03 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-07-01 22:16:59 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
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;
|
2014-07-01 21:57:18 -07:00
|
|
|
|
using System;
|
2014-11-14 19:31:03 -07:00
|
|
|
|
using System.Collections.Generic;
|
2014-07-01 21:57:18 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
|
|
|
|
{
|
|
|
|
|
public class AuthService : IAuthService
|
|
|
|
|
{
|
2014-07-07 18:41:03 -07:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
|
2014-12-29 13:18:48 -07:00
|
|
|
|
public AuthService(IUserManager userManager, IAuthorizationContext authorizationContext, IServerConfigurationManager config, IConnectManager connectManager, ISessionManager sessionManager, IDeviceManager deviceManager)
|
2014-07-01 22:16:59 -07:00
|
|
|
|
{
|
|
|
|
|
AuthorizationContext = authorizationContext;
|
2014-07-07 18:41:03 -07:00
|
|
|
|
_config = config;
|
2014-12-29 13:18:48 -07:00
|
|
|
|
DeviceManager = deviceManager;
|
2014-11-15 08:51:49 -07:00
|
|
|
|
SessionManager = sessionManager;
|
2014-11-02 20:38:43 -07:00
|
|
|
|
ConnectManager = connectManager;
|
2014-07-01 22:16:59 -07:00
|
|
|
|
UserManager = userManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IUserManager UserManager { get; private set; }
|
|
|
|
|
public IAuthorizationContext AuthorizationContext { get; private set; }
|
2014-11-02 20:38:43 -07:00
|
|
|
|
public IConnectManager ConnectManager { get; private set; }
|
2014-11-15 08:51:49 -07:00
|
|
|
|
public ISessionManager SessionManager { get; private set; }
|
2014-12-29 13:18:48 -07:00
|
|
|
|
public IDeviceManager DeviceManager { get; private set; }
|
2014-07-01 22:16:59 -07:00
|
|
|
|
|
2014-07-01 21:57:18 -07: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; }
|
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
public void Authenticate(IServiceRequest 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
|
|
|
|
}
|
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
private void ValidateUser(IServiceRequest 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
|
2014-11-14 19:31:03 -07:00
|
|
|
|
var auth = AuthorizationContext.GetAuthorizationInfo(request);
|
2014-07-01 22:16:59 -07:00
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
if (!IsExemptFromAuthenticationToken(auth, authAttribtues))
|
2014-07-01 22:16:59 -07:00
|
|
|
|
{
|
2014-11-08 20:18:14 -07:00
|
|
|
|
var valid = IsValidConnectKey(auth.Token);
|
2014-11-02 20:38:43 -07:00
|
|
|
|
|
2014-11-08 20:18:14 -07:00
|
|
|
|
if (!valid)
|
|
|
|
|
{
|
2014-11-14 19:31:03 -07:00
|
|
|
|
ValidateSecurityToken(request, auth.Token);
|
2014-08-31 12:15:33 -07:00
|
|
|
|
}
|
2014-07-07 18:41:03 -07:00
|
|
|
|
}
|
2014-07-01 22:16:59 -07:00
|
|
|
|
|
2014-07-07 18:41:03 -07:00
|
|
|
|
var user = string.IsNullOrWhiteSpace(auth.UserId)
|
|
|
|
|
? null
|
2014-09-14 08:10:51 -07:00
|
|
|
|
: UserManager.GetUserById(auth.UserId);
|
2014-07-01 22:16:59 -07:00
|
|
|
|
|
2014-07-14 18:25:58 -07:00
|
|
|
|
if (user == null & !string.IsNullOrWhiteSpace(auth.UserId))
|
|
|
|
|
{
|
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
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
if (!IsExemptFromRoles(auth, authAttribtues))
|
|
|
|
|
{
|
|
|
|
|
var roles = authAttribtues.GetRoles().ToList();
|
|
|
|
|
|
|
|
|
|
ValidateRoles(roles, user);
|
|
|
|
|
}
|
2014-11-15 08:51:49 -07:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(auth.DeviceId) &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(auth.Client) &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(auth.Device))
|
|
|
|
|
{
|
|
|
|
|
SessionManager.LogSessionActivity(auth.Client,
|
|
|
|
|
auth.Version,
|
|
|
|
|
auth.DeviceId,
|
|
|
|
|
auth.Device,
|
|
|
|
|
request.RemoteIp,
|
|
|
|
|
user);
|
|
|
|
|
}
|
2014-11-14 19:31:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-29 13:18:48 -07:00
|
|
|
|
private void ValidateUserAccess(User user, IServiceRequest request,
|
|
|
|
|
IAuthenticationAttributes authAttribtues,
|
|
|
|
|
AuthorizationInfo auth)
|
|
|
|
|
{
|
|
|
|
|
if (user.Policy.IsDisabled)
|
|
|
|
|
{
|
|
|
|
|
throw new SecurityException("User account has been disabled.")
|
|
|
|
|
{
|
|
|
|
|
SecurityExceptionType = SecurityExceptionType.Unauthenticated
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!user.Policy.IsAdministrator &&
|
|
|
|
|
!authAttribtues.EscapeParentalControl &&
|
|
|
|
|
!user.IsParentalScheduleAllowed())
|
|
|
|
|
{
|
|
|
|
|
request.AddResponseHeader("X-Application-Error-Code", "ParentalControl");
|
|
|
|
|
|
|
|
|
|
throw new SecurityException("This user account is not allowed access at this time.")
|
|
|
|
|
{
|
|
|
|
|
SecurityExceptionType = SecurityExceptionType.ParentalControl
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(auth.DeviceId))
|
|
|
|
|
{
|
|
|
|
|
if (!DeviceManager.CanAccessDevice(user.Id.ToString("N"), auth.DeviceId))
|
|
|
|
|
{
|
|
|
|
|
throw new SecurityException("User is not allowed access from this device.")
|
|
|
|
|
{
|
|
|
|
|
SecurityExceptionType = SecurityExceptionType.ParentalControl
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
private bool IsExemptFromAuthenticationToken(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues)
|
|
|
|
|
{
|
|
|
|
|
if (!_config.Configuration.IsStartupWizardCompleted &&
|
|
|
|
|
authAttribtues.AllowBeforeStartupWizard)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _config.Configuration.InsecureApps7.Contains(auth.Client ?? string.Empty,
|
|
|
|
|
StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsExemptFromRoles(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues)
|
|
|
|
|
{
|
|
|
|
|
if (!_config.Configuration.IsStartupWizardCompleted &&
|
|
|
|
|
authAttribtues.AllowBeforeStartupWizard)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-10-28 16:17:55 -07:00
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
private void ValidateRoles(List<string> roles, User user)
|
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-18 12:02:54 -07:00
|
|
|
|
private bool IsValidConnectKey(string token)
|
|
|
|
|
{
|
2014-11-02 20:38:43 -07:00
|
|
|
|
if (string.IsNullOrEmpty(token))
|
2014-10-18 12:02:54 -07:00
|
|
|
|
{
|
2014-11-02 20:38:43 -07:00
|
|
|
|
return false;
|
2014-10-18 12:02:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-02 20:38:43 -07:00
|
|
|
|
return ConnectManager.IsAuthorizationTokenValid(token);
|
2014-10-18 12:02:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
private void ValidateSecurityToken(IServiceRequest request, string token)
|
2014-07-01 21:57:18 -07:00
|
|
|
|
{
|
2014-11-14 19:31:03 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(token))
|
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
|
|
|
|
}
|
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
var info = (AuthenticationInfo)request.Items["OriginalAuthenticationInfo"];
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
if (!info.IsActive)
|
|
|
|
|
{
|
|
|
|
|
throw new SecurityException("Access token has expired.");
|
|
|
|
|
}
|
2014-07-01 21:57:18 -07:00
|
|
|
|
|
2014-11-14 19:31:03 -07:00
|
|
|
|
//if (!string.IsNullOrWhiteSpace(info.UserId))
|
|
|
|
|
//{
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|