Beispiel #1
0
        /// <summary>
        /// Gets a session record from an XSRF token
        /// </summary>
        /// <returns>The session record.</returns>
        /// <param name="xsrf">The XSRF token.</param>
        public async Task <SessionRecord> GetSessionFromXSRFAsync(string xsrf)
        {
            string txt;

            using (await m_lock.LockAsync())
                m_xsrf_storage.TryGetValue(xsrf, out txt);

            if (string.IsNullOrWhiteSpace(txt))
            {
                return(null);
            }

            return(PrimitiveSerializer.Deserialize <SessionRecord>(txt));
        }
Beispiel #2
0
        /// <summary>
        /// Called periodically to expire old items
        /// </summary>
        /// <returns>An awaitable task.</returns>
        public async Task ExpireOldItemsAsync()
        {
            using (await m_lock.LockAsync())
            {
                var to_remove = new List <string>();

                foreach (var e in m_cookie_storage)
                {
                    var ds = PrimitiveSerializer.Deserialize <SessionRecord>(e.Value);
                    if (Utility.IsNullOrExpired(ds))
                    {
                        to_remove.Add(e.Key);

                        if (!string.IsNullOrWhiteSpace(ds.XSRFToken))
                        {
                            m_xsrf_storage.Remove(ds.XSRFToken);
                        }
                    }
                }

                foreach (var r in to_remove)
                {
                    m_cookie_storage.Remove(r);
                }

                to_remove.Clear();

                foreach (var e in m_xsrf_storage)
                {
                    var ds = PrimitiveSerializer.Deserialize <SessionRecord>(e.Value);
                    if (Utility.IsNullOrExpired(ds))
                    {
                        to_remove.Add(e.Key);

                        if (!string.IsNullOrWhiteSpace(ds.Cookie))
                        {
                            m_cookie_storage.Remove(ds.Cookie);
                        }
                    }
                }

                foreach (var r in to_remove)
                {
                    m_xsrf_storage.Remove(r);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Updates the expiration time on the given session record
        /// </summary>
        /// <returns>An awaitable task.</returns>
        /// <param name="record">The record to update.</param>
        public async Task UpdateSessionExpirationAsync(SessionRecord record)
        {
            var txt = PrimitiveSerializer.Serialize(record);

            using (await m_lock.LockAsync())
            {
                if (!string.IsNullOrWhiteSpace(record.XSRFToken))
                {
                    m_xsrf_storage[record.XSRFToken] = txt;
                }

                if (!string.IsNullOrWhiteSpace(record.Cookie))
                {
                    m_cookie_storage[record.Cookie] = txt;
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Adds a new session record
        /// </summary>
        /// <returns>An awaitable task.</returns>
        /// <param name="record">The record to add.</param>
        public async Task AddSessionAsync(SessionRecord record)
        {
            var txt = PrimitiveSerializer.Serialize(record);

            using (await m_lock.LockAsync())
            {
                if (!string.IsNullOrWhiteSpace(record.XSRFToken))
                {
                    if (m_xsrf_storage.ContainsKey(record.XSRFToken))
                    {
                        throw new ArgumentException("Attempted to re-insert an entry into the XSRF token table");
                    }

                    if (!string.IsNullOrWhiteSpace(record.Cookie) && m_cookie_storage.ContainsKey(record.Cookie))
                    {
                        throw new ArgumentException("Attempted to re-insert an entry into the cookie token table");
                    }

                    m_xsrf_storage[record.XSRFToken] = txt;
                    m_cookie_storage[record.Cookie]  = txt;
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(record.Cookie))
                    {
                        throw new ArgumentException("Attempted to inser a record that has neither XSRF nor a cookie");
                    }

                    if (m_cookie_storage.ContainsKey(record.Cookie))
                    {
                        throw new ArgumentException("Attempted to re-insert an entry into the cookie token table");
                    }

                    m_cookie_storage[record.Cookie] = txt;
                }
            }
        }