/// <summary>
        /// Gets the authentication token asynchronous.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.Exception">No existing auth token in DB</exception>
        public async Task <TokenBaerer> GetAuthTokenAsync()
        {
            using (RavenManager rm = RavenManager.Instance)
            {
                var token = await rm.GetLatestTokenAsync();

                return(token);
            }
        }
        /// <summary>
        /// Gets the token form database.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.Exception">No existing auth token in DB</exception>
        private TokenBaerer GetTokenFormDb()
        {
            using (RavenManager rm = RavenManager.Instance)
            {
                token = rm.GetLatestToken();

                //if (token == null)
                //    throw new Exception("No existing auth token in DB");
            }
            return(token);
        }
        /// <summary>
        /// Refreshes the token.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.Exception">No existing auth token in DB</exception>
        public TokenBaerer RefreshToken()
        {
            using (RavenManager rm = RavenManager.Instance)
            {
                TokenBaerer oldToken = rm.GetLatestToken();
                if (oldToken == null)
                {
                    throw new Exception("No existing auth token in DB");
                }

                token = authManager.RefreshToken(oldToken.RefreshToken);

                token.Id          = oldToken.Id;
                token.RealmId     = oldToken.RealmId;
                token.ExpiaryDate = oldToken.ExpiaryDate;

                rm.UpdateToken(token);
                return(token);
            }
        }
        /// <summary>
        /// Gets the new token.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="code">The code.</param>
        /// <param name="realmId">The realm identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">
        /// Error in getting token
        /// or
        /// CSRF validation fail
        /// </exception>
        public async Task <TokenBaerer> GetNewTokenAsync(string state, string code, string realmId)
        {
            string csrf    = authManager.GetCSRFToken();
            bool   isValid = string.Equals(csrf, state);

            if (isValid)
            {
                var token = await authManager.GetTokensAsync(code, realmId);

                if (token == null)
                {
                    throw new Exception("Error in getting token");
                }

                // store token in raven db
                using (RavenManager rm = RavenManager.Instance)
                {
                    // get existing token form raven
                    var oldToken = await rm.GetLatestTokenAsync();

                    // if exist update that token
                    if (oldToken != null)
                    {
                        token.Id = oldToken.Id;
                        await rm.UpdateTokenAsync(token);
                    }
                    // if not store token
                    else
                    {
                        await rm.StoreTokenAsync(token);
                    }
                }
                return(token);
            }
            else
            {
                throw new Exception("CSRF validation fail");
            }
        }