示例#1
0
        protected void ParseErrors(Object response)
        {
            List <Dictionary <String, Object> > tmpList = new List <Dictionary <String, Object> >();

            if (response is List <Object> )
            {
                tmpList.AddRange(SmartMap.CastToListOfDictionary(response));
            }
            else
            {
                tmpList.Add(SmartMap.CastToDictionary(response));
            }

            foreach (Dictionary <String, Object> tmpErrorMap in tmpList)
            {
                SmartMap tmpCaseInsensitiveMap = new SmartMap(tmpErrorMap, true);
                try {
                    if (tmpCaseInsensitiveMap.ContainsKey("Errors.Error.Description"))
                    {
                        //errors object with a list of error object
                        Dictionary <String, Object> tmpErrorObj = (Dictionary <String, Object>)tmpCaseInsensitiveMap.Get("Errors.Error");
                        AddError(tmpErrorObj);
                        continue;
                    }
                } catch (Exception) {
                }

                try {
                    if (tmpCaseInsensitiveMap.ContainsKey("Errors.Error[0].Description"))
                    {
                        //errors object with a list of error object
                        List <Dictionary <String, Object> > tmpErrorList = (List <Dictionary <String, Object> >)tmpCaseInsensitiveMap.Get("Errors.Error");
                        AddError(tmpErrorList);
                        continue;
                    }
                } catch (Exception) {
                }

                try {
                    if (tmpCaseInsensitiveMap.ContainsKey("Errors[0].Description"))
                    {
                        List <Dictionary <String, Object> > tmpErrorList = (List <Dictionary <String, Object> >)tmpCaseInsensitiveMap.Get("Errors");
                        AddError(tmpErrorList);
                        continue;
                    }
                } catch (Exception) {
                }

                try {
                    if (tmpCaseInsensitiveMap.ContainsKey("Description"))
                    {
                        AddError(tmpErrorMap);
                        continue;
                    }
                } catch (Exception) {
                }
            }
        }
示例#2
0
        public void TestJson2()
        {
            String tmpDict = "{\n  \"mapName\": \"name\",\n  \"list\": [\n    {\n      \"itemId\": 1,\n      \"name\": \"name\",\n      \"list\": [\n         1, 2, 3, 4  \n      ]\n    },\n    {\n      \"itemId\": 2,\n      \"name\": \"name\",\n      \"list\": [\n         1, 2, 3, 4  \n      ]\n    },\n    {\n      \"itemId\": 3,\n      \"name\": \"name\",\n      \"list\": [\n         1, 2, 3, 4  \n      ]\n    }\n  ]\n}";

            SmartMap dict = new SmartMap(tmpDict);

            Assert.IsTrue(dict.ContainsKey("mapName"));
            Assert.IsTrue(dict.ContainsKey("list"));

            Assert.AreSame(typeof(List <Dictionary <String, Object> >), dict ["list"].GetType());
        }
示例#3
0
 private void InitializeMap()
 {
     gameMap = new SmartMap(length, width, new MapFloor(), new ConcreteCube());
     gameMap.AddElements <RandomPlacementOnEmptyPosition>(new BreakCube(), breakCubesCount);
     gameMap.AddElements <BonusesRandomPlacement>(GameFactory.CreateBonusSpeed(), bonusSpeedCount);
     gameMap.AddElements <BonusesRandomPlacement>(GameFactory.CreateBonusBombs(), bonusBombsCount);
     gameMap.AddElements <BonusesRandomPlacement>(GameFactory.CreateBonusFlames(), bonusFlamesCount);
     gameMap.AddElements <BonusesRandomPlacement>(GameFactory.CreateBonusWallpass(), bonusWallpassCount);
     gameMap.AddElements <BonusesRandomPlacement>(GameFactory.CreateBonusDetonator(), bonusDetonatorCount);
     gameMap.AddElements <RandomPlacementOnEmptyPosition>(GameFactory.CreateEasyEnemy(), easyEnemiesCount);
     gameMap.AddElements <RandomPlacementOnEmptyPosition>(GameFactory.CreateHardEnemy(gameMap.Field), hardEnemyCount);
     gameMap.CreateAllElements();
 }
示例#4
0
 protected void ParseFirstErrorToMemberVariables()
 {
     if (errors.Count > 0)
     {
         Dictionary <String, Object> tmpErrorMap = errors[0];
         rawErrorData = new SmartMap(tmpErrorMap, true);
         if (rawErrorData.Get("Source") != null)
         {
             source = rawErrorData.Get("Source").ToString();
         }
         if (rawErrorData.Get("ReasonCode") != null)
         {
             reasonCode = rawErrorData.Get("ReasonCode").ToString();
         }
         if (rawErrorData.Get("Description") != null)
         {
             description = rawErrorData.Get("Description").ToString();
         }
     }
 }
示例#5
0
        public IDictionary <String, Object> Encrypt(IDictionary <String, Object> map)
        {
            //requestMap is a SmartMap it offers a easy way to do nested lookups.
            SmartMap smartMap = new SmartMap(map);

            if (this.publicKey != null)
            {
                foreach (String fieldToEncrypt in configuration.FieldsToEncrypt)
                {
                    if (smartMap.ContainsKey(fieldToEncrypt))
                    {
                        String payload = null;

                        // 1) extract the encryptedData from map
                        Object tmpObjectToEncrypt = smartMap.Get(fieldToEncrypt);
                        smartMap.Remove(fieldToEncrypt);

                        if (tmpObjectToEncrypt.GetType() == typeof(Dictionary <String, Object>))
                        {
                            // 2) create json string
                            payload = JsonConvert.SerializeObject(tmpObjectToEncrypt);
                            // 3) escaping the string
                            payload = CryptUtil.SanitizeJson(payload);
                        }
                        else
                        {
                            payload = tmpObjectToEncrypt.ToString();
                        }

                        Tuple <byte[], byte[], byte[]> aesResult = CryptUtil.EncryptAES(System.Text.Encoding.UTF8.GetBytes(payload), configuration.SymmetricKeysize, configuration.SymmetricMode, configuration.SymmetricPadding);

                        // 4) generate random iv
                        byte[] ivBytes = aesResult.Item1;
                        // 5) generate AES SecretKey
                        byte[] secretKeyBytes = aesResult.Item2;
                        // 6) encrypt payload
                        byte[] encryptedDataBytes = aesResult.Item3;

                        String ivValue            = CryptUtil.Encode(ivBytes, configuration.DataEncoding);
                        String encryptedDataValue = CryptUtil.Encode(encryptedDataBytes, configuration.DataEncoding);

                        // 7) encrypt secretKey with issuer key
                        byte[] encryptedSecretKey = CryptUtil.EncrytptRSA(secretKeyBytes, this.publicKey, configuration.OaepEncryptionPadding);
                        String encryptedKeyValue  = CryptUtil.Encode(encryptedSecretKey, configuration.DataEncoding);

                        String fingerprintHexString = publicKeyFingerPrint;

                        String baseKey = "";
                        if (fieldToEncrypt.IndexOf(".") > 0)
                        {
                            baseKey  = fieldToEncrypt.Substring(0, fieldToEncrypt.IndexOf("."));
                            baseKey += ".";
                        }

                        if (configuration.PublicKeyFingerprintFiledName != null)
                        {
                            smartMap.Add(baseKey + configuration.PublicKeyFingerprintFiledName, fingerprintHexString);
                        }
                        if (configuration.OaepHashingAlgorithmFieldName != null)
                        {
                            smartMap.Add(baseKey + configuration.OaepHashingAlgorithmFieldName, configuration.OaepHashingAlgorithm);
                        }
                        smartMap.Add(baseKey + configuration.IvFieldName, ivValue);
                        smartMap.Add(baseKey + configuration.EncryptedKeyFiledName, encryptedKeyValue);
                        smartMap.Add(baseKey + configuration.EncryptedDataFieldName, encryptedDataValue);

                        break;
                    }
                }
            }
            return(smartMap);
        }
示例#6
0
        public IDictionary <String, Object> Decrypt(IDictionary <String, Object> map)
        {
            SmartMap smartMap = new SmartMap(map);

            foreach (String fieldToDecrypt in configuration.FieldsToDecrypt)
            {
                if (smartMap.ContainsKey(fieldToDecrypt))
                {
                    String baseKey = "";
                    if (fieldToDecrypt.IndexOf(".") > 0)
                    {
                        baseKey  = fieldToDecrypt.Substring(0, fieldToDecrypt.LastIndexOf("."));
                        baseKey += ".";
                    }

                    //need to read the key
                    String encryptedKey = (String)smartMap.Get(baseKey + configuration.EncryptedKeyFiledName);
                    smartMap.Remove(baseKey + configuration.EncryptedKeyFiledName);

                    byte[] encryptedKeyByteArray = CryptUtil.Decode(encryptedKey, configuration.DataEncoding);

                    //need to decryt with RSA
                    byte[] secretKeyBytes = null;
                    if (smartMap.ContainsKey(baseKey + configuration.OaepHashingAlgorithmFieldName))
                    {
                        string oaepHashingAlgorithm = (String)smartMap.Get(baseKey + configuration.OaepHashingAlgorithmFieldName);
                        oaepHashingAlgorithm = oaepHashingAlgorithm.Replace("SHA", "SHA-");
                        RSAEncryptionPadding customEncryptionPadding = configuration.OaepEncryptionPadding;
                        if (oaepHashingAlgorithm.Equals("SHA-256"))
                        {
                            customEncryptionPadding = RSAEncryptionPadding.OaepSHA256;
                        }
                        else if (oaepHashingAlgorithm.Equals("SHA-512"))
                        {
                            customEncryptionPadding = RSAEncryptionPadding.OaepSHA512;
                        }
                        secretKeyBytes = CryptUtil.DecryptRSA(encryptedKeyByteArray, this.privateKey, customEncryptionPadding);
                    }
                    else
                    {
                        secretKeyBytes = CryptUtil.DecryptRSA(encryptedKeyByteArray, this.privateKey, configuration.OaepEncryptionPadding);
                    }



                    //need to read the iv
                    String ivString = (String)smartMap.Get(baseKey + configuration.IvFieldName);
                    smartMap.Remove(baseKey + configuration.IvFieldName);

                    byte[] ivByteArray = CryptUtil.Decode(ivString.ToString(), configuration.DataEncoding);

                    // remove the field that are not required in the map
                    if (smartMap.ContainsKey(configuration.PublicKeyFingerprintFiledName))
                    {
                        smartMap.Remove(configuration.PublicKeyFingerprintFiledName);
                    }

                    //need to decrypt the data
                    String encryptedData = (String)smartMap.Get(baseKey + configuration.EncryptedDataFieldName);
                    smartMap.Remove(baseKey + configuration.EncryptedDataFieldName);
                    byte[] encryptedDataByteArray = CryptUtil.Decode(encryptedData, configuration.DataEncoding);

                    byte[] decryptedDataByteArray = CryptUtil.DecryptAES(ivByteArray, secretKeyBytes, encryptedDataByteArray, configuration.SymmetricKeysize, configuration.SymmetricMode, configuration.SymmetricPadding);
                    String decryptedDataString    = System.Text.Encoding.UTF8.GetString(decryptedDataByteArray);

                    if (decryptedDataString.StartsWith("{"))
                    {
                        Dictionary <String, Object> decryptedDataMap = JsonConvert.DeserializeObject <Dictionary <String, Object> >(decryptedDataString);
                        foreach (KeyValuePair <String, Object> entry in decryptedDataMap)
                        {
                            smartMap.Add(baseKey + configuration.EncryptedDataFieldName + "." + entry.Key, entry.Value);
                        }
                    }
                    else
                    {
                        smartMap.Add(baseKey + configuration.EncryptedDataFieldName, decryptedDataString);
                    }

                    break;
                }
            }
            return(smartMap);
        }
示例#7
0
 public static void Out(SmartMap response, String key)
 {
     Console.WriteLine(key + "---> " + response[key]);
 }
        /// <summary>
        /// Execute the specified action, resourcePath and baseObject.
        /// </summary>
        /// <param name="action">Action.</param>
        /// <param name="resourcePath">Resource path.</param>
        /// <param name="requestMap">Request Map.</param>
        /// <param name="headerList">Header List.</param>
        public virtual IDictionary <String, Object> Execute(OperationConfig config, OperationMetadata metadata, BaseObject requestMap)
        {
            IRestResponse           response;
            RestyRequest            request;
            IRestClient             client;
            CryptographyInterceptor interceptor;

            try
            {
                request     = this.GetRequest(config, metadata, requestMap);
                interceptor = request.interceptor;

                //arizzini: create client
                if (this.restClient != null)
                {
                    client = restClient;
                }
                else
                {
                    client = new RestClient();
                }

                client.BaseUrl = request.BaseUrl;
                client.Proxy   = ApiConfig.GetProxy();

                log.Debug(">>Execute(action='" + config.Action + "', resourcePaht='" + config.ResourcePath + "', requestMap='" + requestMap + "'");
                log.Debug("excute(), request.Method='" + request.Method + "'");
                log.Debug("excute(), request.URL=" + request.AbsoluteUrl.ToString());
                log.Debug("excute(), request.Header=");
                log.Debug(request.Parameters.Where(x => x.Type == ParameterType.HttpHeader));
                log.Debug("excute(), request.Body=");
                log.Debug(request.Parameters.Where(x => x.Type == ParameterType.RequestBody));
                response = client.Execute(request);
                log.Debug("Execute(), response.Header=");
                log.Debug(response.Headers);
                log.Debug("Execute(), response.Body=");
                log.Debug(response.Content.ToString());


                if (response.ErrorException == null && response.Content != null)
                {
                    IDictionary <String, Object> responseObj = new Dictionary <String, Object>();

                    if (response.Content.Length > 0)
                    {
                        if (response.StatusCode < HttpStatusCode.Ambiguous)
                        {
                            responseObj = SmartMap.AsDictionary(response.Content);
                            if (interceptor != null)
                            {
                                try
                                {
                                    responseObj = interceptor.Decrypt(responseObj);
                                }
                                catch (Exception e)
                                {
                                    throw new ApiException("Error: decrypting payload", e);
                                }
                            }

                            log.Debug("<<Execute()");
                            return(responseObj);
                        }
                        else
                        {
                            int status = (int)response.StatusCode;
                            throw new ApiException(status, SmartMap.AsObject(response.Content));
                        }
                    }
                    else
                    {
                        return(responseObj);
                    }
                }
                else
                {
                    throw new ApiException(response.ErrorMessage, response.ErrorException);
                }
            } catch (ApiException apiE) {
                log.Error(apiE.Message, apiE);
                throw;
            } catch (Exception genE) {
                ApiException wrapper = new ApiException(genE.Message, genE);
                log.Error(wrapper.Message, wrapper);
                throw wrapper;
            }
        }