/// <exclude />
        public static EntityToken Deserialize(string serialziedEntityToken, bool includeHashValue)
        {
            if (string.IsNullOrEmpty(serialziedEntityToken))
            {
                throw new ArgumentNullException("serialziedEntityToken");
            }

            Dictionary <string, string> dic = StringConversionServices.ParseKeyValueCollection(serialziedEntityToken);

            if (!dic.ContainsKey("entityTokenType") ||
                !dic.ContainsKey("entityToken") ||
                (includeHashValue && !dic.ContainsKey("entityTokenHash")))
            {
                throw new ArgumentException("Failed to deserialize the value. Is has to be searized with EntityTokenSerializer.", "serialziedEntityToken");
            }

            string entityTokenTypeString = StringConversionServices.DeserializeValueString(dic["entityTokenType"]);
            string entityTokenString     = StringConversionServices.DeserializeValueString(dic["entityToken"]);

            if (includeHashValue)
            {
                string entityTokenHash = StringConversionServices.DeserializeValueString(dic["entityTokenHash"]);

                HashValue hashValue = HashValue.Deserialize(entityTokenHash);
                if (!HashSigner.ValidateSignedHash(entityTokenString, hashValue))
                {
                    throw new SecurityException("Serialized entity token is tampered");
                }
            }

            Type entityType = TypeManager.GetType(entityTokenTypeString);

            MethodInfo methodInfo = entityType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static);

            if (methodInfo == null)
            {
                throw new InvalidOperationException(string.Format("The entity token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", entityType, typeof(EntityToken)));
            }


            EntityToken entityToken;

            try
            {
                entityToken = (EntityToken)methodInfo.Invoke(null, new object[] { entityTokenString });
            }
            catch (Exception ex)
            {
                throw new EntityTokenSerializerException("Failed to deserialize entity token '{0}'".FormatWith(entityTokenString), ex);
            }

            if (entityToken == null)
            {
                throw new EntityTokenSerializerException("Deserialization function returned null value. EntityToken: '{0}'".FormatWith(entityTokenString));
            }

            return(entityToken);
        }
        private static EntityToken DeserializeLegacy(string serializedEntityToken, bool includeHashValue)
        {
            var dic = StringConversionServices.ParseKeyValueCollection(serializedEntityToken);

            if (!dic.ContainsKey("entityTokenType") ||
                !dic.ContainsKey("entityToken") ||
                (includeHashValue && !dic.ContainsKey("entityTokenHash")))
            {
                throw new ArgumentException("Failed to deserialize the value. Is has to be serialized with EntityTokenSerializer.", nameof(serializedEntityToken));
            }

            string entityTokenTypeString = StringConversionServices.DeserializeValueString(dic["entityTokenType"]);
            string entityTokenString     = StringConversionServices.DeserializeValueString(dic["entityToken"]);

            if (includeHashValue)
            {
                string entityTokenHash = StringConversionServices.DeserializeValueString(dic["entityTokenHash"]);

                HashValue hashValue = HashValue.Deserialize(entityTokenHash);
                if (!HashSigner.ValidateSignedHash(entityTokenString, hashValue))
                {
                    throw new SecurityException("Serialized entity token is tampered");
                }
            }

            Type entityType = TypeManager.GetType(entityTokenTypeString);

            MethodInfo methodInfo = entityType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static);

            if (methodInfo == null || !(typeof(EntityToken).IsAssignableFrom(methodInfo.ReturnType)))
            {
                throw new InvalidOperationException($"The entity token {entityType} is missing a public static Deserialize method taking a string as parameter and returning an {typeof(EntityToken)}");
            }

            EntityToken entityToken;

            try
            {
                entityToken = (EntityToken)methodInfo.Invoke(null, new object[] { entityTokenString });
            }
            catch (Exception ex)
            {
                throw new EntityTokenSerializerException($"Failed to deserialize entity token '{serializedEntityToken}'", ex);
            }

            if (entityToken == null)
            {
                throw new EntityTokenSerializerException($"Deserialization function returned null value. EntityToken: '{serializedEntityToken}'");
            }

            return(entityToken);
        }
        /// <exclude />
        public static string Serialize(EntityToken entityToken, bool includeHashValue)
        {
            Verify.ArgumentNotNull(entityToken, "entityToken");

            var sb = new StringBuilder();

            StringConversionServices.SerializeKeyValuePair(sb, "entityTokenType", TypeManager.SerializeType(entityToken.GetType()));

            string serializedEntityToken = entityToken.Serialize();

            if (serializedEntityToken == null)
            {
                throw new InvalidCastException(string.Format("'{0}' Serialize returned null", entityToken.GetType()));
            }

            StringConversionServices.SerializeKeyValuePair(sb, "entityToken", serializedEntityToken);

            if (includeHashValue)
            {
                StringConversionServices.SerializeKeyValuePair(sb, "entityTokenHash", HashSigner.GetSignedHash(serializedEntityToken).Serialize());
            }

            return(sb.ToString());
        }
Esempio n. 4
0
        /// <exclude />
        public static ActionToken Deserialize(string serialziedActionToken, bool includeHashValue)
        {
            if (string.IsNullOrEmpty(serialziedActionToken))
            {
                throw new ArgumentNullException("serialziedActionToken");
            }

            Dictionary <string, string> dic = StringConversionServices.ParseKeyValueCollection(serialziedActionToken);

            if ((dic.ContainsKey("actionTokenType") == false) ||
                (dic.ContainsKey("actionToken") == false) ||
                ((includeHashValue) && (dic.ContainsKey("actionTokenHash") == false)))
            {
                throw new ArgumentException("Failed to deserialize the value. It has to be serialized with ActionTokenSerializer class.", "serialziedActionToken");
            }

            string actionTokenTypeString = StringConversionServices.DeserializeValueString(dic["actionTokenType"]);
            string actionTokenString     = StringConversionServices.DeserializeValueString(dic["actionToken"]);

            if (includeHashValue)
            {
                string actionTokenHash = StringConversionServices.DeserializeValueString(dic["actionTokenHash"]);

                HashValue hashValue = HashValue.Deserialize(actionTokenHash);
                if (HashSigner.ValidateSignedHash(actionTokenString, hashValue) == false)
                {
                    throw new SecurityException("Serialized action token is tampered");
                }
            }

            Type actionType = TypeManager.GetType(actionTokenTypeString);

            MethodInfo methodInfo = actionType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static);

            if (methodInfo == null || !(typeof(ActionToken).IsAssignableFrom(methodInfo.ReturnType)))
            {
                Log.LogWarning("ActionTokenSerializer", string.Format("The action token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", actionType, typeof(ActionToken)));
                throw new InvalidOperationException(string.Format("The action token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", actionType, typeof(ActionToken)));
            }


            ActionToken actionToken;

            try
            {
                actionToken = (ActionToken)methodInfo.Invoke(null, new object[] { actionTokenString });
            }
            catch (Exception ex)
            {
                Log.LogWarning("ActionTokenSerializer", string.Format("The action token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", actionType, typeof(ActionToken)));
                Log.LogWarning("ActionTokenSerializer", ex);

                throw new InvalidOperationException(string.Format("The action token {0} is missing a public static Deserialize method taking a string as parameter and returning an {1}", actionType, typeof(ActionToken)), ex);
            }

            if (actionToken == null)
            {
                Log.LogWarning("ActionTokenSerializer", string.Format("public static Deserialize method taking a string as parameter and returning an {1} on the action token {0} did not return an object", actionType, typeof(ActionToken)));

                throw new InvalidOperationException(string.Format("public static Deserialize method taking a string as parameter and returning an {1} on the action token {0} did not return an object", actionType, typeof(ActionToken)));
            }

            return(actionToken);
        }
Esempio n. 5
0
        /// <exclude />
        public static string Serialize(ActionToken actionToken, bool includeHashValue)
        {
            StringBuilder sb = new StringBuilder();

            StringConversionServices.SerializeKeyValuePair(sb, "actionTokenType", TypeManager.SerializeType(actionToken.GetType()));

            string serializedActionToken = actionToken.Serialize();

            StringConversionServices.SerializeKeyValuePair(sb, "actionToken", serializedActionToken);

            if (includeHashValue)
            {
                StringConversionServices.SerializeKeyValuePair(sb, "actionTokenHash", HashSigner.GetSignedHash(serializedActionToken).Serialize());
            }

            return(sb.ToString());
        }