Ejemplo n.º 1
0
 public static void Init(bool bReload)
 {
     if (!Config.CheckTFA)
     {
         return;
     }
     if ((m_LoadState == TFALoadProcess.Loaded) && !bReload)
     {
         return;
     }
     if (bReload)
     {
         lock (m_TFAReadLock)
         {
             m_LoadState = TFALoadProcess.NotStarted;
         }
     }
     System.Threading.ThreadPool.QueueUserWorkItem(ReadOTPSites);
 }
Ejemplo n.º 2
0
        private static void ReadOTPSites(object s)
        {
            lock (m_TFAReadLock)
            {
                if (m_LoadState == TFALoadProcess.Loading)
                {
                    return;
                }
                if (m_LoadState == TFALoadProcess.Loaded)
                {
                    return;
                }
                if (m_LoadState == TFALoadProcess.FileNotFound)
                {
                    return;
                }
                m_LoadState = TFALoadProcess.Loading;
            }
            m_dTFA.Clear();
            JsonObject       j          = null;
            bool             bException = false;
            IOConnectionInfo ioc        = IOConnectionInfo.FromPath(TFA_JSON_FILE);

            try
            {
                byte[] b = IOConnection.ReadFile(ioc);
                if (b != null)
                {
                    j = new JsonObject(new CharStream(StrUtil.Utf8.GetString(b)));
                }
            }
            catch (Exception ex)
            {
                PluginDebug.AddError("Error reading OTP sites", 0, "Error: " + ex.Message);
                bException = true;
            }
            if (j == null)
            {
                if (!IOConnection.FileExists(ioc))
                {
                    lock (m_TFAReadLock) { m_LoadState = TFALoadProcess.FileNotFound; }
                    Tools.ShowError("Error reading OTP sites: File does not exist\n\n" + TFA_JSON_FILE);
                }
                else
                {
                    lock (m_TFAReadLock) { m_LoadState = TFALoadProcess.Error; }
                }
                if (!bException)
                {
                    PluginDebug.AddError("Error reading OTP sites", 0);
                }
                return;
            }
            DateTime dtStart = DateTime.Now;

            foreach (KeyValuePair <string, object> kvp in j.Items)
            {
                List <string> keys = (kvp.Value as JsonObject).Items.Keys.ToList();
                for (int i = 0; i < keys.Count; i++)
                {
                    JsonObject k          = (kvp.Value as JsonObject).GetValue <JsonObject>(keys[i]);
                    TFAData    tfa        = new TFAData();
                    var        tfaDetails = k.GetValueArray <string>("tfa");
                    tfa.tfa = tfaDetails != null && tfaDetails.Length > 0;
                    if (!tfa.tfa)
                    {
                        continue;
                    }
                    tfa.name     = k.GetValue <string>("name");
                    tfa.sms      = tfaDetails.Contains("sms") || k.GetValue <bool>("sms", false);
                    tfa.email    = tfaDetails.Contains("email") || k.GetValue <bool>("email", false);
                    tfa.phone    = tfaDetails.Contains("phone") || k.GetValue <bool>("phone", false);
                    tfa.software = tfaDetails.Contains("software") || k.GetValue <bool>("software", false);
                    tfa.hardware = tfaDetails.Contains("hardwar") || k.GetValue <bool>("hardware", false);
                    tfa.url      = k.GetValue <string>("url");
                    tfa.img      = k.GetValue <string>("img");
                    tfa.doc      = k.GetValue <string>("doc");
                    tfa.category = kvp.Key;
                    m_dTFA[CreatePattern(tfa.url)] = tfa;
                }
            }
            DateTime dtEnd = DateTime.Now;

            lock (m_TFAReadLock)
            {
                m_LoadState = TFALoadProcess.Loaded;
                PluginDebug.AddInfo("OTP sites read", 0, "Required time: " + (dtEnd - dtStart).ToString(), "Number of sites: " + m_dTFA.Count.ToString());
            }
        }
Ejemplo n.º 3
0
        private static void ReadOTPSites(object s)
        {
            lock (m_TFAReadLock)
            {
                if (m_LoadState == TFALoadProcess.Loading)
                {
                    return;
                }
                if (m_LoadState == TFALoadProcess.Loaded)
                {
                    return;
                }
                m_LoadState = TFALoadProcess.Loading;
            }
            m_dTFA.Clear();
            JsonObject j          = null;
            bool       bException = false;

            try
            {
                IOConnectionInfo ioc = IOConnectionInfo.FromPath("https://twofactorauth.org/data.json");
                byte[]           b   = IOConnection.ReadFile(ioc);
                j = new JsonObject(new CharStream(StrUtil.Utf8.GetString(b)));
            }
            catch (Exception ex)
            {
                PluginDebug.AddError("Error reading OTP sites", 0, "Error: " + ex.Message);
                bException = true;
            }
            if (j == null)
            {
                lock (m_TFAReadLock) { m_LoadState = TFALoadProcess.Error; }
                if (!bException)
                {
                    PluginDebug.AddError("Error reading OTP sites", 0);
                }
                return;
            }
            DateTime dtStart = DateTime.Now;

            foreach (KeyValuePair <string, object> kvp in j.Items)
            {
                List <string> keys = (kvp.Value as JsonObject).Items.Keys.ToList();
                for (int i = 0; i < keys.Count; i++)
                {
                    JsonObject k   = (kvp.Value as JsonObject).GetValue <JsonObject>(keys[i]);
                    TFAData    tfa = new TFAData();
                    tfa.tfa = k.GetValue <bool>("tfa", false);
                    if (!tfa.tfa)
                    {
                        continue;
                    }
                    tfa.name     = k.GetValue <string>("name");
                    tfa.sms      = k.GetValue <bool>("sms", false);
                    tfa.url      = k.GetValue <string>("url");
                    tfa.img      = k.GetValue <string>("img");
                    tfa.email    = k.GetValue <bool>("email", false);
                    tfa.phone    = k.GetValue <bool>("phone", false);
                    tfa.software = k.GetValue <bool>("software", false);
                    tfa.hardware = k.GetValue <bool>("hardware", false);
                    tfa.doc      = k.GetValue <string>("doc");
                    tfa.category = kvp.Key;
                    m_dTFA[CreatePattern(tfa.url)] = tfa;
                }
            }
            DateTime dtEnd = DateTime.Now;

            lock (m_TFAReadLock)
            {
                m_LoadState = TFALoadProcess.Loaded;
                PluginDebug.AddInfo("OTP sites read", 0, "Required time: " + (dtEnd - dtStart).ToString(), "Number of sites: " + m_dTFA.Count.ToString());
            }
        }