Пример #1
0
        /// <summary>
        /// Typed UserSession
        /// </summary>
        protected virtual TUserSession SessionAs <TUserSession>()
        {
            var ret = TryResolve <TUserSession>();

            return(!Equals(ret, default(TUserSession))
                ? ret
                : SessionFeature.GetOrCreateSession <TUserSession>(Cache, Request, Response));
        }
Пример #2
0
        public static IAuthSession CreateNewSession(this IUserAuth user, IRequest httpReq)
        {
            var sessionId  = SessionExtensions.CreateRandomSessionId();
            var newSession = SessionFeature.CreateNewSession(httpReq, sessionId);
            var session    = HostContext.AppHost.OnSessionFilter(httpReq, newSession, sessionId) ?? newSession;

            session.PopulateSession(user);
            return(session);
        }
Пример #3
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);
        }
Пример #4
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;
        }
Пример #6
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;
        }
Пример #7
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);
        }
        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;
        }
Пример #9
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);
        }
Пример #10
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);
            }
        }
Пример #11
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);
            }
        }
Пример #12
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);
        }
Пример #13
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;
        }
Пример #15
0
        public static void AuthenticateIfBasicAuth(IRequest req, IResponse res)
        {
            //Need to run SessionFeature filter since its not executed before this attribute (Priority -100)
            SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()

            var userPass = req.GetBasicAuthUserAndPassword();

            if (userPass != null)
            {
                var authService = ((IResolver)req).TryResolve <AuthenticateService>();
                authService.Request = req;
                var response = authService.Post(new Authenticate
                {
                    provider = BasicAuthProvider.Name,
                    UserName = userPass.Value.Key,
                    Password = userPass.Value.Value
                });
            }
        }
Пример #16
0
        public void Register(IAppHost appHost)
        {
            var s = SessionFeature.DefaultSessionFactory() as IWebSudoAuthSession;

            if (s == null)
            {
                throw new NotSupportedException("The IUserAuth session must also implement IWebSudoAuthSession");
            }

            appHost.GlobalRequestFilters.Add(OnRequestStart);
            appHost.GlobalResponseFilters.Add(OnRequestEnd);

            var authFeature = appHost.GetPlugin <AuthFeature>();

            authFeature.AuthEvents.Add(this);

            //appHost.GetPlugin<MetadataFeature>()
            //    ?.AddLink(MetadataFeature.AvailableFeatures, "http://docs.servicestack.net/swagger-api", nameof(WebSudoFeature));
        }
Пример #17
0
        public static async Task <TUserSession> SessionAsAsync <TUserSession>(this IRequest req, CancellationToken token = default)
        {
            if (HostContext.TestMode)
            {
                var mockSession = req.TryResolve <TUserSession>();
                if (!Equals(mockSession, default(TUserSession)))
                {
                    mockSession = req.TryResolve <IAuthSession>() is TUserSession
                        ? (TUserSession)req.TryResolve <IAuthSession>()
                        : default;
                }

                if (!Equals(mockSession, default(TUserSession)))
                {
                    return(mockSession);
                }
            }

            return(await SessionFeature.GetOrCreateSessionAsync <TUserSession>(req.GetCacheClientAsync(), req, req.Response, token).ConfigAwait());
        }
Пример #18
0
        /// <summary>
        /// Typed UserSession
        /// </summary>
        protected virtual TUserSession SessionAs <TUserSession>()
        {
            if (HostContext.TestMode)
            {
                var mockSession = TryResolve <TUserSession>();
                if (Equals(mockSession, default(TUserSession)))
                {
                    mockSession = TryResolve <IAuthSession>() is TUserSession
                        ? (TUserSession)TryResolve <IAuthSession>()
                        : default;
                }

                if (!Equals(mockSession, default(TUserSession)))
                {
                    return(mockSession);
                }
            }

            return(SessionFeature.GetOrCreateSession <TUserSession>(Cache, Request, Response));
        }
Пример #19
0
        public static TUserSession SessionAs <TUserSession>(this IRequest req)
        {
            if (HostContext.TestMode)
            {
                var mockSession = req.TryResolve <TUserSession>();
                if (!Equals(mockSession, default(TUserSession)))
                {
                    mockSession = req.TryResolve <IAuthSession>() is TUserSession
                        ? (TUserSession)req.TryResolve <IAuthSession>()
                        : default(TUserSession);
                }

                if (!Equals(mockSession, default(TUserSession)))
                {
                    return(mockSession);
                }
            }

            return(SessionFeature.GetOrCreateSession <TUserSession>(req.GetCacheClient(), req, req.Response));
        }
        public static IAuthSession GetUntypedSession(this ICacheClient cache,
                                                     IRequest httpReq = null, IResponse httpRes = null)
        {
            var sessionKey = GetSessionKey(httpReq);

            if (sessionKey != null)
            {
                var userSession = cache.Get <IAuthSession>(sessionKey);
                if (!Equals(userSession, default(AuthUserSession)))
                {
                    return(userSession);
                }
            }

            if (sessionKey == null)
            {
                SessionFeature.CreateSessionIds(httpReq, httpRes);
            }

            var unAuthorizedSession = (IAuthSession)typeof(AuthUserSession).CreateInstance();

            return(unAuthorizedSession);
        }
Пример #21
0
        public static async Task <IAuthSession> GetUntypedSessionAsync(this ICacheClientAsync cache,
                                                                       IRequest httpReq = null, IResponse httpRes = null, CancellationToken token = default)
        {
            var sessionKey = GetSessionKey(httpReq);

            if (sessionKey != null)
            {
                var userSession = await cache.GetAsync <IAuthSession>(sessionKey, token);

                if (!Equals(userSession, default(AuthUserSession)))
                {
                    return(userSession);
                }
            }

            if (sessionKey == null)
            {
                SessionFeature.CreateSessionIds(httpReq, httpRes);
            }

            var unAuthorizedSession = (IAuthSession)typeof(AuthUserSession).CreateInstance();

            return(unAuthorizedSession);
        }
Пример #22
0
        public static void AuthenticateIfDigestAuth(IRequest req, IResponse res)
        {
            //Need to run SessionFeature filter since its not executed before this attribute (Priority -100)
            SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()

            var digestAuth = req.GetDigestAuth();

            if (digestAuth != null)
            {
                var authService = ((IResolver)req).TryResolve <AuthenticateService>();
                authService.Request = req;
                var response = authService.Post(new Authenticate
                {
                    provider = DigestAuthProvider.Name,
                    nonce    = digestAuth["nonce"],
                    uri      = digestAuth["uri"],
                    response = digestAuth["response"],
                    qop      = digestAuth["qop"],
                    nc       = digestAuth["nc"],
                    cnonce   = digestAuth["cnonce"],
                    UserName = digestAuth["username"]
                });
            }
        }
Пример #23
0
        public static TimeSpan?GetSessionTimeToLive(this ICacheClient cache, string sessionId)
        {
            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            return(cache.GetTimeToLive(sessionKey));
        }
 public static TUserSession SessionAs <TUserSession>(this ICacheClient cache,
                                                     IRequest httpReq = null, IResponse httpRes = null)
 {
     return(SessionFeature.GetOrCreateSession <TUserSession>(cache, httpReq, httpRes));
 }
        public static string GetSessionKey(IRequest httpReq = null)
        {
            var sessionId = httpReq.GetSessionId();

            return(sessionId == null ? null : SessionFeature.GetSessionKey(sessionId));
        }
        public static string GetOrCreateSessionId(this IRequest httpReq)
        {
            var sessionId = httpReq.GetSessionId();

            return(sessionId ?? SessionFeature.CreateSessionIds(httpReq));
        }
Пример #27
0
 public static Task <TUserSession> SessionAsAsync <TUserSession>(this ICacheClientAsync cache,
                                                                 IRequest httpReq = null, IResponse httpRes = null, CancellationToken token = default)
 {
     return(SessionFeature.GetOrCreateSessionAsync <TUserSession>(cache, httpReq, httpRes, token));
 }
Пример #28
0
 /// <summary>
 /// Typed UserSession
 /// </summary>
 public virtual Task <TUserSession> SessionAsAsync <TUserSession>(CancellationToken token = default)
 {
     return(SessionFeature.GetOrCreateSessionAsync <TUserSession>(CacheAsync, Request, Response, token));
 }
Пример #29
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);
        }
Пример #30
0
 /// <summary>
 /// Typed UserSession
 /// </summary>
 public virtual TUserSession SessionAs <TUserSession>()
 {
     return(SessionFeature.GetOrCreateSession <TUserSession>(Cache, Request, Response));
 }