mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-06 18:26:33 +00:00
revise endpoint attributes
This commit is contained in:
@@ -91,7 +91,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
|
||||
{typeof (ResourceNotFoundException), 404},
|
||||
{typeof (FileNotFoundException), 404},
|
||||
{typeof (DirectoryNotFoundException), 404},
|
||||
{typeof (Implementations.Security.AuthenticationException), 401},
|
||||
{typeof (SecurityException), 401},
|
||||
{typeof (UnauthorizedAccessException), 401}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Connect;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using ServiceStack;
|
||||
using ServiceStack.Auth;
|
||||
using ServiceStack.Web;
|
||||
using MediaBrowser.Controller.Security;
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
@@ -16,61 +14,43 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
{
|
||||
private readonly IServerConfigurationManager _config;
|
||||
|
||||
public AuthService(IUserManager userManager, ISessionManager sessionManager, IAuthorizationContext authorizationContext, IServerConfigurationManager config, IConnectManager connectManager)
|
||||
public AuthService(IUserManager userManager, IAuthorizationContext authorizationContext, IServerConfigurationManager config, IConnectManager connectManager)
|
||||
{
|
||||
AuthorizationContext = authorizationContext;
|
||||
_config = config;
|
||||
ConnectManager = connectManager;
|
||||
SessionManager = sessionManager;
|
||||
UserManager = userManager;
|
||||
}
|
||||
|
||||
public IUserManager UserManager { get; private set; }
|
||||
public ISessionManager SessionManager { get; private set; }
|
||||
public IAuthorizationContext AuthorizationContext { get; private set; }
|
||||
public IConnectManager ConnectManager { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Restrict authentication to a specific <see cref="IAuthProvider"/>.
|
||||
/// For example, if this attribute should only permit access
|
||||
/// if the user is authenticated with <see cref="BasicAuthProvider"/>,
|
||||
/// you should set this property to <see cref="BasicAuthProvider.Name"/>.
|
||||
/// </summary>
|
||||
public string Provider { get; set; }
|
||||
|
||||
/// <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; }
|
||||
|
||||
public void Authenticate(IRequest request,
|
||||
IResponse response,
|
||||
object requestDto,
|
||||
IAuthenticated authAttribtues)
|
||||
public void Authenticate(IServiceRequest request,
|
||||
IAuthenticationAttributes authAttribtues)
|
||||
{
|
||||
if (HostContext.HasValidAuthSecret(request))
|
||||
return;
|
||||
|
||||
//ExecuteBasic(req, res, requestDto); //first check if session is authenticated
|
||||
//if (res.IsClosed) return; //AuthenticateAttribute already closed the request (ie auth failed)
|
||||
|
||||
ValidateUser(request, response, authAttribtues);
|
||||
ValidateUser(request, authAttribtues);
|
||||
}
|
||||
|
||||
private void ValidateUser(IRequest req, IResponse response, IAuthenticated authAttribtues)
|
||||
private void ValidateUser(IServiceRequest request,
|
||||
IAuthenticationAttributes authAttribtues)
|
||||
{
|
||||
// This code is executed before the service
|
||||
var auth = AuthorizationContext.GetAuthorizationInfo(req);
|
||||
var auth = AuthorizationContext.GetAuthorizationInfo(request);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(auth.Token) ||
|
||||
!_config.Configuration.InsecureApps6.Contains(auth.Client ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
||||
if (!IsExemptFromAuthenticationToken(auth, authAttribtues))
|
||||
{
|
||||
var valid = IsValidConnectKey(auth.Token);
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
SessionManager.ValidateSecurityToken(auth.Token);
|
||||
ValidateSecurityToken(request, auth.Token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,45 +60,83 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
|
||||
if (user == null & !string.IsNullOrWhiteSpace(auth.UserId))
|
||||
{
|
||||
throw new ArgumentException("User with Id " + auth.UserId + " not found");
|
||||
throw new SecurityException("User with Id " + auth.UserId + " not found");
|
||||
}
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
if (user.Configuration.IsDisabled)
|
||||
{
|
||||
throw new AuthenticationException("User account has been disabled.");
|
||||
throw new SecurityException("User account has been disabled.")
|
||||
{
|
||||
SecurityExceptionType = SecurityExceptionType.Unauthenticated
|
||||
};
|
||||
}
|
||||
|
||||
if (!user.Configuration.IsAdministrator &&
|
||||
!authAttribtues.EscapeParentalControl &&
|
||||
!user.IsParentalScheduleAllowed())
|
||||
{
|
||||
response.AddHeader("X-Application-Error-Code", "ParentalControl");
|
||||
throw new AuthenticationException("This user account is not allowed access at this time.");
|
||||
request.AddResponseHeader("X-Application-Error-Code", "ParentalControl");
|
||||
throw new SecurityException("This user account is not allowed access at this time.")
|
||||
{
|
||||
SecurityExceptionType = SecurityExceptionType.ParentalControl
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
var roles = authAttribtues.GetRoles().ToList();
|
||||
if (!IsExemptFromRoles(auth, authAttribtues))
|
||||
{
|
||||
var roles = authAttribtues.GetRoles().ToList();
|
||||
|
||||
ValidateRoles(roles, user);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private void ValidateRoles(List<string> roles, User user)
|
||||
{
|
||||
if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
if (user == null || !user.Configuration.IsAdministrator)
|
||||
{
|
||||
throw new ArgumentException("Administrative access is required for this request.");
|
||||
throw new SecurityException("User does not have admin access.")
|
||||
{
|
||||
SecurityExceptionType = SecurityExceptionType.Unauthenticated
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(auth.DeviceId) &&
|
||||
!string.IsNullOrWhiteSpace(auth.Client) &&
|
||||
!string.IsNullOrWhiteSpace(auth.Device))
|
||||
if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
SessionManager.LogSessionActivity(auth.Client,
|
||||
auth.Version,
|
||||
auth.DeviceId,
|
||||
auth.Device,
|
||||
req.RemoteIp,
|
||||
user);
|
||||
if (user == null || !user.Configuration.EnableContentDeletion)
|
||||
{
|
||||
throw new SecurityException("User does not have delete access.")
|
||||
{
|
||||
SecurityExceptionType = SecurityExceptionType.Unauthenticated
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,40 +150,34 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
return ConnectManager.IsAuthorizationTokenValid(token);
|
||||
}
|
||||
|
||||
protected bool DoHtmlRedirectIfConfigured(IRequest req, IResponse res, bool includeRedirectParam = false)
|
||||
private void ValidateSecurityToken(IServiceRequest request, string token)
|
||||
{
|
||||
var htmlRedirect = this.HtmlRedirect ?? AuthenticateService.HtmlRedirect;
|
||||
if (htmlRedirect != null && req.ResponseContentType.MatchesContentType(MimeTypes.Html))
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
DoHtmlRedirect(htmlRedirect, req, res, includeRedirectParam);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void DoHtmlRedirect(string redirectUrl, IRequest req, IResponse res, bool includeRedirectParam)
|
||||
{
|
||||
var url = req.ResolveAbsoluteUrl(redirectUrl);
|
||||
if (includeRedirectParam)
|
||||
{
|
||||
var absoluteRequestPath = req.ResolveAbsoluteUrl("~" + req.PathInfo + ToQueryString(req.QueryString));
|
||||
url = url.AddQueryParam(HostContext.ResolveLocalizedString(LocalizedStrings.Redirect), absoluteRequestPath);
|
||||
throw new SecurityException("Access token is invalid or expired.");
|
||||
}
|
||||
|
||||
res.RedirectToUrl(url);
|
||||
}
|
||||
var info = (AuthenticationInfo)request.Items["OriginalAuthenticationInfo"];
|
||||
|
||||
private static string ToQueryString(INameValueCollection queryStringCollection)
|
||||
{
|
||||
return ToQueryString((NameValueCollection)queryStringCollection.Original);
|
||||
}
|
||||
if (info == null)
|
||||
{
|
||||
throw new SecurityException("Access token is invalid or expired.");
|
||||
}
|
||||
|
||||
private static string ToQueryString(NameValueCollection queryStringCollection)
|
||||
{
|
||||
if (queryStringCollection == null || queryStringCollection.Count == 0)
|
||||
return String.Empty;
|
||||
if (!info.IsActive)
|
||||
{
|
||||
throw new SecurityException("Access token has expired.");
|
||||
}
|
||||
|
||||
return "?" + queryStringCollection.ToFormUrlEncoded();
|
||||
//if (!string.IsNullOrWhiteSpace(info.UserId))
|
||||
//{
|
||||
// var user = _userManager.GetUserById(info.UserId);
|
||||
|
||||
// if (user == null || user.Configuration.IsDisabled)
|
||||
// {
|
||||
// throw new SecurityException("User account has been disabled.");
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using MediaBrowser.Controller.Security;
|
||||
using ServiceStack.Web;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
{
|
||||
public class AuthorizationContext : IAuthorizationContext
|
||||
{
|
||||
public AuthorizationInfo GetAuthorizationInfo(IRequest requestContext)
|
||||
private readonly IAuthenticationRepository _authRepo;
|
||||
|
||||
public AuthorizationContext(IAuthenticationRepository authRepo)
|
||||
{
|
||||
_authRepo = authRepo;
|
||||
}
|
||||
|
||||
public AuthorizationInfo GetAuthorizationInfo(object requestContext)
|
||||
{
|
||||
var req = new ServiceStackServiceRequest((IRequest)requestContext);
|
||||
return GetAuthorizationInfo(req);
|
||||
}
|
||||
|
||||
public AuthorizationInfo GetAuthorizationInfo(IServiceRequest requestContext)
|
||||
{
|
||||
object cached;
|
||||
if (requestContext.Items.TryGetValue("AuthorizationInfo", out cached))
|
||||
{
|
||||
return (AuthorizationInfo)cached;
|
||||
}
|
||||
|
||||
return GetAuthorization(requestContext);
|
||||
}
|
||||
|
||||
@@ -17,7 +38,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
/// </summary>
|
||||
/// <param name="httpReq">The HTTP req.</param>
|
||||
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
||||
private AuthorizationInfo GetAuthorization(IRequest httpReq)
|
||||
private AuthorizationInfo GetAuthorization(IServiceRequest httpReq)
|
||||
{
|
||||
var auth = GetAuthorizationDictionary(httpReq);
|
||||
|
||||
@@ -29,7 +50,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
|
||||
if (auth != null)
|
||||
{
|
||||
// TODO: Remove this
|
||||
auth.TryGetValue("UserId", out userId);
|
||||
|
||||
auth.TryGetValue("DeviceId", out deviceId);
|
||||
auth.TryGetValue("Device", out device);
|
||||
auth.TryGetValue("Client", out client);
|
||||
@@ -84,7 +107,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
}
|
||||
}
|
||||
|
||||
return new AuthorizationInfo
|
||||
var info = new AuthorizationInfo
|
||||
{
|
||||
Client = client,
|
||||
Device = device,
|
||||
@@ -93,6 +116,26 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
Version = version,
|
||||
Token = token
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
var result = _authRepo.Get(new AuthenticationInfoQuery
|
||||
{
|
||||
AccessToken = token
|
||||
});
|
||||
|
||||
var tokenInfo = result.Items.FirstOrDefault();
|
||||
|
||||
if (tokenInfo != null)
|
||||
{
|
||||
info.UserId = tokenInfo.UserId;
|
||||
}
|
||||
httpReq.Items["OriginalAuthenticationInfo"] = tokenInfo;
|
||||
}
|
||||
|
||||
httpReq.Items["AuthorizationInfo"] = info;
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -100,7 +143,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
/// </summary>
|
||||
/// <param name="httpReq">The HTTP req.</param>
|
||||
/// <returns>Dictionary{System.StringSystem.String}.</returns>
|
||||
private Dictionary<string, string> GetAuthorizationDictionary(IRequest httpReq)
|
||||
private Dictionary<string, string> GetAuthorizationDictionary(IServiceRequest httpReq)
|
||||
{
|
||||
var auth = httpReq.Headers["Authorization"];
|
||||
|
||||
|
||||
@@ -19,18 +19,30 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
||||
_sessionManager = sessionManager;
|
||||
}
|
||||
|
||||
public SessionInfo GetSession(IRequest requestContext)
|
||||
public SessionInfo GetSession(IServiceRequest requestContext)
|
||||
{
|
||||
var authorization = _authContext.GetAuthorizationInfo(requestContext);
|
||||
|
||||
return _sessionManager.GetSession(authorization.DeviceId, authorization.Client, authorization.Version);
|
||||
}
|
||||
|
||||
public User GetUser(IRequest requestContext)
|
||||
public User GetUser(IServiceRequest requestContext)
|
||||
{
|
||||
var session = GetSession(requestContext);
|
||||
|
||||
return session == null || !session.UserId.HasValue ? null : _userManager.GetUserById(session.UserId.Value);
|
||||
}
|
||||
|
||||
public SessionInfo GetSession(object requestContext)
|
||||
{
|
||||
var req = new ServiceStackServiceRequest((IRequest)requestContext);
|
||||
return GetSession(req);
|
||||
}
|
||||
|
||||
public User GetUser(object requestContext)
|
||||
{
|
||||
var req = new ServiceStackServiceRequest((IRequest)requestContext);
|
||||
return GetUser(req);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user