Exemplo n.º 1
0
        public ShopConnectorConnectionRecord InsertConnection(ConnectionModel model)
        {
            var utcNow = DateTime.UtcNow;
            ShopConnectorConnectionRecord connection = null;

            if (model.IsForExport)
            {
                var    hmac = new HmacAuthentication();
                string publicKey, secretKey;

                for (var i = 0; i < 9999; ++i)
                {
                    if (hmac.CreateKeys(out publicKey, out secretKey) && !_connectionRepository.TableUntracked.Any(x => x.PublicKey == publicKey))
                    {
                        connection = new ShopConnectorConnectionRecord
                        {
                            IsActive    = true,
                            IsForExport = model.IsForExport,
                            Url         = model.Url,
                            PublicKey   = publicKey,
                            SecretKey   = secretKey,
                            LimitedToManufacturerIds = string.Join(",", model.LimitedToManufacturerIds ?? new int[0]),
                            LimitedToStoreIds        = string.Join(",", model.LimitedToStoreIds ?? new int[0]),
                            CreatedOnUtc             = utcNow,
                            UpdatedOnUtc             = utcNow
                        };
                        break;
                    }
                }
            }
            else
            {
                connection = new ShopConnectorConnectionRecord
                {
                    IsActive    = true,
                    IsForExport = model.IsForExport,
                    Url         = model.Url,
                    PublicKey   = model.PublicKey,
                    SecretKey   = model.SecretKey,
                    LimitedToManufacturerIds = string.Join(",", model.LimitedToManufacturerIds ?? new int[0]),
                    LimitedToStoreIds        = string.Join(",", model.LimitedToStoreIds ?? new int[0]),
                    CreatedOnUtc             = utcNow,
                    UpdatedOnUtc             = utcNow
                };
            }

            if (connection != null)
            {
                _connectionRepository.Insert(connection);
                ConnectionCache.Remove();
            }

            return(connection);
        }
Exemplo n.º 2
0
    public void HmacVerficationTest()
    {
        var key  = Encoding.UTF8.GetBytes("HMAC KEY");
        var data = Encoding.UTF8.GetBytes(testMessage);

        var hmac = new HmacAuthentication(key);
        var hash = hmac.ComputeHash(data);

        var isValid = hmac.VerifyHash(hash, data);

        Assert.True(isValid);
    }
Exemplo n.º 3
0
    public void RsaSigningTest()
    {
        var     rsa  = new RsaEncryption();
        RsaKeys keys = rsa.GenerateKeys();

        var hmacKey = Encoding.UTF8.GetBytes("HMAC KEY");
        var hmac    = new HmacAuthentication(hmacKey);

        var data = Encoding.UTF8.GetBytes(testMessage);
        var hash = hmac.ComputeHash(data);

        var signature = rsa.SignData(hash, keys.privateKey);
        var isValid   = rsa.VerifySignature(hash, signature, keys.publicKey);

        Assert.True(isValid);
    }
        public virtual bool CreateKeys(User user)
        {
            if (user != null)
            {
                var    hmac = new HmacAuthentication();
                string key1, key2;

                for (int i = 0; i < 9999; ++i)
                {
                    if (hmac.CreateKeys(out key1, out key2) && !_userRepository.GetAll().Any(x => x.PublicKey.ToLower().Equals(key1.ToLower())))
                    {
                        user.PublicKey = key1;
                        user.SecretKey = key2;
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 5
0
        public bool CreateKeys(int customerId)
        {
            if (customerId != 0)
            {
                var    hmac = new HmacAuthentication();
                var    userData = WebApiCachingUserData.Data();
                string key1, key2;

                for (int i = 0; i < 9999; ++i)
                {
                    if (hmac.CreateKeys(out key1, out key2) && !userData.Exists(x => x.PublicKey.IsCaseInsensitiveEqual(key1)))
                    {
                        var apiUser = new WebApiUserCacheData
                        {
                            CustomerId = customerId,
                            PublicKey  = key1,
                            SecretKey  = key2,
                            Enabled    = true
                        };

                        RemoveKeys(customerId);

                        var attribute = new GenericAttribute
                        {
                            EntityId = customerId,
                            KeyGroup = "Customer",
                            Key      = WebApiCachingUserData.Key,
                            Value    = apiUser.ToString()
                        };

                        _genericAttributeService.InsertAttribute(attribute);

                        WebApiCachingUserData.Remove();
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 6
0
        public static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("usage!");
                return;
            }

            var apiKey  = args[0];
            var request = new HttpRequestMessage(HttpMethod.Post, args[1].TrimEnd('/') + "/Umbraco/BackOffice/DTeam/Api/CaptureProgress");

            var requestPath = request.RequestUri.CleanPathAndQuery();
            var timestamp   = DateTime.UtcNow;
            var nonce       = Guid.NewGuid();

            var signature   = HmacAuthentication.GetSignature(requestPath, timestamp, nonce, apiKey);
            var headerToken = HmacAuthentication.GenerateAuthorizationHeader(signature, nonce, timestamp);

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", headerToken);

            var httpClient = new HttpClient();

            var result = httpClient.SendAsync(request, CancellationToken.None).Result;

            Console.WriteLine(result.StatusCode);

            if (result.StatusCode != HttpStatusCode.OK)
            {
                return;
            }

            var formatter  = new JsonMediaTypeFormatter();
            var formatters = new[] { formatter };
            var datetime   = result.Content.ReadAsAsync <DateTime>(formatters, CancellationToken.None).Result;

            //Console.WriteLine(datetime.ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss K"));
            Console.WriteLine(datetime.ToString("yyyy/MM/dd HH:mm:ss K"));
        }
Exemplo n.º 7
0
 public void SetUp()
 {
     _hmacAuth = new HmacAuthentication();
 }