You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Server.Impleme.../HttpServer/Security/SessionContext.cs

65 lines
2.4 KiB

using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
using ServiceStack.Web;
using System.Threading.Tasks;
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;
}
public Task<SessionInfo> GetSession(IServiceRequest requestContext)
{
var authorization = _authContext.GetAuthorizationInfo(requestContext);
if (!string.IsNullOrWhiteSpace(authorization.Token))
{
var auth = GetTokenInfo(requestContext);
return _sessionManager.GetSessionByAuthenticationToken(auth, authorization.DeviceId, requestContext.RemoteIp, authorization.Version);
}
var session = _sessionManager.GetSession(authorization.DeviceId, authorization.Client, authorization.Version);
return Task.FromResult(session);
}
private AuthenticationInfo GetTokenInfo(IServiceRequest request)
{
object info;
request.Items.TryGetValue("OriginalAuthenticationInfo", out info);
return info as AuthenticationInfo;
}
public Task<SessionInfo> GetSession(object requestContext)
{
var req = new ServiceStackServiceRequest((IRequest)requestContext);
return GetSession(req);
}
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)
{
var req = new ServiceStackServiceRequest((IRequest)requestContext);
return GetUser(req);
}
}
}