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;
|
2015-03-09 19:40:03 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 19:02:04 +00:00
|
|
|
|
using MediaBrowser.Model.Services;
|
2014-07-02 04:57:18 +00:00
|
|
|
|
|
2016-11-04 01:18:51 +00:00
|
|
|
|
namespace Emby.Server.Implementations.HttpServer.Security
|
2014-07-02 04:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 18:38:26 +00:00
|
|
|
|
public Task<SessionInfo> GetSession(IRequest requestContext)
|
2014-07-02 04:57:18 +00:00
|
|
|
|
{
|
|
|
|
|
var authorization = _authContext.GetAuthorizationInfo(requestContext);
|
|
|
|
|
|
2015-05-25 03:01:44 +00:00
|
|
|
|
var user = string.IsNullOrWhiteSpace(authorization.UserId) ? null : _userManager.GetUserById(authorization.UserId);
|
|
|
|
|
return _sessionManager.LogSessionActivity(authorization.Client, authorization.Version, authorization.DeviceId, authorization.Device, requestContext.RemoteIp, user);
|
2014-07-02 04:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 18:38:26 +00:00
|
|
|
|
private AuthenticationInfo GetTokenInfo(IRequest 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
|
|
|
|
{
|
2017-09-03 18:38:26 +00:00
|
|
|
|
return GetSession((IRequest)requestContext);
|
2014-11-15 02:31:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 18:38:26 +00:00
|
|
|
|
public async Task<User> GetUser(IRequest requestContext)
|
2015-03-09 19:40:03 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
{
|
2017-09-03 18:38:26 +00:00
|
|
|
|
return GetUser((IRequest)requestContext);
|
2014-11-15 02:31:03 +00:00
|
|
|
|
}
|
2014-07-02 04:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|