public void Can_retrieve_TimeToLive_on_IAuthSession()
        {
            IAuthSession session = new CustomAuthSession
            {
                Id         = "sess-1",
                UserAuthId = "1",
                Custom     = "custom"
            };

            var sessionKey = SessionFeature.GetSessionKey(session.Id);

            Cache.Remove(sessionKey);

            var ttl = Cache.GetTimeToLive(sessionKey);

            Assert.That(ttl, Is.Null);

            Cache.Set(sessionKey, session);
            ttl = Cache.GetTimeToLive(sessionKey);
            Assert.That(ttl.Value, Is.EqualTo(TimeSpan.MaxValue));

            var sessionExpiry = SessionFeature.DefaultSessionExpiry;

            Cache.Set(sessionKey, session, sessionExpiry);
            ttl = Cache.GetTimeToLive(sessionKey);
            Assert.That(ttl.Value, Is.GreaterThan(TimeSpan.FromSeconds(0)));
            Assert.That(ttl.Value, Is.LessThan(sessionExpiry).
                        Or.EqualTo(sessionExpiry).Within(TimeSpan.FromSeconds(1)));
        }
        public void Can_retrieve_IAuthSession_with_global_ExcludeTypeInfo_set()
        {
            JsConfig.ExcludeTypeInfo = true;

            IAuthSession session = new CustomAuthSession
            {
                Id         = "sess-1",
                UserAuthId = "1",
                Custom     = "custom"
            };

            var sessionKey = SessionFeature.GetSessionKey(session.Id);

            Cache.Set(sessionKey, session, SessionFeature.DefaultSessionExpiry);

            var sessionCache = Cache.Get <IAuthSession>(sessionKey);

            Assert.That(sessionCache, Is.Not.Null);

            var typedSession = sessionCache as CustomAuthSession;

            Assert.That(typedSession, Is.Not.Null);
            Assert.That(typedSession.Custom, Is.EqualTo("custom"));

            JsConfig.Reset();
        }
예제 #3
0
        public object Get(ResetUserAuth request)
        {
            this.Cache.Remove(SessionFeature.GetSessionKey(Request));

            Db.DeleteAll <UserAuth>();
            Db.DeleteAll <UserOAuthProvider>();

            return(HttpResult.Redirect(Request.UrlReferrer.AbsoluteUri));
        }
        //public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
        //{
        //    base.OnAuthenticated(authService, this, tokens, authInfo);
        //    ILog log = LogManager.GetLogger(GetType());

        //    var user = session.ConvertTo<User>();
        //    user.Id = (session as CustomUserSession).Uid;

        //    foreach (var authToken in session.ProviderOAuthAccess)
        //    {
        //        if (authToken.Provider == FacebookAuthProvider.Name)
        //        {
        //            user.UserName = authToken.DisplayName;
        //            user.FirstName = authToken.FirstName;
        //            user.LastName = authToken.LastName;
        //            user.Email = authToken.Email;
        //        }
        //    }
        //}

        public override void OnLogout(IServiceBase authService)
        {
            base.OnLogout(authService);
            using (var cache = authService.TryResolve <ICacheClient>())
            {
                var sessionKey = SessionFeature.GetSessionKey(this.Id);
                cache.Remove(sessionKey);
            }
        }
예제 #5
0
        public bool ValidUpdater(UpdateFulfillment instance, string fulfiller)
        {
            var key         = SessionFeature.GetSessionKey();
            var currentUser = CacheClient.Get <AuthUserSession>(key);

            if (instance.Status == "Completed" && fulfiller != currentUser.UserName)
            {
                return(false);
            }
            return(true);
        }
예제 #6
0
        public AuthUserSession CurrentSession()
        {
            var httpCookie = Request.Cookies["X-ESQTV-SID"];

            if (httpCookie != null)
            {
                var currentSession = httpCookie.Value;
                return(Cache.Get <AuthUserSession>(SessionFeature.GetSessionKey(currentSession)));
            }

            return(null);
        }
예제 #7
0
        public object Get(ResetUserAuth request)
        {
            this.Cache.Remove(SessionFeature.GetSessionKey(Request));

            Db.DeleteAll <UserAuth>();
            Db.DeleteAll <UserAuthDetails>();

            var referrer = Request.UrlReferrer != null
                ? Request.UrlReferrer.AbsoluteUri
                : HostContext.Config.WebHostUrl;

            return(HttpResult.Redirect(referrer));
        }
예제 #8
0
        public object Get(ResetUserAuth request)
        {
            this.Cache.Remove(SessionFeature.GetSessionKey(Request));

            Db.DeleteAll <UserAuth>();
            Db.DeleteAll <UserAuthDetails>();

            var referrer = Request.UrlReferrer != null
                ? Request.UrlReferrer.AbsoluteUri
                : HttpHandlerFactory.GetBaseUrl();

            return(HttpResult.Redirect(referrer));
        }
예제 #9
0
        /// <summary>
        /// Logg off and clear session in service stack.
        /// </summary>
        public void ServiceStackLogOff()
        {
            try
            {
                var httpCookie = Request.Cookies["X-ESQTV-SID"];
                if (httpCookie != null)
                {
                    var currentSession = httpCookie.Value;
                    Cache.Remove(SessionFeature.GetSessionKey(currentSession));
                }
            }

            catch { }
        }
예제 #10
0
 public static void AddGuidRecordsToCache <T>(IWebEasRepositoryBase repository, IEnumerable <KeyValuePair <string, string> > recordsToCache, string hashId = null)
 {
     if (recordsToCache.Any())
     {
         hashId ??= UrnId.Create <T>(repository.Session.Id);
         repository.Redis.Remove(hashId);
         repository.Redis.SetRangeInHash(hashId, recordsToCache);
         var sessionKey = SessionFeature.GetSessionKey(repository.Session.Id);
         var ttl        = repository.Redis.GetTimeToLive(sessionKey);
         if (ttl.HasValue)
         {
             repository.Redis.ExpireEntryIn(hashId, ttl.Value);
         }
     }
 }
예제 #11
0
 protected bool IsUserAuthorized(IRequest thisRequest)
 {
     try
     {
         // Within the hub itself we can get the request directly from the context.
         //Microsoft.AspNet.SignalR.IRequest myRequest = this.Context.Request; // Unfortunately this is a signalR IRequest, not a ServiceStack IRequest, but we can still use it to get the cookies.
         bool              perm       = thisRequest.Cookies["ss-opt"].Value == "perm";
         string            sessionID  = perm ? thisRequest.Cookies["ss-pid"].Value : thisRequest.Cookies["ss-id"].Value;
         var               sessionKey = SessionFeature.GetSessionKey(sessionID);
         CustomUserSession session    = HostContext.Cache.Get <CustomUserSession>(sessionKey);
         return(session.IsAuthenticated);
     }
     catch (Exception ex)
     {
         // probably not auth'd so no cookies, session etc.
     }
     return(false);
 }
예제 #12
0
        public void Can_GetKeysByPattern()
        {
            if (!(Cache is ICacheClientExtended))
            {
                return;
            }

            ((PocoDynamo)((DynamoDbCacheClient)Cache).Dynamo).PagingLimit = 2;

            JsConfig.ExcludeTypeInfo = true;

            5.Times(i => {
                IAuthSession session = new CustomAuthSession
                {
                    Id         = "sess-" + i,
                    UserAuthId = i.ToString(),
                    Custom     = "custom" + i
                };

                var sessionKey = SessionFeature.GetSessionKey(session.Id);
                Cache.Set(sessionKey, session, SessionFeature.DefaultSessionExpiry);
                Cache.Set("otherkey" + i, i);
            });

            var sessionPattern = IdUtils.CreateUrn <IAuthSession>("");

            Assert.That(sessionPattern, Is.EqualTo("urn:iauthsession:"));
            var sessionKeys = Cache.GetKeysStartingWith(sessionPattern).ToList();

            Assert.That(sessionKeys.Count, Is.EqualTo(5));
            Assert.That(sessionKeys.All(x => x.StartsWith("urn:iauthsession:")));

            var allSessions = Cache.GetAll <IAuthSession>(sessionKeys);

            Assert.That(allSessions.Values.Count(x => x != null), Is.EqualTo(sessionKeys.Count));

            var allKeys = Cache.GetAllKeys().ToList()
                          .Where(x => x.StartsWith("urn:iauthsession:") || x.StartsWith("otherkey")).ToList();

            Assert.That(allKeys.Count, Is.EqualTo(10));

            JsConfig.Reset();
        }
예제 #13
0
        public async Task Can_GetKeysByPattern()
        {
            if (!(Cache is ICacheClientExtended))
            {
                return;
            }

            JsConfig.ExcludeTypeInfo = true;

            for (int i = 0; i < 5; i++)
            {
                IAuthSession session = new CustomAuthSession
                {
                    Id         = "sess-" + i,
                    UserAuthId = i.ToString(),
                    Custom     = "custom" + i
                };

                var sessionKey = SessionFeature.GetSessionKey(session.Id);
                await Cache.SetAsync(sessionKey, session, SessionFeature.DefaultSessionExpiry);

                await Cache.SetAsync("otherkey" + i, i);
            }

            var sessionPattern = IdUtils.CreateUrn <IAuthSession>("");

            Assert.That(sessionPattern, Is.EqualTo("urn:iauthsession:"));
#if !NETFX
            var sessionKeys = await Cache.GetKeysStartingWithAsync(sessionPattern).ToListAsync();

            Assert.That(sessionKeys.Count, Is.EqualTo(5));
            Assert.That(sessionKeys.All(x => x.StartsWith("urn:iauthsession:")));

            var allSessions = await Cache.GetAllAsync <IAuthSession>(sessionKeys);

            Assert.That(allSessions.Values.Count(x => x != null), Is.EqualTo(sessionKeys.Count));

            var allKeys = (await Cache.GetAllKeysAsync().ToListAsync()).ToList();
            Assert.That(allKeys.Count, Is.EqualTo(10));
#endif
            JsConfig.Reset();
        }
예제 #14
0
        public async Task Can_retrieve_IAuthSession()
        {
            IAuthSession session = new CustomAuthSession
            {
                Id         = "sess-1",
                UserAuthId = "1",
                Custom     = "custom"
            };

            var sessionKey = SessionFeature.GetSessionKey(session.Id);
            await Cache.SetAsync(sessionKey, session, SessionFeature.DefaultSessionExpiry);

            var sessionCache = await Cache.GetAsync <IAuthSession>(sessionKey);

            Assert.That(sessionCache, Is.Not.Null);

            var typedSession = sessionCache as CustomAuthSession;

            Assert.That(typedSession, Is.Not.Null);
            Assert.That(typedSession.Custom, Is.EqualTo("custom"));
        }
예제 #15
0
        public void Can_retrieve_IAuthSession()
        {
            IAuthSession session = new CustomAuthSession
            {
                Id         = "sess-1",
                UserAuthId = "1",
                Custom     = "custom"
            };

            var sessionKey = SessionFeature.GetSessionKey(session.Id);

            Cache.Set(sessionKey, session, HostContext.GetDefaultSessionExpiry());

            var sessionCache = Cache.Get <IAuthSession>(sessionKey);

            Assert.That(sessionCache, Is.Not.Null);

            var typedSession = sessionCache as CustomAuthSession;

            Assert.That(typedSession, Is.Not.Null);
            Assert.That(typedSession.Custom, Is.EqualTo("custom"));
        }
        public bool ProcessQueue(string tenantId)
        {
            var listId          = GetQueueId(tenantId);
            var queueHashId     = GetHashId(tenantId);
            var lockKey         = GetListLockKey(tenantId);
            var processedHashId = GetProcessedHashId(tenantId);

            bool result = false;

            using (var redisClient = redisManager.GetClient())
            {
                var firstMessageId = redisClient.GetItemFromList(listId, 0);
                if (!string.IsNullOrEmpty(firstMessageId))
                {
                    var msg = redisClient.GetValueFromHash(queueHashId, firstMessageId).FromJson <IsoOperationMessage>();
                    if (msg == null || string.IsNullOrEmpty(msg.Error))
                    {
                        //spracoval to iny server
                        if (msg == null)
                        {
                            ProcessList(listId, lockKey, redisClient, firstMessageId);
                            result = true;
                        }
                        else
                        {
                            if (msg.Confirmed.HasValue)
                            {
                                ProcessList(listId, lockKey, redisClient, firstMessageId);
                                redisClient.RemoveEntryFromHash(queueHashId, firstMessageId);
                                redisClient.SetEntryInHash(processedHashId, firstMessageId, msg.ToJson());
                                result = true;
                            }
                            else
                            {
                                var canResend = msg.ReSendCount <resendCount && msg.SentDate.HasValue && (DateTime.Now - msg.SentDate.Value).TotalSeconds> secondsBetweenResend;
                                var modul = GetModule(msg.Operation);

                                if (!msg.Sent || canResend)
                                {
                                    var subscriptions = serverEvents.GetSubscriptionInfosByUserId("00000000-0000-0000-0000-000000000002");
                                    if (subscriptions.Any(x => x.Channels != null && x.Channels.Contains(modul)))
                                    {
                                        foreach (var sub in subscriptions.Where(x => x.Channels != null && x.Channels.Contains(modul)))
                                        {
                                            var subSessionKey = SessionFeature.GetSessionKey(sub.SessionId);
                                            var subSession    = redisClient.Get <EsamSession>(subSessionKey);

                                            if (subSession != null && subSession.TenantId == tenantId)
                                            {
                                                if (msg.Sent)
                                                {
                                                    msg.ReSendCount += 1;
                                                }

                                                //serverEvents.NotifySubscription(sub.SubscriptionId, msg.Operation.ToString().ReplaceFirst("_", "."), msg.Data, modul);
                                                msg.Sent     = true;
                                                msg.SentDate = DateTime.Now;

                                                if (msg.ReSendCount >= resendCount)
                                                {
                                                    msg.Error = "Spravu nie je mozne dorucit";
                                                }

                                                redisClient.SetEntryInHash(queueHashId, firstMessageId, msg.ToJson());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (result)
            {
                return(ProcessQueue(tenantId));
            }

            return(result);
        }
예제 #17
0
 public void ClearSession()
 {
     userSession = null;
     this.Cache.Remove(SessionFeature.GetSessionKey());
 }
예제 #18
0
 public void ClearSession()
 {
     userSession = null;
     this.Cache.Remove(SessionFeature.GetSessionKey(HostContext.GetCurrentRequest()));
 }