Exemplo n.º 1
0
        //------------------------------------------------------------------------------
        public static object MakeDynamicSetting(SettingInfo inSettingInfo)
        {
            var simpleValue = SettingsSerializerHelper.SimplifyObject(inSettingInfo.SettingValue);
            var stringValue = SettingsSerializerHelper.ConvertUsingTypeConverter <String>(simpleValue);

            return(stringValue);
        }
Exemplo n.º 2
0
        //------------------------------------------------------------------------------
        public static Tuple <object /*value*/, Type /*destinationType*/, object /*defaultValue*/> CorrectValueType(object value,
                                                                                                                   Type destinationType,
                                                                                                                   object defaultValue,
                                                                                                                   FunctionalityHook functionalityHook)
        {
            Tuple <object, Type, object> result;

            try
            {
                if (functionalityHook != null && functionalityHook.TypeCorrector != null)
                {
                    result = functionalityHook.TypeCorrector.CorrectType(value, destinationType, defaultValue);
                }
                else
                {
                    object settingValue = SettingsSerializerHelper.CorrectSettingType(value, destinationType);
                    result = Tuple.Create(settingValue, destinationType, defaultValue);
                }
            }
            catch (Exception exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error, exc);

                //throw;
                result = Tuple.Create(defaultValue, destinationType, defaultValue);
            }

            return(result);
        }
Exemplo n.º 3
0
        public static NSObject ToNSObject(this Object obj)
        {
            var simpleObj = SettingsSerializerHelper.SimplifyObject(obj);

            Debug.Assert(simpleObj != null);

            return(NSObject.FromObject(simpleObj));
        }
        //------------------------------------------------------------------------------
        public void Save(string settingName, SettingBaseAttribute attr, object value)
        {
            var simpleValue = SettingsSerializerHelper.SimplifyObject(value);
            var stringValue = SettingsSerializerHelper.ConvertUsingTypeConverter <String>(simpleValue);

            SecStatusCode eCode = KeyChainUtils.SetPasswordForUsername(
                settingName,
                stringValue,
                m_serviceName,
                m_secAccessible,
                m_synchronizable);

            if (eCode == SecStatusCode.Success)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Info,
                                                         "KeyChainSerializer.Save - success",
                                                         settingName);
            }
            else
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                                                         "KeyChainSerializer.Save - error",
                                                         String.Format("Setting name is {0}. Error code is {1}",
                                                                       settingName,
                                                                       eCode));
            }

            // keychain error handling from Xamarin.Auth
            // https://github.com/xamarin/Xamarin.Auth/blob/master/source/Core/Xamarin.Auth.XamarinIOS/AccountStore/KeyChainAccountStore.Aync.cs
            if (eCode != SecStatusCode.Success)
            {
                StringBuilder sb = new StringBuilder("error = ");
                sb.AppendLine("Could not save value to KeyChain: " + eCode);
                sb.AppendLine("Add Empty Entitlements.plist ");
                sb.AppendLine("File /+ New file /+ iOS /+ Entitlements.plist");

                /*
                 *  Error: Could not save account to KeyChain -- iOS 10 #128
                 *  https://github.com/xamarin/Xamarin.Auth/issues/128
                 *  https://bugzilla.xamarin.com/show_bug.cgi?id=43514
                 */
                if ((int)eCode == -34018)
                {
                    // http://stackoverflow.com/questions/38456471/secitemadd-always-returns-error-34018-in-xcode-8-in-ios-10-simulator
                    // NOTE: code was not copy/pasted! That was iOS sample

                    sb.AppendLine("SecKeyChain.Add returned : " + eCode);
                    sb.AppendLine("1. Add Keychain Access Groups to the Entitlements file.");
                    sb.AppendLine("2. Turn on the Keychain Sharing switch in the Capabilities section in the app.");
                }

                string msg = sb.ToString();

                throw new ExceptionForUser(new Exception(msg));
            }
        }
Exemplo n.º 5
0
        //------------------------------------------------------------------------------
        IEnumerable <string> LoadDynamicSettingsOrderImpl(string settingsOrderFullName, ISettingsSerializer serializer, Type settingsType)
        {
            try
            {
                var inSettingInfo = new SettingInfo()
                {
                    SettingName = settingsOrderFullName,
                };

                var settingsHelperData = new SettingsHelperData()
                {
                    SerializersCache  = new Dictionary <Type, KeyValuePair <ISettingsSerializer, bool> >(),
                    SerializersToSync = new OrderedSet <ISettingsSerializer>(),
                };

                var outSettingInfo = ProcessArbitraryDynamicSetting(inSettingInfo, SettingsEnumerationMode.Load, ref settingsHelperData);

                SettingsHelper.SynchronizeSerializers(settingsHelperData.SerializersToSync);

                if (outSettingInfo == null)
                {
                    return(null);
                }

                var settingsNamesOrder = SettingsSerializerHelper.ConvertUsingTypeConverter <string>(outSettingInfo.SettingValue);

                settingsNamesOrder = settingsNamesOrder.Trim();

                var settingsOrderResult = new List <String>();

                if (String.IsNullOrEmpty(settingsNamesOrder))
                {
                    return(settingsOrderResult);
                }

                var parts = settingsNamesOrder.Split(',');
                foreach (var settingName in parts)
                {
                    var trimSettingName = settingName.Trim();

                    settingsOrderResult.Add(trimSettingName);
                }

                return(settingsOrderResult);
            }
            catch
            {
                // return null in case of any error
            }

            return(null);
        }
Exemplo n.º 6
0
        //------------------------------------------------------------------------------
        public void Save(string settingName, SettingBaseAttribute attr, object value)
        {
            var simpleObj = SettingsSerializerHelper.SimplifyObject(value);

            var prefs   = GetUserDefaults();
            var nsValue = simpleObj.ToNSObject();

            prefs[settingName] = nsValue;

            InternalConfiguration.PlatformHelper.Log(LogLevel.Info,
                                                     "UserDefaultsSerializer.Save - success",
                                                     String.Format("Setting name is {0}; Value is {1}",
                                                                   settingName,
                                                                   nsValue != null ? nsValue.ToString() : "null"));
        }
Exemplo n.º 7
0
        //------------------------------------------------------------------------------
        static public object CorrectSettingType(object originalValue, Type destinationType)
        {
            Debug.Assert(destinationType != null);

            if (originalValue == null)
            {
                return(null);
            }

            if (destinationType == typeof(DateTime) && originalValue.GetType() == destinationType)
            {
                return(originalValue);
            }

            if (IsGenericNullable(destinationType))
            {
                destinationType = GetUnderlyingType(destinationType);
            }

            originalValue =
                InternalConfiguration.PlatformHelper.ToUnderlyingObject(originalValue, destinationType);

            String strValue = originalValue as String;

            if (strValue != null && strValue.IsBase64String())
            {
                try
                {
                    // try to deserialize string value to smthng else
                    originalValue = DeserializeToWrapperObjectValue(strValue);
                    return(originalValue);
                }
                catch (Polenter.Serialization.Core.DeserializingException)
                {
                }
                catch (System.FormatException)
                {
                }
                catch (System.IO.EndOfStreamException)
                {
                }
            }

            var result = SettingsSerializerHelper.ConvertUsingTypeConverter(destinationType, originalValue);

            return(result);
        }
Exemplo n.º 8
0
        //------------------------------------------------------------------------------
        T GetSettingImpl <T>(string settingName, T defaultValue, bool useInternalDefaultValue)
        {
            lock (m_lockObject)
            {
                var fullSettingName = CorrectDynamicSettingName(settingName);

                object settingValue = null;

                DynamicSettingInfo dsi;
                bool exists = m_dynamicSettings.TryGetValue(fullSettingName, out dsi);

                if (exists)
                {
                    settingValue = dsi.SettingValue;

                    try
                    {
                        T convertedSettingValue = SettingsSerializerHelper.CorrectSettingType <T>(settingValue);

                        return(convertedSettingValue);
                    }
                    catch (Exception exc)
                    {
                        var msg = String.Format("Dynamic setting '{0}' type conversion exception", settingName);
                        InternalConfiguration.PlatformHelper.Log(LogLevel.Warning, msg, exc);
                    }
                }

                if (useInternalDefaultValue)
                {
                    if (m_dynamicSettingsDefaultValues == null)
                    {
                        var msg = String.Format("Set default value for setting '{0}'", settingName);
                        throw new KeyNotFoundException(msg);
                    }

                    object objDefaultValue = m_dynamicSettingsDefaultValues[fullSettingName];
                    defaultValue = SettingsSerializerHelper.CorrectSettingType <T>(objDefaultValue);
                }
                else
                {
                    // in this case T defaultValue is valid value
                }

                return(defaultValue);
            }
        }
Exemplo n.º 9
0
        //------------------------------------------------------------------------------
        public static object GetDefaultValue(SettingBaseAttribute attr, Type settingType)
        {
            object defaultValue = null;

            try
            {
                if (attr != null && attr.DefaultValueInUse)
                {
                    defaultValue = attr.Default;

                    if (defaultValue is DefaulValueMode)
                    {
                        var defaultValueMode = (DefaulValueMode)defaultValue;
                        switch (defaultValueMode)
                        {
                        case DefaulValueMode.TypeDefaultValue:
                        {
                            defaultValue = GetTypeDefaultValue(settingType);
                            break;
                        }

                        default:
                        {
                            throw new NotImplementedException("Unknown DefaultValueMode");
                        }
                        }
                    }
                    else
                    {
                        defaultValue = SettingsSerializerHelper.ConvertUsingTypeConverter(settingType, defaultValue);
                    }
                }
                else
                {
                    defaultValue = GetTypeDefaultValue(settingType);
                }
            }
            catch (Exception exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error, "GetDefaultValue exception", exc);
                InternalConfiguration.PlatformHelper.Log(LogLevel.Info, "Use type default value instead");
                defaultValue = GetTypeDefaultValue(settingType);
            }

            return(defaultValue);
        }
        //------------------------------------------------------------------------------
        public void Save(string settingName, SettingBaseAttribute attr, object value)
        {
            settingName = SettingNamePrefix + settingName;

            var simpleValue = SettingsSerializerHelper.SimplifyObject(value);
            var stringValue = SettingsSerializerHelper.ConvertUsingTypeConverter <String>(simpleValue);

            var binaryData = Encoding.ASCII.GetBytes(stringValue);

            var entropy       = SettingsConfiguration.SecureSettingsAdditionalEntropy;
            var encryptedData =
                ProtectedData.Protect(binaryData, entropy, SettingsConfiguration.SecureSettingsScope);

            string encryptedBase64StringData = Convert.ToBase64String(encryptedData);

            m_Store.Save(settingName, attr, encryptedBase64StringData);
        }
Exemplo n.º 11
0
        //------------------------------------------------------------------------------
        static public Object SimplifyObject(Object obj)
        {
            Type type   = null;
            bool isEnum = false;

            if (obj != null)
            {
                type   = obj.GetType();
                isEnum = type.GetTypeInfo().IsEnum;
            }


            if (type == typeof(Boolean) ||
                type == typeof(Char) ||
                type == typeof(SByte) ||
                type == typeof(Byte) ||
                type == typeof(Int16) ||
                type == typeof(UInt16) ||
                type == typeof(Int32) ||
                type == typeof(UInt32) ||
                type == typeof(Int64) ||
                type == typeof(UInt64) ||
                type == typeof(String) ||
                isEnum
                // this values will be saved with wrapper
                //type == typeof(Single)
                //type == typeof(Double)
                //type == typeof(Decimal)
                )
            {
            }
            else if (type == typeof(DateTime))
            {
                obj = ((DateTime)obj).ToString("o", CultureInfo.InvariantCulture);
            }
            else
            {
                // in case if object is null, custom object or smthng else
                // save it to string with wrapper
                obj = SettingsSerializerHelper.SaveToStringWithWrapperObject(obj);
            }

            return(obj);
        }
Exemplo n.º 12
0
        //------------------------------------------------------------------------------
        public void Save(string settingName, SettingBaseAttribute attr, object value)
        {
            var simpleValue = SettingsSerializerHelper.SimplifyObject(value);
            var stringValue = SettingsSerializerHelper.ConvertUsingTypeConverter <String>(simpleValue);

            using (var stream = m_Store.OpenFile(settingName, FileMode.Create, FileAccess.Write))
            {
                using (var sw = new StreamWriter(stream))
                {
                    sw.Write(stringValue);
                }
            }

            InternalConfiguration.PlatformHelper.Log(LogLevel.Info,
                                                     "IsolatedStorageSerializer.Save - success",
                                                     String.Format("setting is {0}, value is {1}",
                                                                   settingName,
                                                                   stringValue != null ? stringValue : "null"));
        }
        //------------------------------------------------------------------------------
        public void Save(string settingName, SettingBaseAttribute attr, object value)
        {
            if (m_editor == null)
            {
                var prefs = GetSharedPreferences();
                m_editor = prefs.Edit();
            }

            object saveValue = value;

            if (value is bool)
            {
                m_editor.PutBoolean(settingName, (bool)value);
            }
            else if (value is int)
            {
                m_editor.PutInt(settingName, (int)value);
            }
            else if (value is long)
            {
                m_editor.PutLong(settingName, (long)value);
            }
            else if (value is float)
            {
                m_editor.PutFloat(settingName, (float)value);
            }
            else
            {
                var simpleValue = SettingsSerializerHelper.SimplifyObject(value);
                var stringValue = SettingsSerializerHelper.ConvertUsingTypeConverter <String>(simpleValue);

                saveValue = stringValue;

                m_editor.PutString(settingName, stringValue);
            }

            InternalConfiguration.PlatformHelper.Log(LogLevel.Info,
                                                     "SharedPreferencesSerializer.Save - success",
                                                     String.Format("Setting is {0}. Value is {1}",
                                                                   settingName,
                                                                   saveValue != null ? saveValue.ToString() : "null"));
        }
        //------------------------------------------------------------------------------
        public void Save(string settingName, SettingBaseAttribute attr, object value)
        {
            var simpleValue = SettingsSerializerHelper.SimplifyObject(value);
            var stringValue = SettingsSerializerHelper.ConvertUsingTypeConverter <String>(simpleValue);

            bool status = m_keyChainUtils.SaveKey(settingName, stringValue);

            if (status)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Info,
                                                         "KeyChainSerializer.Save - success",
                                                         settingName);
            }
            else
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                                                         "KeyChainSerializer.Save - error",
                                                         settingName);
            }
        }
        //------------------------------------------------------------------------------
        public bool Load(string settingName, SettingBaseAttribute attr, out object value)
        {
            settingName = SettingNamePrefix + settingName;

            object encryptedValue;
            bool   loaded = m_Store.Load(settingName, attr, out encryptedValue);

            if (loaded)
            {
                var    encryptedBase64StringData = SettingsSerializerHelper.ConvertUsingTypeConverter <String>(encryptedValue);
                byte[] encryptedBytes            = Convert.FromBase64String(encryptedBase64StringData);

                var entropy        = SettingsConfiguration.SecureSettingsAdditionalEntropy;
                var decryptedValue = ProtectedData.Unprotect(encryptedBytes, entropy, SettingsConfiguration.SecureSettingsScope);

                value = Encoding.ASCII.GetString(decryptedValue);
            }
            else
            {
                value = null;
            }

            return(loaded);
        }
Exemplo n.º 16
0
        //------------------------------------------------------------------------------
        static public T CorrectSettingType <T>(object originalValue)
        {
            T result = (T)SettingsSerializerHelper.CorrectSettingType(originalValue, typeof(T));

            return(result);
        }
Exemplo n.º 17
0
        //------------------------------------------------------------------------------
        public Tuple <object, Type, object> CorrectType(object value, Type destinationType, object defaultValue)
        {
            var objDestinationValue = SettingsSerializerHelper.CorrectSettingType(value, typeof(object));

            return(Tuple.Create <object, Type, object>(objDestinationValue, typeof(object), null));
        }