jellyfin/Emby.Server.Implementations/HttpServer/Security/SessionContext.cs

58 lines
1.9 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
2015-03-09 12:40:03 -07:00
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
2015-03-09 12:40:03 -07:00
using System.Threading.Tasks;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.Services;
2018-09-12 10:26:21 -07:00
using System;
2016-11-03 18:18:51 -07:00
namespace Emby.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;
}
2018-09-12 10:26:21 -07:00
public SessionInfo GetSession(IRequest requestContext)
{
var authorization = _authContext.GetAuthorizationInfo(requestContext);
2018-09-12 10:26:21 -07:00
var user = authorization.User;
2015-05-24 20:01:44 -07:00
return _sessionManager.LogSessionActivity(authorization.Client, authorization.Version, authorization.DeviceId, authorization.Device, requestContext.RemoteIp, user);
}
2017-09-03 11:38:26 -07:00
private AuthenticationInfo GetTokenInfo(IRequest request)
{
2015-03-09 12:40:03 -07:00
object info;
request.Items.TryGetValue("OriginalAuthenticationInfo", out info);
return info as AuthenticationInfo;
}
2014-11-14 19:31:03 -07:00
2018-09-12 10:26:21 -07:00
public SessionInfo GetSession(object requestContext)
2014-11-14 19:31:03 -07:00
{
2017-09-03 11:38:26 -07:00
return GetSession((IRequest)requestContext);
2014-11-14 19:31:03 -07:00
}
2018-09-12 10:26:21 -07:00
public User GetUser(IRequest requestContext)
2015-03-09 12:40:03 -07:00
{
2018-09-12 10:26:21 -07:00
var session = GetSession(requestContext);
2015-03-09 12:40:03 -07:00
2018-09-12 10:26:21 -07:00
return session == null || session.UserId.Equals(Guid.Empty) ? null : _userManager.GetUserById(session.UserId);
2015-03-09 12:40:03 -07:00
}
2018-09-12 10:26:21 -07:00
public User GetUser(object requestContext)
2014-11-14 19:31:03 -07:00
{
2017-09-03 11:38:26 -07:00
return GetUser((IRequest)requestContext);
2014-11-14 19:31:03 -07:00
}
}
}