2014-07-02 04:57:18 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Net;
|
2015-03-09 19:40:03 +00:00
|
|
|
|
using MediaBrowser.Controller.Security;
|
2014-07-02 04:57:18 +00:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using ServiceStack.Web;
|
2015-03-09 19:40:03 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2014-07-02 04:57:18 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.HttpServer.Security
|
|
|
|
|
{
|
|
|
|
|
public class SessionContext : ISessionContext
|
|
|
|
|
{
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
|
|
|
|
|
|
|
|
|
public SessionContext(IUserManager userManager, IAuthorizationContext authContext, ISessionManager sessionManager)
|
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_authContext = authContext;
|
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 19:40:03 +00:00
|
|
|
|
public Task<SessionInfo> GetSession(IServiceRequest requestContext)
|
2014-07-02 04:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
var authorization = _authContext.GetAuthorizationInfo(requestContext);
|
|
|
|
|
|
2015-03-08 19:48:30 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(authorization.Token))
|
|
|
|
|
{
|
2015-03-09 19:40:03 +00:00
|
|
|
|
var auth = GetTokenInfo(requestContext);
|
|
|
|
|
return _sessionManager.GetSessionByAuthenticationToken(auth, requestContext.RemoteIp, authorization.Version);
|
2015-03-08 19:48:30 +00:00
|
|
|
|
}
|
2015-03-09 19:40:03 +00:00
|
|
|
|
|
|
|
|
|
var session = _sessionManager.GetSession(authorization.DeviceId, authorization.Client, authorization.Version);
|
|
|
|
|
return Task.FromResult(session);
|
2014-07-02 04:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 19:40:03 +00:00
|
|
|
|
private AuthenticationInfo GetTokenInfo(IServiceRequest request)
|
2014-07-02 04:57:18 +00:00
|
|
|
|
{
|
2015-03-09 19:40:03 +00:00
|
|
|
|
object info;
|
|
|
|
|
request.Items.TryGetValue("OriginalAuthenticationInfo", out info);
|
|
|
|
|
return info as AuthenticationInfo;
|
2014-07-02 04:57:18 +00:00
|
|
|
|
}
|
2014-11-15 02:31:03 +00:00
|
|
|
|
|
2015-03-09 19:40:03 +00:00
|
|
|
|
public Task<SessionInfo> GetSession(object requestContext)
|
2014-11-15 02:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
var req = new ServiceStackServiceRequest((IRequest)requestContext);
|
|
|
|
|
return GetSession(req);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 19:40:03 +00:00
|
|
|
|
public async Task<User> GetUser(IServiceRequest requestContext)
|
|
|
|
|
{
|
|
|
|
|
var session = await GetSession(requestContext).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return session == null || !session.UserId.HasValue ? null : _userManager.GetUserById(session.UserId.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<User> GetUser(object requestContext)
|
2014-11-15 02:31:03 +00:00
|
|
|
|
{
|
|
|
|
|
var req = new ServiceStackServiceRequest((IRequest)requestContext);
|
|
|
|
|
return GetUser(req);
|
|
|
|
|
}
|
2014-07-02 04:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|