예제 #1
0
        public static IAuthSession GetSession(this IRequest httpReq, bool reload = false)
        {
            if (httpReq == null)
            {
                return(null);
            }

            if (HostContext.TestMode)
            {
                var mockSession = httpReq.TryResolve <IAuthSession>(); //testing
                if (mockSession != null)
                {
                    return(mockSession);
                }
            }

            object oSession = null;

            if (!reload)
            {
                httpReq.Items.TryGetValue(Keywords.Session, out oSession);
            }

            var sessionId = httpReq.GetSessionId();
            var session   = oSession as IAuthSession;

            if (session != null)
            {
                session = HostContext.AppHost.OnSessionFilter(session, sessionId);
            }
            if (session != null)
            {
                return(session);
            }

            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            if (sessionKey != null)
            {
                session = httpReq.GetCacheClient().Get <IAuthSession>(sessionKey);

                if (session != null)
                {
                    session = HostContext.AppHost.OnSessionFilter(session, sessionId);
                }
            }

            if (session == null)
            {
                var newSession = SessionFeature.CreateNewSession(httpReq, sessionId);
                session = HostContext.AppHost.OnSessionFilter(newSession, sessionId) ?? newSession;
            }

            httpReq.Items[Keywords.Session] = session;
            return(session);
        }
예제 #2
0
        /// <summary>
        /// Override to intercept when Sessions using async APIs are saved
        /// </summary>
        public virtual Task OnSaveSessionAsync(IRequest httpReq, IAuthSession session, TimeSpan?expiresIn = null, CancellationToken token = default)
        {
            if (httpReq == null || session.FromToken) // Don't persist Sessions populated from tokens
            {
                return(TypeConstants.EmptyTask);
            }

            var sessionKey = SessionFeature.GetSessionKey(session.Id ?? httpReq.GetOrCreateSessionId());

            session.LastModified            = DateTime.UtcNow;
            httpReq.Items[Keywords.Session] = session;
            return(this.GetCacheClientAsync(httpReq).CacheSetAsync(sessionKey, session, expiresIn ?? GetDefaultSessionExpiry(httpReq), token));
        }
        public virtual void OnSaveSession(IRequest httpReq, IAuthSession session, TimeSpan?expiresIn = null)
        {
            if (httpReq == null)
            {
                return;
            }

            var sessionKey = SessionFeature.GetSessionKey(session.Id ?? httpReq.GetOrCreateSessionId());

            session.LastModified = DateTime.UtcNow;
            this.GetCacheClient().CacheSet(sessionKey, session, expiresIn ?? GetDefaultSessionExpiry(httpReq));

            httpReq.Items[SessionFeature.RequestItemsSessionKey] = session;
        }
예제 #4
0
        public static void RemoveSession(this IRequest httpReq)
        {
            if (httpReq == null)
            {
                return;
            }

            using (var cache = httpReq.GetCacheClient())
            {
                var sessionKey = SessionFeature.GetSessionKey(httpReq.GetSessionId());
                cache.Remove(sessionKey);
            }

            httpReq.Items.Remove(RequestItemsSessionKey);
        }
예제 #5
0
        public static void SaveSession(this IRequest httpReq, IAuthSession session, TimeSpan?expiresIn = null)
        {
            if (httpReq == null)
            {
                return;
            }

            using (var cache = httpReq.GetCacheClient())
            {
                var sessionKey = SessionFeature.GetSessionKey(httpReq.GetSessionId());
                cache.CacheSet(sessionKey, session, expiresIn ?? HostContext.GetDefaultSessionExpiry());
            }

            httpReq.Items[RequestItemsSessionKey] = session;
        }
        public virtual void OnSaveSession(IRequest httpReq, IAuthSession session, TimeSpan?expiresIn = null)
        {
            if (httpReq == null)
            {
                return;
            }

            using (var cache = this.GetCacheClient())
            {
                var sessionKey = SessionFeature.GetSessionKey(session.Id ?? httpReq.GetOrCreateSessionId());
                session.LastModified = DateTime.UtcNow;
                cache.CacheSet(sessionKey, session, expiresIn ?? HostContext.GetDefaultSessionExpiry());
            }

            httpReq.Items[ServiceExtensions.RequestItemsSessionKey] = session;
        }
예제 #7
0
        public static async Task RemoveSessionAsync(this IRequest httpReq, string sessionId, CancellationToken token = default)
        {
            if (httpReq == null)
            {
                return;
            }
            if (sessionId == null)
            {
                throw new ArgumentNullException(nameof(sessionId));
            }

            var sessionKey = SessionFeature.GetSessionKey(sessionId);
            await httpReq.GetCacheClientAsync().RemoveAsync(sessionKey, token).ConfigAwait();

            httpReq.Items.Remove(Keywords.Session);
        }
예제 #8
0
        public static IAuthSession GetSession(this IRequest httpReq, bool reload = false)
        {
            if (httpReq == null)
            {
                return(null);
            }

            object oSession = null;

            if (!reload)
            {
                httpReq.Items.TryGetValue(RequestItemsSessionKey, out oSession);
            }

            if (oSession != null)
            {
                return((IAuthSession)oSession);
            }

            using (var cache = httpReq.GetCacheClient())
            {
                var sessionId = httpReq.GetSessionId();
                var session   = cache.Get <IAuthSession>(SessionFeature.GetSessionKey(sessionId));
                if (session == null)
                {
                    session           = AuthenticateService.CurrentSessionFactory();
                    session.Id        = sessionId;
                    session.CreatedAt = session.LastModified = DateTime.UtcNow;
                    session.OnCreated(httpReq);

                    var authEvents = HostContext.TryResolve <IAuthEvents>();
                    if (authEvents != null)
                    {
                        authEvents.OnCreated(httpReq, session);
                    }
                }

                if (httpReq.Items.ContainsKey(RequestItemsSessionKey))
                {
                    httpReq.Items.Remove(RequestItemsSessionKey);
                }

                httpReq.Items.Add(RequestItemsSessionKey, session);
                return(session);
            }
        }
예제 #9
0
        public static IAuthSession GetSession(this IRequest httpReq, bool reload = false)
        {
            if (httpReq == null)
            {
                return(null);
            }

            if (HostContext.TestMode)
            {
                var mockSession = httpReq.TryResolve <IAuthSession>(); //testing
                if (mockSession != null)
                {
                    return(mockSession);
                }
            }

            object oSession = null;

            if (!reload)
            {
                httpReq.Items.TryGetValue(SessionFeature.RequestItemsSessionKey, out oSession);
            }

            var sessionId     = httpReq.GetSessionId();
            var cachedSession = FilterSession(oSession as IAuthSession, sessionId);

            if (cachedSession != null)
            {
                return(cachedSession);
            }

            using (var cache = httpReq.GetCacheClient())
            {
                var sessionKey = SessionFeature.GetSessionKey(sessionId);
                var session    = (sessionKey != null ? FilterSession(cache.Get <IAuthSession>(sessionKey), sessionId) : null)
                                 ?? SessionFeature.CreateNewSession(httpReq, sessionId);

                if (httpReq.Items.ContainsKey(SessionFeature.RequestItemsSessionKey))
                {
                    httpReq.Items.Remove(SessionFeature.RequestItemsSessionKey);
                }

                httpReq.Items.Add(SessionFeature.RequestItemsSessionKey, session);
                return(session);
            }
        }
예제 #10
0
        public static void RemoveSession(this IRequest httpReq, string sessionId)
        {
            if (httpReq == null)
            {
                return;
            }
            if (sessionId == null)
            {
                throw new ArgumentNullException(nameof(sessionId));
            }

            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            httpReq.GetCacheClient().Remove(sessionKey);

            httpReq.Items.Remove(Keywords.Session);
        }
예제 #11
0
        public static void RemoveSession(this IRequest httpReq, string sessionId)
        {
            if (httpReq == null)
            {
                return;
            }
            if (sessionId == null)
            {
                throw new ArgumentNullException("sessionId");
            }

            using (var cache = httpReq.GetCacheClient())
            {
                var sessionKey = SessionFeature.GetSessionKey(sessionId);
                cache.Remove(sessionKey);
            }

            httpReq.Items.Remove(SessionFeature.RequestItemsSessionKey);
        }
        public virtual void OnSaveSession(IRequest httpReq, IAuthSession session, TimeSpan?expiresIn = null)
        {
            if (httpReq == null)
            {
                return;
            }

            if (session.FromToken) // Don't persist Sessions populated from tokens
            {
                return;
            }

            var sessionKey = SessionFeature.GetSessionKey(session.Id ?? httpReq.GetOrCreateSessionId());

            session.LastModified = DateTime.UtcNow;
            this.GetCacheClient(httpReq).CacheSet(sessionKey, session, expiresIn ?? GetDefaultSessionExpiry(httpReq));

            httpReq.Items[Keywords.Session] = session;
        }
예제 #13
0
        public static TimeSpan?GetSessionTimeToLive(this ICacheClient cache, string sessionId)
        {
            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            return(cache.GetTimeToLive(sessionKey));
        }
예제 #14
0
        public static IAuthSession GetSession(this IRequest httpReq, bool reload = false)
        {
            if (httpReq == null)
            {
                return(null);
            }

            if (HostContext.TestMode)
            {
                var mockSession = httpReq.TryResolve <IAuthSession>(); //testing
                if (mockSession != null)
                {
                    return(mockSession);
                }
            }

            object oSession = null;

            if (!reload)
            {
                httpReq.Items.TryGetValue(Keywords.Session, out oSession);
            }

            if (oSession == null && !httpReq.Items.ContainsKey(Keywords.HasPreAuthenticated))
            {
                try
                {
                    HostContext.AppHost.ApplyPreAuthenticateFilters(httpReq, httpReq.Response);
                    httpReq.Items.TryGetValue(Keywords.Session, out oSession);
                }
                catch (Exception ex)
                {
                    Log.Error("Error in GetSession() when ApplyPreAuthenticateFilters", ex);
                    /*treat errors as non-existing session*/
                }
            }

            var sessionId = httpReq.GetSessionId();
            var session   = oSession as IAuthSession;

            if (session != null)
            {
                session = HostContext.AppHost.OnSessionFilter(httpReq, session, sessionId);
            }
            if (session != null)
            {
                return(session);
            }

            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            if (sessionKey != null)
            {
                session = httpReq.GetCacheClient().Get <IAuthSession>(sessionKey);

                if (session != null)
                {
                    session = HostContext.AppHost.OnSessionFilter(httpReq, session, sessionId);
                }
            }

            if (session == null)
            {
                var newSession = SessionFeature.CreateNewSession(httpReq, sessionId);
                session = HostContext.AppHost.OnSessionFilter(httpReq, newSession, sessionId) ?? newSession;
            }

            httpReq.Items[Keywords.Session] = session;
            return(session);
        }
예제 #15
0
        public static async Task <TimeSpan?> GetSessionTimeToLiveAsync(this ICacheClientAsync cache, string sessionId, CancellationToken token = default)
        {
            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            return(await cache.GetTimeToLiveAsync(sessionKey, token).ConfigAwait());
        }
예제 #16
0
        internal static async Task <IAuthSession> GetSessionInternalAsync(this IRequest httpReq, bool reload, bool async, CancellationToken token = default)
        {
            if (httpReq == null)
            {
                return(null);
            }

            if (HostContext.TestMode)
            {
                var mockSession = httpReq.TryResolve <IAuthSession>(); //testing
                if (mockSession != null)
                {
                    return(mockSession);
                }
            }

            httpReq.Items.TryGetValue(Keywords.Session, out var oSession);
            if (reload && (oSession as IAuthSession)?.FromToken != true) // can't reload FromToken sessions from cache
            {
                oSession = null;
            }

            var appHost = HostContext.AppHost;

            if (oSession == null && !httpReq.Items.ContainsKey(Keywords.HasPreAuthenticated))
            {
                try
                {
                    await appHost.ApplyPreAuthenticateFiltersAsync(httpReq, httpReq.Response).ConfigAwait();

                    httpReq.Items.TryGetValue(Keywords.Session, out oSession);
                }
                catch (Exception ex)
                {
                    Log.Error("Error in GetSession() when ApplyPreAuthenticateFilters", ex);
                    /*treat errors as non-existing session*/
                }
            }

            var sessionId = httpReq.GetSessionId();
            var session   = oSession as IAuthSession;

            if (session != null)
            {
                session = appHost.OnSessionFilter(httpReq, session, sessionId);
            }
            if (session != null)
            {
                return(session);
            }

            if (appHost.HasValidAuthSecret(httpReq))
            {
                session = HostContext.GetAuthSecretSession();
                if (session != null)
                {
                    return(session);
                }
            }

            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            if (sessionKey != null)
            {
                session = async
                    ? await httpReq.GetCacheClientAsync().GetAsync <IAuthSession>(sessionKey, token).ConfigAwait()
                    : httpReq.GetCacheClient().Get <IAuthSession>(sessionKey);

                if (session != null)
                {
                    session = appHost.OnSessionFilter(httpReq, session, sessionId);
                }
            }

            if (session == null)
            {
                var newSession = SessionFeature.CreateNewSession(httpReq, sessionId);
                session = appHost.OnSessionFilter(httpReq, newSession, sessionId) ?? newSession;
            }

            httpReq.Items[Keywords.Session] = session;
            return(session);
        }
        public static string GetSessionKey(IRequest httpReq = null)
        {
            var sessionId = httpReq.GetSessionId();

            return(sessionId == null ? null : SessionFeature.GetSessionKey(sessionId));
        }
        public static IAuthSession GetSession(this IRequest request, bool reload = false)
        {
            if (HostContext.TestMode)
            {
                var mockSession = request.TryResolve <IAuthSession>(); //testing
                if (mockSession != null)
                {
                    return(mockSession);
                }
            }

            object oSession = null;

            if (!reload)
            {
                request.Items.TryGetValue(Keywords.Session, out oSession);
            }

            // Apply PreAuthenticate Filters from IAuthWithRequest AuthProviders
            if (oSession == null && !request.Items.ContainsKey(Keywords.HasPreAuthenticated))
            {
                request.Items[Keywords.HasPreAuthenticated] = true;
                foreach (var authProvider in AuthenticateService.AuthWithRequestProviders)
                {
                    try { authProvider.PreAuthenticate(request, request.Response); } catch { }
                    //throw new Exception("Error in GetSession() when ApplyPreAuthenticateFilters", ex);
                    /*treat errors as non-existing session*/
                }
                request.Items.TryGetValue(Keywords.Session, out oSession);
            }

            var sessionId = request.GetSessionId() ?? request.CreateSessionIds(request.Response);
            var session   = oSession as IAuthSession;

            if (session != null)
            {
                session = HostContext.AppHost.OnSessionFilter(session, sessionId);
            }
            if (session != null)
            {
                return(session);
            }

            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            if (sessionKey != null)
            {
                session = request.GetCacheClient().Get <IAuthSession>(sessionKey);

                if (session != null)
                {
                    session = HostContext.AppHost.OnSessionFilter(session, sessionId);
                }
            }

            if (session == null)
            {
                var newSession = SessionFeature.CreateNewSession(request, sessionId);
                session = HostContext.AppHost.OnSessionFilter(newSession, sessionId) ?? newSession;
            }

            request.Items[Keywords.Session] = session;
            return(session);
        }