2014-10-06 23:58:46 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-07-02 05:16:59 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
2014-07-02 04:57:18 +00:00
|
|
|
|
using ServiceStack;
|
|
|
|
|
using ServiceStack.Auth;
|
|
|
|
|
using ServiceStack.Web;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
|
|
|
|
{
|
|
|
|
|
public class AuthService : IAuthService
|
|
|
|
|
{
|
2014-07-08 01:41:03 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
|
|
|
|
|
public AuthService(IUserManager userManager, ISessionManager sessionManager, IAuthorizationContext authorizationContext, IServerConfigurationManager config)
|
2014-07-02 05:16:59 +00:00
|
|
|
|
{
|
|
|
|
|
AuthorizationContext = authorizationContext;
|
2014-07-08 01:41:03 +00:00
|
|
|
|
_config = config;
|
2014-07-02 05:16:59 +00:00
|
|
|
|
SessionManager = sessionManager;
|
|
|
|
|
UserManager = userManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IUserManager UserManager { get; private set; }
|
|
|
|
|
public ISessionManager SessionManager { get; private set; }
|
|
|
|
|
public IAuthorizationContext AuthorizationContext { get; private set; }
|
|
|
|
|
|
2014-07-02 04:57:18 +00:00
|
|
|
|
/// <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; }
|
|
|
|
|
|
2014-09-14 17:42:23 +00:00
|
|
|
|
public void Authenticate(IRequest request,
|
|
|
|
|
IResponse response,
|
|
|
|
|
object requestDto,
|
|
|
|
|
bool allowLocal,
|
|
|
|
|
string[] roles)
|
2014-07-02 04:57:18 +00:00
|
|
|
|
{
|
2014-09-14 17:42:23 +00:00
|
|
|
|
if (HostContext.HasValidAuthSecret(request))
|
2014-07-02 04:57:18 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2014-07-09 00:46:11 +00:00
|
|
|
|
//ExecuteBasic(req, res, requestDto); //first check if session is authenticated
|
|
|
|
|
//if (res.IsClosed) return; //AuthenticateAttribute already closed the request (ie auth failed)
|
2014-07-02 04:57:18 +00:00
|
|
|
|
|
2014-09-14 17:42:23 +00:00
|
|
|
|
ValidateUser(request, allowLocal, roles);
|
2014-07-02 04:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-14 17:42:23 +00:00
|
|
|
|
private void ValidateUser(IRequest req, bool allowLocal,
|
2014-10-06 23:58:46 +00:00
|
|
|
|
IEnumerable<string> roles)
|
2014-07-02 04:57:18 +00:00
|
|
|
|
{
|
2014-10-06 23:58:46 +00:00
|
|
|
|
// This code is executed before the service
|
2014-07-02 05:16:59 +00:00
|
|
|
|
var auth = AuthorizationContext.GetAuthorizationInfo(req);
|
|
|
|
|
|
2014-10-06 23:58:46 +00:00
|
|
|
|
if (!allowLocal || !req.IsLocal)
|
2014-07-02 05:16:59 +00:00
|
|
|
|
{
|
2014-10-15 12:46:24 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(auth.Token) ||
|
|
|
|
|
!_config.Configuration.InsecureApps2.Contains(auth.Client ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
2014-08-31 19:15:33 +00:00
|
|
|
|
{
|
2014-10-18 19:02:54 +00:00
|
|
|
|
if (!IsValidConnectKey(auth.Token))
|
|
|
|
|
{
|
|
|
|
|
SessionManager.ValidateSecurityToken(auth.Token);
|
|
|
|
|
}
|
2014-08-31 19:15:33 +00:00
|
|
|
|
}
|
2014-07-08 01:41:03 +00:00
|
|
|
|
}
|
2014-07-02 05:16:59 +00:00
|
|
|
|
|
2014-07-08 01:41:03 +00:00
|
|
|
|
var user = string.IsNullOrWhiteSpace(auth.UserId)
|
|
|
|
|
? null
|
2014-09-14 15:10:51 +00:00
|
|
|
|
: UserManager.GetUserById(auth.UserId);
|
2014-07-02 05:16:59 +00:00
|
|
|
|
|
2014-07-15 01:25:58 +00:00
|
|
|
|
if (user == null & !string.IsNullOrWhiteSpace(auth.UserId))
|
|
|
|
|
{
|
2014-09-14 17:42:23 +00:00
|
|
|
|
throw new ArgumentException("User with Id " + auth.UserId + " not found");
|
2014-07-15 01:25:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-15 00:05:09 +00:00
|
|
|
|
if (user != null)
|
2014-07-08 01:41:03 +00:00
|
|
|
|
{
|
2014-10-15 00:05:09 +00:00
|
|
|
|
if (user.Configuration.IsDisabled)
|
|
|
|
|
{
|
|
|
|
|
throw new AuthenticationException("User account has been disabled.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!user.Configuration.IsAdministrator && !user.IsParentalScheduleAllowed())
|
|
|
|
|
{
|
|
|
|
|
throw new AuthenticationException("This user account is not allowed access at this time.");
|
|
|
|
|
}
|
2014-07-02 05:16:59 +00:00
|
|
|
|
}
|
2014-07-08 01:41:03 +00:00
|
|
|
|
|
2014-09-14 17:42:23 +00:00
|
|
|
|
if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
if (user == null || !user.Configuration.IsAdministrator)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Administrative access is required for this request.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-08 01:41:03 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(auth.DeviceId) &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(auth.Client) &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(auth.Device))
|
2014-07-02 04:57:18 +00:00
|
|
|
|
{
|
2014-07-08 01:41:03 +00:00
|
|
|
|
SessionManager.LogSessionActivity(auth.Client,
|
|
|
|
|
auth.Version,
|
|
|
|
|
auth.DeviceId,
|
|
|
|
|
auth.Device,
|
|
|
|
|
req.RemoteIp,
|
|
|
|
|
user);
|
2014-07-02 04:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-18 19:02:54 +00:00
|
|
|
|
private bool IsValidConnectKey(string token)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(token))
|
|
|
|
|
{
|
|
|
|
|
return UserManager.Users.Any(u => string.Equals(token, u.ConnectAccessKey, StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(u.ConnectAccessKey));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 04:57:18 +00:00
|
|
|
|
protected bool DoHtmlRedirectIfConfigured(IRequest req, IResponse res, bool includeRedirectParam = false)
|
|
|
|
|
{
|
|
|
|
|
var htmlRedirect = this.HtmlRedirect ?? AuthenticateService.HtmlRedirect;
|
|
|
|
|
if (htmlRedirect != null && req.ResponseContentType.MatchesContentType(MimeTypes.Html))
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.RedirectToUrl(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ToQueryString(INameValueCollection queryStringCollection)
|
|
|
|
|
{
|
|
|
|
|
return ToQueryString((NameValueCollection)queryStringCollection.Original);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ToQueryString(NameValueCollection queryStringCollection)
|
|
|
|
|
{
|
|
|
|
|
if (queryStringCollection == null || queryStringCollection.Count == 0)
|
|
|
|
|
return String.Empty;
|
|
|
|
|
|
|
|
|
|
return "?" + queryStringCollection.ToFormUrlEncoded();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|