Browse Source

fix session not found errors

Luke Pulverenti 10 years ago
parent
commit
1b46fb62c4

+ 4 - 3
MediaBrowser.Api/BaseApiService.cs

@@ -1,4 +1,5 @@
-using MediaBrowser.Controller.Dto;
+using System.Threading.Tasks;
+using MediaBrowser.Controller.Dto;
 using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Entities.Audio;
 using MediaBrowser.Controller.Library;
@@ -88,9 +89,9 @@ namespace MediaBrowser.Api
         /// Gets the session.
         /// </summary>
         /// <returns>SessionInfo.</returns>
-        protected SessionInfo GetSession()
+        protected async Task<SessionInfo> GetSession()
         {
-            var session = SessionContext.GetSession(Request);
+            var session = await SessionContext.GetSession(Request).ConfigureAwait(false);
 
             if (session == null)
             {

+ 1 - 1
MediaBrowser.Api/LiveTv/LiveTvService.cs

@@ -315,7 +315,7 @@ namespace MediaBrowser.Api.LiveTv
 
         private void AssertUserCanManageLiveTv()
         {
-            var user = SessionContext.GetUser(Request);
+            var user = SessionContext.GetUser(Request).Result;
 
             if (user == null)
             {

+ 9 - 9
MediaBrowser.Api/Session/SessionsService.cs

@@ -418,7 +418,7 @@ namespace MediaBrowser.Api.Session
                 SeekPositionTicks = request.SeekPositionTicks
             };
 
-            var task = _sessionManager.SendPlaystateCommand(GetSession().Id, request.Id, command, CancellationToken.None);
+            var task = _sessionManager.SendPlaystateCommand(GetSession().Result.Id, request.Id, command, CancellationToken.None);
 
             Task.WaitAll(task);
         }
@@ -436,7 +436,7 @@ namespace MediaBrowser.Api.Session
                 ItemType = request.ItemType
             };
 
-            var task = _sessionManager.SendBrowseCommand(GetSession().Id, request.Id, command, CancellationToken.None);
+            var task = _sessionManager.SendBrowseCommand(GetSession().Result.Id, request.Id, command, CancellationToken.None);
 
             Task.WaitAll(task);
         }
@@ -455,7 +455,7 @@ namespace MediaBrowser.Api.Session
                 name = commandType.ToString();
             }
 
-            var currentSession = GetSession();
+            var currentSession = GetSession().Result;
 
             var command = new GeneralCommand
             {
@@ -481,7 +481,7 @@ namespace MediaBrowser.Api.Session
                 Text = request.Text
             };
 
-            var task = _sessionManager.SendMessageCommand(GetSession().Id, request.Id, command, CancellationToken.None);
+            var task = _sessionManager.SendMessageCommand(GetSession().Result.Id, request.Id, command, CancellationToken.None);
 
             Task.WaitAll(task);
         }
@@ -500,14 +500,14 @@ namespace MediaBrowser.Api.Session
                 StartPositionTicks = request.StartPositionTicks
             };
 
-            var task = _sessionManager.SendPlayCommand(GetSession().Id, request.Id, command, CancellationToken.None);
+            var task = _sessionManager.SendPlayCommand(GetSession().Result.Id, request.Id, command, CancellationToken.None);
 
             Task.WaitAll(task);
         }
 
         public void Post(SendGeneralCommand request)
         {
-            var currentSession = GetSession();
+            var currentSession = GetSession().Result;
 
             var command = new GeneralCommand
             {
@@ -522,7 +522,7 @@ namespace MediaBrowser.Api.Session
 
         public void Post(SendFullGeneralCommand request)
         {
-            var currentSession = GetSession();
+            var currentSession = GetSession().Result;
 
             request.ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null;
 
@@ -545,7 +545,7 @@ namespace MediaBrowser.Api.Session
         {
             if (string.IsNullOrWhiteSpace(request.Id))
             {
-                request.Id = GetSession().Id;
+                request.Id = GetSession().Result.Id;
             }
             _sessionManager.ReportCapabilities(request.Id, new ClientCapabilities
             {
@@ -569,7 +569,7 @@ namespace MediaBrowser.Api.Session
         {
             if (string.IsNullOrWhiteSpace(request.Id))
             {
-                request.Id = GetSession().Id;
+                request.Id = GetSession().Result.Id;
             }
             _sessionManager.ReportCapabilities(request.Id, request);
         }

+ 5 - 5
MediaBrowser.Api/UserLibrary/PlaystateService.cs

@@ -231,7 +231,7 @@ namespace MediaBrowser.Api.UserLibrary
                 datePlayed = DateTime.ParseExact(request.DatePlayed, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
             }
 
-            var session = GetSession();
+            var session = await GetSession().ConfigureAwait(false);
 
             var dto = await UpdatePlayedStatus(user, request.Id, true, datePlayed).ConfigureAwait(false);
 
@@ -266,7 +266,7 @@ namespace MediaBrowser.Api.UserLibrary
 
         public void Post(ReportPlaybackStart request)
         {
-            request.SessionId = GetSession().Id;
+            request.SessionId = GetSession().Result.Id;
 
             var task = _sessionManager.OnPlaybackStart(request);
 
@@ -294,7 +294,7 @@ namespace MediaBrowser.Api.UserLibrary
 
         public void Post(ReportPlaybackProgress request)
         {
-            request.SessionId = GetSession().Id;
+            request.SessionId = GetSession().Result.Id;
 
             var task = _sessionManager.OnPlaybackProgress(request);
 
@@ -317,7 +317,7 @@ namespace MediaBrowser.Api.UserLibrary
 
         public void Post(ReportPlaybackStopped request)
         {
-            request.SessionId = GetSession().Id;
+            request.SessionId = GetSession().Result.Id;
 
             var task = _sessionManager.OnPlaybackStopped(request);
 
@@ -339,7 +339,7 @@ namespace MediaBrowser.Api.UserLibrary
         {
             var user = _userManager.GetUserById(request.UserId);
 
-            var session = GetSession();
+            var session = await GetSession().ConfigureAwait(false);
 
             var dto = await UpdatePlayedStatus(user, request.Id, false, null).ConfigureAwait(false);
 

+ 6 - 5
MediaBrowser.Controller/Net/ISessionContext.cs

@@ -1,14 +1,15 @@
 using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Session;
+using System.Threading.Tasks;
 
 namespace MediaBrowser.Controller.Net
 {
     public interface ISessionContext 
     {
-        SessionInfo GetSession(object requestContext);
-        User GetUser(object requestContext);
-   
-        SessionInfo GetSession(IServiceRequest requestContext);
-        User GetUser(IServiceRequest requestContext);
+        Task<SessionInfo> GetSession(object requestContext);
+        Task<User> GetUser(object requestContext);
+
+        Task<SessionInfo> GetSession(IServiceRequest requestContext);
+        Task<User> GetUser(IServiceRequest requestContext);
     }
 }

+ 12 - 1
MediaBrowser.Controller/Session/ISessionManager.cs

@@ -1,5 +1,6 @@
 using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Security;
 using MediaBrowser.Model.Entities;
 using MediaBrowser.Model.Events;
 using MediaBrowser.Model.Session;
@@ -282,8 +283,18 @@ namespace MediaBrowser.Controller.Session
         /// Gets the session by authentication token.
         /// </summary>
         /// <param name="token">The token.</param>
+        /// <param name="remoteEndpoint">The remote endpoint.</param>
         /// <returns>SessionInfo.</returns>
-        SessionInfo GetSessionByAuthenticationToken(string token);
+        Task<SessionInfo> GetSessionByAuthenticationToken(string token, string remoteEndpoint);
+
+        /// <summary>
+        /// Gets the session by authentication token.
+        /// </summary>
+        /// <param name="info">The information.</param>
+        /// <param name="remoteEndpoint">The remote endpoint.</param>
+        /// <param name="appVersion">The application version.</param>
+        /// <returns>Task&lt;SessionInfo&gt;.</returns>
+        Task<SessionInfo> GetSessionByAuthenticationToken(AuthenticationInfo info, string remoteEndpoint, string appVersion);
 
         /// <summary>
         /// Logouts the specified access token.

+ 21 - 9
MediaBrowser.Server.Implementations/HttpServer/Security/SessionContext.cs

@@ -1,8 +1,10 @@
 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
 {
@@ -19,31 +21,41 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security
             _sessionManager = sessionManager;
         }
 
-        public SessionInfo GetSession(IServiceRequest requestContext)
+        public Task<SessionInfo> GetSession(IServiceRequest requestContext)
         {
             var authorization = _authContext.GetAuthorizationInfo(requestContext);
 
             if (!string.IsNullOrWhiteSpace(authorization.Token))
             {
-                return _sessionManager.GetSessionByAuthenticationToken(authorization.Token);
+                var auth = GetTokenInfo(requestContext);
+                return _sessionManager.GetSessionByAuthenticationToken(auth, requestContext.RemoteIp, authorization.Version);
             }
-            return _sessionManager.GetSession(authorization.DeviceId, authorization.Client, authorization.Version);
+
+            var session = _sessionManager.GetSession(authorization.DeviceId, authorization.Client, authorization.Version);
+            return Task.FromResult(session);
         }
 
-        public User GetUser(IServiceRequest requestContext)
+        private AuthenticationInfo GetTokenInfo(IServiceRequest request)
         {
-            var session = GetSession(requestContext);
-
-            return session == null || !session.UserId.HasValue ? null : _userManager.GetUserById(session.UserId.Value);
+            object info;
+            request.Items.TryGetValue("OriginalAuthenticationInfo", out info);
+            return info as AuthenticationInfo;
         }
 
-        public SessionInfo GetSession(object requestContext)
+        public Task<SessionInfo> GetSession(object requestContext)
         {
             var req = new ServiceStackServiceRequest((IRequest)requestContext);
             return GetSession(req);
         }
 
-        public User GetUser(object requestContext)
+        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);

+ 54 - 35
MediaBrowser.Server.Implementations/Session/SessionManager.cs

@@ -237,10 +237,7 @@ namespace MediaBrowser.Server.Implementations.Session
 
             var activityDate = DateTime.UtcNow;
 
-            var userId = user == null ? (Guid?)null : user.Id;
-            var username = user == null ? null : user.Name;
-
-            var session = await GetSessionInfo(clientType, appVersion, deviceId, deviceName, remoteEndPoint, userId, username).ConfigureAwait(false);
+            var session = await GetSessionInfo(clientType, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
 
             session.LastActivityDate = activityDate;
 
@@ -281,7 +278,7 @@ namespace MediaBrowser.Server.Implementations.Session
 
                 if (session != null)
                 {
-                    var key = GetSessionKey(session.Client, session.ApplicationVersion, session.DeviceId);
+                    var key = GetSessionKey(session.Client, session.DeviceId);
 
                     SessionInfo removed;
 
@@ -365,7 +362,7 @@ namespace MediaBrowser.Server.Implementations.Session
             }
         }
 
-        private string GetSessionKey(string clientType, string appVersion, string deviceId)
+        private string GetSessionKey(string clientType, string deviceId)
         {
             return clientType + deviceId;
         }
@@ -378,29 +375,31 @@ namespace MediaBrowser.Server.Implementations.Session
         /// <param name="deviceId">The device id.</param>
         /// <param name="deviceName">Name of the device.</param>
         /// <param name="remoteEndPoint">The remote end point.</param>
-        /// <param name="userId">The user identifier.</param>
-        /// <param name="username">The username.</param>
+        /// <param name="user">The user.</param>
         /// <returns>SessionInfo.</returns>
-        private async Task<SessionInfo> GetSessionInfo(string clientType, string appVersion, string deviceId, string deviceName, string remoteEndPoint, Guid? userId, string username)
+        private async Task<SessionInfo> GetSessionInfo(string clientType, string appVersion, string deviceId, string deviceName, string remoteEndPoint, User user)
         {
-            var key = GetSessionKey(clientType, appVersion, deviceId);
+            var key = GetSessionKey(clientType, deviceId);
 
             await _sessionLock.WaitAsync(CancellationToken.None).ConfigureAwait(false);
 
+            var userId = user == null ? (Guid?)null : user.Id;
+            var username = user == null ? null : user.Name;
+
             try
             {
-                SessionInfo connection;
+                SessionInfo sessionInfo;
                 DeviceInfo device = null;
 
-                if (!_activeConnections.TryGetValue(key, out connection))
+                if (!_activeConnections.TryGetValue(key, out sessionInfo))
                 {
-                    var sessionInfo = new SessionInfo
-                    {
-                        Client = clientType,
-                        DeviceId = deviceId,
-                        ApplicationVersion = appVersion,
-                        Id = key.GetMD5().ToString("N")
-                    };
+                    sessionInfo = new SessionInfo
+                   {
+                       Client = clientType,
+                       DeviceId = deviceId,
+                       ApplicationVersion = appVersion,
+                       Id = key.GetMD5().ToString("N")
+                   };
 
                     sessionInfo.DeviceName = deviceName;
                     sessionInfo.UserId = userId;
@@ -410,7 +409,6 @@ namespace MediaBrowser.Server.Implementations.Session
                     OnSessionStarted(sessionInfo);
 
                     _activeConnections.TryAdd(key, sessionInfo);
-                    connection = sessionInfo;
 
                     if (!string.IsNullOrEmpty(deviceId))
                     {
@@ -426,24 +424,25 @@ namespace MediaBrowser.Server.Implementations.Session
                     deviceName = device.CustomName;
                 }
 
-                connection.DeviceName = deviceName;
-                connection.UserId = userId;
-                connection.UserName = username;
-                connection.RemoteEndPoint = remoteEndPoint;
+                sessionInfo.DeviceName = deviceName;
+                sessionInfo.UserId = userId;
+                sessionInfo.UserName = username;
+                sessionInfo.RemoteEndPoint = remoteEndPoint;
+                sessionInfo.ApplicationVersion = appVersion;
 
                 if (!userId.HasValue)
                 {
-                    connection.AdditionalUsers.Clear();
+                    sessionInfo.AdditionalUsers.Clear();
                 }
 
-                if (connection.SessionController == null)
+                if (sessionInfo.SessionController == null)
                 {
-                    connection.SessionController = _sessionFactories
-                        .Select(i => i.GetSessionController(connection))
+                    sessionInfo.SessionController = _sessionFactories
+                        .Select(i => i.GetSessionController(sessionInfo))
                         .FirstOrDefault(i => i != null);
                 }
 
-                return connection;
+                return sessionInfo;
             }
             finally
             {
@@ -920,7 +919,7 @@ namespace MediaBrowser.Server.Implementations.Session
                 return FilterToSingleMediaType(items)
                     .OrderBy(i => i.SortName);
             }
-            
+
             if (item.IsFolder)
             {
                 var folder = (Folder)item;
@@ -1640,7 +1639,25 @@ namespace MediaBrowser.Server.Implementations.Session
                 string.Equals(i.Client, client));
         }
 
-        public SessionInfo GetSessionByAuthenticationToken(string token)
+        public Task<SessionInfo> GetSessionByAuthenticationToken(AuthenticationInfo info, string remoteEndpoint, string appVersion)
+        {
+            if (info == null)
+            {
+                throw new ArgumentNullException("info");
+            }
+
+            var user = string.IsNullOrWhiteSpace(info.UserId)
+                ? null
+                : _userManager.GetUserById(info.UserId);
+
+            appVersion = string.IsNullOrWhiteSpace(appVersion)
+                ? "1"
+                : appVersion;
+
+            return GetSessionInfo(info.AppName, appVersion, info.DeviceId, info.DeviceName, remoteEndpoint, user);
+        }
+
+        public Task<SessionInfo> GetSessionByAuthenticationToken(string token, string remoteEndpoint)
         {
             var result = _authRepo.Get(new AuthenticationInfoQuery
             {
@@ -1654,10 +1671,12 @@ namespace MediaBrowser.Server.Implementations.Session
 
             var info = result.Items[0];
 
-            // TODO: Make Token part of SessionInfo and get result that way
-            // This can't be done until all apps are updated to new authentication.
-            return Sessions.FirstOrDefault(i => string.Equals(i.DeviceId, info.DeviceId) &&
-                string.Equals(i.Client, info.AppName));
+            if (info == null)
+            {
+                return null;
+            }
+
+            return GetSessionByAuthenticationToken(info, remoteEndpoint, null);
         }
 
         public Task SendMessageToUserSessions<T>(string userId, string name, T data,

+ 6 - 7
MediaBrowser.Server.Implementations/Session/SessionWebSocketListener.cs

@@ -1,5 +1,4 @@
 using MediaBrowser.Controller.Net;
-using MediaBrowser.Controller.Security;
 using MediaBrowser.Controller.Session;
 using MediaBrowser.Model.Events;
 using MediaBrowser.Model.Logging;
@@ -61,9 +60,9 @@ namespace MediaBrowser.Server.Implementations.Session
             serverManager.WebSocketConnected += _serverManager_WebSocketConnected;
         }
 
-        void _serverManager_WebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
+        async void _serverManager_WebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
         {
-            var session = GetSession(e.Argument.QueryString);
+            var session = await GetSession(e.Argument.QueryString, e.Argument.RemoteEndPoint).ConfigureAwait(false);
 
             if (session != null)
             {
@@ -84,11 +83,11 @@ namespace MediaBrowser.Server.Implementations.Session
             }
         }
 
-        void _httpServer_WebSocketConnecting(object sender, WebSocketConnectingEventArgs e)
+        async void _httpServer_WebSocketConnecting(object sender, WebSocketConnectingEventArgs e)
         {
             if (e.QueryString.AllKeys.Contains("api_key", StringComparer.OrdinalIgnoreCase))
             {
-                var session = GetSession(e.QueryString);
+                var session = await GetSession(e.QueryString, e.Endpoint).ConfigureAwait(false);
 
                 if (session == null)
                 {
@@ -97,10 +96,10 @@ namespace MediaBrowser.Server.Implementations.Session
             }
         }
 
-        private SessionInfo GetSession(NameValueCollection queryString)
+        private Task<SessionInfo> GetSession(NameValueCollection queryString, string remoteEndpoint)
         {
             var token = queryString["api_key"];
-            return _sessionManager.GetSessionByAuthenticationToken(token);
+            return _sessionManager.GetSessionByAuthenticationToken(token, remoteEndpoint);
         }
 
         public void Dispose()