Exemplo n.º 1
0
        public IEnumerable <T> GetInstances(RedisValue[] values)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            for (int i = 0; i < values.Length; i += 3)
            {
                byte[] data = values[i];
                if (data == null || data.Length == 0)
                {
                    logger.LogWarning("Not Data Found in redis record");
                    continue;
                }

                if (isWellKnown)
                {
                    yield return(serializer.Deserialize <T>(data));
                }
                else
                {
                    var type = Type.GetType(values[i + 2]);
                    if (type == null)
                    {
                        throw new Exception("Type is not resolved");
                    }

                    yield return((T)serializer.Deserialize(type, data));
                }
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public Task WatchAsync(Action <T> callback, params string[] keys)
        {
            var key = GetKey(keys);

            _etcdClient.Watch(key, async response =>
            {
                if (_retryBackOffAmount != 0)
                {
                    _logger.LogInformation("Watch Recovered from exception for {key}", key);

                    callback(await GetAsync());
                    _retryBackOffAmount = 0;
                }

                _logger.LogDebug("Watch Triggered for {key}", key);

                if (!response.Events.Any())
                {
                    return;
                }

                var value = response.Events[0].Kv.Value.ToStringUtf8();
                callback(_dataSerializer.Deserialize(value));
            }, _metadata, WatchExceptionHandler);

            return(Task.CompletedTask);
        }
        public IEnumerable <T> Update <T>(long revision)
        {
            var message = new NameValueCollection
            {
                { "command", "update" },
                { "data", revision.ToString() }
            };
            NameValueCollection result = Send(message);

            return(dataSerializer.Deserialize <IEnumerable <T> >(Convert.FromBase64String(result["data"])));
        }
Exemplo n.º 4
0
        public AuthenticationTicket Unprotect(string protectedText)
        {
            try
            {
                if (protectedText == null)
                {
                    return(default(AuthenticationTicket));
                }

                var protectedData = Base64UrlTextEncoder.Decode(protectedText);
                if (protectedData == null)
                {
                    return(default(AuthenticationTicket));
                }


                var userData = rsa.Decrypt(protectedData);
                if (userData == null)
                {
                    return(default(AuthenticationTicket));
                }
                var v1 = _serializer.Deserialize(userData);
                var v2 = v1.Properties.ExpiresUtc;
                return(v1);
            }
            catch
            {
                // TODO trace exception, but do not leak other information
                return(default(AuthenticationTicket));
            }
        }
    public AuthenticationTicket?Unprotect(string?protectedText, string?purpose)
    {
        AuthenticationTicket?authTicket;

        try
        {
            var unprotectedBytes = dataProtector.Unprotect
                                   (
                Base64UrlTextEncoder.Decode(protectedText ?? "")
                                   );
            authTicket = ticketSerializer.Deserialize(unprotectedBytes);
            var embeddedJwt = authTicket?.Properties?.GetTokenValue("Jwt");
            new JwtSecurityTokenHandler().ValidateToken(embeddedJwt, validationParameters, out var token);
            if (!(token is JwtSecurityToken jwt))
            {
                throw new SecurityTokenValidationException("JWT token was found to be invalid");
            }
            if (!jwt.Header.Alg.Equals(Algorithm, StringComparison.Ordinal))
            {
                throw new ArgumentException($"Algorithm must be '{Algorithm}'");
            }
        }
        catch (Exception)
        {
            authTicket = null;
        }
        return(authTicket);
    }
Exemplo n.º 6
0
        /// <inheritdoc />
        public Task WatchAsync(Action <T> callback, params string[] keys)
        {
            var fileSystemWatcher = new FileSystemWatcher()
            {
                Path                = _fileInfo.Directory?.FullName,
                NotifyFilter        = NotifyFilters.LastWrite,
                Filter              = _fileInfo.Name,
                EnableRaisingEvents = true
            };

            void Callback(object sender, FileSystemEventArgs args)
            {
                var file = System.IO.File.ReadAllText(args.FullPath);

                callback(_dataSerializer.Deserialize(file));
            }

            fileSystemWatcher.Created += Callback;
            fileSystemWatcher.Changed += Callback;

            _handlers.Add(Callback);
            _watchers.Add(fileSystemWatcher);

            return(Task.CompletedTask);
        }
Exemplo n.º 7
0
        public NameValueCollection Interact(IServerEndpoint serverEndpoint, NameValueCollection message)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverEndpoint.Url + ":" + serverEndpoint.Port);

            byte[]          postBuffer;
            Stream          postData       = request.GetRequestStream();
            HttpWebResponse response       = (HttpWebResponse)request.GetResponse();
            Stream          responseStream = response.GetResponseStream();

            byte[] responseBuffer;
            string result;
            NameValueCollection nameValueCollection = new NameValueCollection();

            request.Method = "POST";
            foreach (KeyValuePair <string, string> collection in message)
            {
                postBuffer            = serializer.Serialize(collection.Value);
                request.ContentLength = postBuffer.Length;
                postData.Write(postBuffer, 0, postBuffer.Length);
                responseBuffer = new byte[BufferSize];
                responseStream.Read(responseBuffer, 0, responseBuffer.Length);
                result = serializer.Deserialize <string>(responseBuffer);
                nameValueCollection.Add(collection.Key, result);
            }
            postData.Close();
            response.Close();
            responseStream.Close();
            return(nameValueCollection);
        }
Exemplo n.º 8
0
        private IEnumerable <T> GetValues(RedisKey key, RedisValue[] values)
        {
            foreach (var value in values)
            {
                if (!value.HasValue)
                {
                    log.LogDebug("{0} Redis value is null", key);
                    yield break;
                }

                if (RedisValueExtractor.IsPrimitive <T>())
                {
                    yield return(RedisValueExtractor.SafeConvert <T>(value));
                }
                else
                {
                    var data = (byte[])value;
                    if ((data == null) ||
                        (data.Length == 0))
                    {
                        log.LogDebug("{0} Data length is zero", key);
                        yield break;
                    }

                    yield return(serializer.Deserialize <T>(data));
                }
            }
        }
Exemplo n.º 9
0
        private void FurnishInstance(object settingsInstance, int storeId)
        {
            var settingInstanceType = settingsInstance.GetType();
            //each setting group will have some properties. We'll loop through these using reflection
            var propertyFields = settingInstanceType.GetProperties();
            var allSettings    = Repository.Where(x => x.GroupName == settingInstanceType.Name && x.StoreId == storeId).Select().ToList();

            foreach (var property in propertyFields)
            {
                var propertyName = property.Name;

                //retrieve the value of setting from db
                var savedSettingEntity = allSettings.FirstOrDefault(x => x.Key == propertyName);

                if (savedSettingEntity != null)
                {
                    try
                    {
                        property.SetValue(settingsInstance,
                                          property.PropertyType.IsPrimitive()
                                ? TypeConverter.CastPropertyValue(property, savedSettingEntity.Value)
                                : _dataSerializer.Deserialize(savedSettingEntity.Value, property.PropertyType));
                    }
                    catch (Exception ex)
                    {
                        //keep default value
                    }
                }
            }
        }
Exemplo n.º 10
0
        public static Result <object, Exception> TryDeserialize(this IDataSerializer serializer, Stream source, Type type)
        {
            var position = source.Position;

            try
            {
                var result = serializer.Deserialize(source, type);
                if (result == null)
                {
                    return(Result <object, Exception> .CreateError(new SerializationException("Serializer returned null")));
                }

                var actualType = result.GetType();
                if (!type.IsAssignableFrom(actualType))
                {
                    return(Result <object, Exception> .CreateError(new InvalidCastException(
                                                                       String.Format("Source was expected to be of type {0} but was of type {1}.",
                                                                                     type.Name,
                                                                                     actualType.Name))));
                }

                return(Result <object, Exception> .CreateSuccess(result));
            }
            catch (Exception e)
            {
                return(Result <object, Exception> .CreateError(e));
            }
            finally
            {
                source.Position = position;
            }
        }
Exemplo n.º 11
0
        public static Result <T, Exception> TryDeserializeAs <T>(this IDataSerializer serializer, Stream source)
        {
            var position = source.Position;

            try
            {
                var result = serializer.Deserialize(source, typeof(T));
                if (result == null)
                {
                    return(Result <T, Exception> .CreateError(new SerializationException("Serializer returned null")));
                }

                if (!(result is T))
                {
                    return(Result <T, Exception> .CreateError(new InvalidCastException(
                                                                  String.Format("Source was expected to be of type {0} but was of type {1}.",
                                                                                typeof(T).Name,
                                                                                result.GetType().Name))));
                }

                return(Result <T, Exception> .CreateSuccess((T)result));
            }
            catch (Exception e)
            {
                return(Result <T, Exception> .CreateError(e));
            }
            finally
            {
                source.Position = position;
            }
        }
Exemplo n.º 12
0
        public AuthenticationTicket Unprotect(string protectedText, string purpose)
        {
            var authTicket = default(AuthenticationTicket);
            var algorithm  = JwtOptions.Algorithm;

            try
            {
                authTicket = _ticketSerializer.Deserialize(
                    _dataProtector.Unprotect(
                        Base64UrlTextEncoder.Decode(protectedText)));

                var embeddedJwt = authTicket
                                  .Properties?
                                  .GetTokenValue(_jwtOptions.TokenName);
                new JwtSecurityTokenHandler()
                .ValidateToken(embeddedJwt, _jwtOptions.ValidationParameters, out var token);

                if (!(token is JwtSecurityToken jwt))
                {
                    throw new SecurityTokenValidationException("JWT token was found to be invalid");
                }

                if (!jwt.Header.Alg.Equals(algorithm, StringComparison.Ordinal))
                {
                    throw new ArgumentException($"Algorithm must be '{algorithm}'");
                }
            }
            catch (Exception)
            {
                return(null);
            }

            return(authTicket);
        }
Exemplo n.º 13
0
        public async Task <IPhotoDirectory> Load(string path)
        {
            var configFilename = _fileSystem.Path.Combine(path, PhotoFolderConsts.ConfigFileName);
            var configFile     = _fileSystem.FileInfo.FromFileName(configFilename);

            if (!configFile.Exists)
            {
                throw new FileNotFoundException("The photo directory config was not found.", configFilename);
            }

            var configContent = await _fileSystem.File.ReadAllTextAsync(configFilename);

            var config = _serializer.Deserialize <PhotoDirectoryConfig>(configContent);

            var workspacePath = _fileSystem.Path.Combine(Environment.ExpandEnvironmentVariables(_workspaceOptions.Path), config.Guid.ToString("D"));
            var databaseFile  = _fileSystem.FileInfo.FromFileName(_fileSystem.Path.Combine(workspacePath, "database.sqlite"));

            databaseFile.Directory.Create();

            var contextOptions = _contextBuilder.Build(databaseFile.FullName);

            if (_workspaceOptions.ApplyMigrations)
            {
                await using var context = new AppDbContext(contextOptions);
                await context.Database.MigrateAsync();
            }

            var deletedFilesPath = _fileSystem.Path.Combine(path, config.DirectoryMemoryPath);
            var deletedFiles     = await DirectoryMemoryManager.Load(_fileSystem, deletedFilesPath);

            return(new PhotoDirectory(_fileSystem, path, config, contextOptions, deletedFiles));
        }
Exemplo n.º 14
0
        public TData Unprotect(string protectedText)
        {
            try
            {
                if (protectedText == null)
                {
                    return(default(TData));
                }

                byte[] protectedData = _encoder.Decode(protectedText);
                if (protectedData == null)
                {
                    return(default(TData));
                }

                byte[] userData = _protector.Unprotect(protectedData);
                if (userData == null)
                {
                    return(default(TData));
                }

                TData model = _serializer.Deserialize(userData);
                return(model);
            }
            catch
            {
                // TODO trace exception, but do not leak other information
                return(default(TData));
            }
        }
Exemplo n.º 15
0
        public AuthenticationTicket Unprotect(string protectedText)
        {
            try
            {
                if (protectedText == null)
                {
                    return(default(AuthenticationTicket));
                }

                Base64UrlTextEncoder encoder = new Base64UrlTextEncoder();
                var protectedData            = encoder.Decode(protectedText);
                if (protectedData == null)
                {
                    return(default(AuthenticationTicket));
                }


                var userData = rsa.Decrypt(protectedData);
                if (userData == null)
                {
                    return(default(AuthenticationTicket));
                }

                return(_serializer.Deserialize(userData));
            }
            catch
            {
                // TODO trace exception, but do not leak other information
                return(default(AuthenticationTicket));
            }
        }
Exemplo n.º 16
0
        /// <inheritdoc />
        public TData?Unprotect(string?protectedText, string?purpose)
        {
            try
            {
                if (protectedText == null)
                {
                    return(default(TData));
                }

                var protectedData = Base64UrlTextEncoder.Decode(protectedText);
                if (protectedData == null)
                {
                    return(default(TData));
                }

                var protector = _protector;
                if (!string.IsNullOrEmpty(purpose))
                {
                    protector = protector.CreateProtector(purpose);
                }

                var userData = protector.Unprotect(protectedData);
                if (userData == null)
                {
                    return(default(TData));
                }

                return(_serializer.Deserialize(userData));
            }
            catch
            {
                // TODO trace exception, but do not leak other information
                return(default(TData));
            }
        }
Exemplo n.º 17
0
        private void FurnishInstance <T>(T settingsInstance) where T : ISettingGroup
        {
            var settingInstanceType = settingsInstance.GetType();
            //each setting group will have some properties. We'll loop through these using reflection
            var propertyFields = settingInstanceType.GetProperties();
            var allSettings    = Repository.Where(x => x.GroupName == settingInstanceType.Name).Select().ToList();

            foreach (var property in propertyFields)
            {
                var propertyName = property.Name;

                //retrieve the value of setting from db
                var savedSettingEntity = allSettings.FirstOrDefault(x => x.Key == propertyName);

                if (savedSettingEntity != null)
                {
                    try
                    {
                        property.SetValue(settingsInstance,
                                          property.PropertyType.IsPrimitive()
                                ? TypeConverter.CastPropertyValue(property, savedSettingEntity.Value)
                                : _dataSerializer.Deserialize(savedSettingEntity.Value, property.PropertyType));
                    }
                    catch (Exception ex)
                    {
                        _logger.Log <Setting>(LogLevel.Error,
                                              $"Invalid value '{savedSettingEntity.Value}' for setting '{settingInstanceType.Name}.{property.Name}'",
                                              ex);
                    }
                }
            }
        }
Exemplo n.º 18
0
        public async Task <byte[]> ReadRequestMessageAsync(
            PipeReader bodyReader,
            IList <string> orderedParameterNames,
            IMethod method,
            CancellationToken token)
        {
            var values         = new object?[orderedParameterNames.Count];
            var methodAccessor = CreateMethodAccessor(method);

            if (orderedParameterNames.Count > 0)
            {
                var requestParameterTypes = methodAccessor.GetParameterTypes();

                JsonElement body;
                using (var stream = bodyReader.AsStream())
                {
                    var options = new JsonSerializerOptions {
                        PropertyNameCaseInsensitive = true
                    };
                    body = await JsonSerializer.DeserializeAsync <JsonElement>(stream, options, token).ConfigureAwait(false);
                }

                foreach (var entry in body.EnumerateObject())
                {
                    var index = FindIndex(orderedParameterNames, entry.Name);
                    if (index < 0)
                    {
                        continue;
                    }

                    var parameterType = requestParameterTypes[index];
                    try
                    {
                        values[index] = _serializer.Deserialize(entry.Value.GetRawText(), parameterType);
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException(
                                  "Fail to deserialize parameter [{0}] with type [{1}] from request.".FormatWith(entry.Name, parameterType),
                                  ex);
                    }
                }
            }

            var payload = methodAccessor.SerializeRequest(values);

            var result = new byte[payload.Length + 5];

            // not compressed
            result[0] = 0;

            // length
            BinaryPrimitives.WriteUInt32BigEndian(result.AsSpan(1), (uint)payload.Length);

            // data
            Buffer.BlockCopy(payload, 0, result, 5, payload.Length);

            return(result);
        }
Exemplo n.º 19
0
        public static IKeyPair Load(FileInfo file, IDataSerializer serializer)
        {
            file.Refresh();
            if (!file.Exists)
                throw new FileNotFoundException("not found", file.FullName);

            return serializer.Deserialize<KeyPair>(File.ReadAllBytes(file.FullName));
        }
Exemplo n.º 20
0
        public ImmutableEnvelope ReadAsEnvelopeData(byte[] buffer)
        {
            var header = EnvelopeHeaderContract.ReadHeader(buffer);

            if (header.MessageFormatVersion != EnvelopeHeaderContract.Schema2DataFormat)
            {
                throw new InvalidOperationException("Unexpected bytes in enveloper header");
            }


            EnvelopeContract envelope;

            using (var stream = new MemoryStream(buffer, EnvelopeHeaderContract.FixedSize, (int)header.EnvelopeBytes))
            {
                envelope = _envelopeSerializer.DeserializeEnvelope(stream);
            }

            var items = new ImmutableMessage[envelope.Messages.Length];

            for (var i = 0; i < items.Length; i++)
            {
                var  itemContract = envelope.Messages[i];
                var  attributes   = EnvelopeConvert.ItemAttributesFromContract(itemContract.Attributes);
                Type contractType;

                var itemPosition = EnvelopeHeaderContract.FixedSize + (int)header.EnvelopeBytes +
                                   (int)itemContract.ContentPosition;
                var itemSize = (int)itemContract.ContentSize;
                if (_dataSerializer.TryGetContractTypeByName(itemContract.ContractName, out contractType))
                {
                    try
                    {
                        // we can deserialize. Convert it to a message
                        using (var stream = new MemoryStream(buffer, itemPosition, itemSize))
                        {
                            var instance = _dataSerializer.Deserialize(stream, contractType);
                            items[i] = new ImmutableMessage(contractType, instance, attributes, i);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException("Failed to deserialize: " + itemContract.ContractName, ex);
                    }
                }
                else
                {
                    // we can't deserialize. Keep it as buffer
                    var bufferInstance = new byte[itemContract.ContentSize];
                    Buffer.BlockCopy(buffer, itemPosition, bufferInstance, 0, itemSize);
                    items[i] = new ImmutableMessage(null, bufferInstance, attributes, i);
                }
            }

            var envelopeAttributes = EnvelopeConvert.EnvelopeAttributesFromContract(envelope.EnvelopeAttributes);

            return(new ImmutableEnvelope(envelope.EnvelopeId, envelopeAttributes, items, envelope.DeliverOnUtc,
                                         envelope.CreatedOnUtc));
        }
Exemplo n.º 21
0
        public AuthenticationProperties Unprotect(string protectedText, string purpose)
        {
            // Decrypt the key and retrieve the data from the cache.
            var key        = protectedText;
            var cacheKey   = $"{CacheKeyPrefix}{key}";
            var serialized = _cache.Get(cacheKey);

            return(_serializer.Deserialize(serialized));
        }
Exemplo n.º 22
0
        public async Task Deserialize_GivenStream_ReturnsAListOfIdInfo()
        {
            var byteArray = Encoding.UTF8.GetBytes(_idFiledata);
            var stream    = new MemoryStream(byteArray);

            var result = (await _dataSerializer.Deserialize <ValidIDInfo>(stream)).ToList();

            Assert.That(JsonConvert.SerializeObject(result), Is.EqualTo(JsonConvert.SerializeObject(_idInfos)));
        }
        public async Task <AuthorizationCode> GetAsync(string key)
        {
            var data = await _cache.GetAsync(key);

            if (data == null)
            {
                return(null);
            }
            return(_serializer.Deserialize(data));
        }
Exemplo n.º 24
0
 /// <summary>
 /// 深拷贝
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static T Clone <T>(T obj)
 {
     if (obj != null)
     {
         var _temp = serializer.Serialize(obj);
         var res   = serializer.Deserialize <T>(_temp);
         return(res);
     }
     return(default(T));
 }
        public async Task <RefreshToken> GetAsync(string key)
        {
            var data = await _cache.GetAsync(key);

            if (data == null)
            {
                return(null);
            }
            return(_serializer.Deserialize(data));
        }
Exemplo n.º 26
0
        public async UniTask <T> LoadAsync(Uri uri, CancellationToken cancellationToken = default)
        {
            T data = default;

            if (await asyncCRUDHandler.ExistsAsync(uri, cancellationToken))
            {
                data = dataSerializer.Deserialize(await asyncCRUDHandler.ReadAsync(uri, cancellationToken));
            }

            return(data != null ? data : new T());
        }
Exemplo n.º 27
0
        public T Load(Uri uri)
        {
            T data = default;

            if (crudHandler.Exists(uri))
            {
                data = dataSerializer.Deserialize(crudHandler.Read(uri));
            }

            return(data != null ? data : new T());
        }
        public async Task <Consent> LoadAsync(string subject, string client)
        {
            var key  = $"{subject}_{client}";
            var data = await _cache.GetAsync(key);

            if (data == null)
            {
                return(null);
            }
            return(_serializer.Deserialize(data));
        }
        /// <summary>
        /// 批量接收
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queueName">队列名</param>
        /// <param name="exchangeName"></param>
        /// <param name="noAck"></param>
        /// <returns></returns>
        public List <T> ReceiveBatch <T>(string queueName, string exchangeName = null, bool noAck = false)
        {
            List <T> list = new List <T>();
            List <BasicGetResult> resList = BasicDequeueBatch(exchangeName, queueName, noAck: noAck);

            foreach (var res in resList)
            {
                try
                {
                    T obj = serializer.Deserialize <T>(res.Body);
                    list.Add(obj);
                }
                catch (Exception ex)
                {
                    RecordException(ex, res.Body);
                }
            }

            return(list);
        }
Exemplo n.º 30
0
        public object LoadWidgetSettings(string widgetId, Type type)
        {
            var widgetSettingName = string.Format(WidgetSettingKey, widgetId);
            var setting           = _settingService.FirstOrDefault(x => x.Key == widgetSettingName);

            if (setting == null)
            {
                return(Activator.CreateInstance(type));
            }
            return(_dataSerializer.Deserialize(setting.Value, type));
        }
Exemplo n.º 31
0
        public async Task <IList <T> > Read <T>() where T : class, new()
        {
            string path = GetPath <T>();

            if (!File.Exists(path))
            {
                return(new List <T>());
            }
            var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);

            return(await _dataSerializer.Deserialize <T>(fileStream));
        }