public async Task Init(ElencyConfiguration config)
        {
            if (config == null)
            {
                throw new Exception("You must define a configuration");
            }

            if (_initialized == true)
            {
                throw new Exception("The client is already initialised");
            }

            try
            {
                config.Validate();
                _config            = config;
                _configurationPath = string.Format("/config/{0}/{1}/{2}",
                                                   _config.AppId,
                                                   _config.Environment,
                                                   _config.AppVersion);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (_config.LocalConfiguration != null)
            {
                await GetLocalConfiguration();

                return;
            }

            await GetConfiguration();

            _initialized = true;

            if (_config.RefreshInterval > 0)
            {
                _timerCallback = new TimerCallback(RefreshConfigurationOnInteval);
                _timer         = new Timer(_timerCallback, null, 0, _config.RefreshInterval);
            }
        }
Esempio n. 2
0
        public static string GenerateAuthorizationHeader(ElencyConfiguration config, string path, string method, bool hmac = false)
        {
            var dateTime1970 = new DateTime(1970, 1, 1);
            var span         = DateTime.UtcNow - dateTime1970;
            var timestamp    = span.TotalMilliseconds.ToString("0");
            var nonce        = Guid.NewGuid().ToString();
            var value        = string.Format("{0}{1}{2}{3}{4}",
                                             config.AppId,
                                             path.ToLower(),
                                             method.ToLower(),
                                             nonce,
                                             timestamp);
            var key       = (hmac == true ? config.HMACAuthorizationKey : config.ConfigEncryptionKey);
            var signature = HMACSHA256.Hash(value, key);

            return(string.Format("{0}:{1}:{2}:{3}",
                                 config.AppId,
                                 signature,
                                 nonce,
                                 timestamp));
        }