Exemplo n.º 1
0
        public void CanCompileFunction()
        {
            List<IKey> keyList = new List<IKey>();

            IKey key1 = new Key();
            key1.BlobId = new Guid("F354CA3F-B09A-44A1-8A33-C5EB57BDD89E");
            key1.OwnerUserId = new Guid("F354CA3F-B09A-44A1-8A33-C5EB57BDD89F");

            IKey key2 = new Key();
            key2.BlobId = new Guid("F354CA3F-B09A-44A1-8A33-C5EB57BDD89E");
            key2.OwnerUserId = new Guid("F354CA3F-B09A-44A1-8A33-C5EB57BDD89D");

            keyList.Add(key1);
            keyList.Add(key2);

            SearchQuery<IKey> keyQuery = new SearchQuery<IKey>("blobid:F354CA3F-B09A-44A1-8A33-C5EB57BDD89E;owneruserid:F354CA3F-B09A-44A1-8A33-C5EB57BDD89D");

            var result = keyList.FirstOrDefault(keyQuery.SearchFunction);

            Assert.Same(key2, result);
        }
Exemplo n.º 2
0
        public void KeyCollectionCrud()
        {
            Random random = new Random();

            IKey key = new Key();
            key.BlobId = Guid.NewGuid();
            key.OwnerUserId = Guid.NewGuid();
            key.Secure = true;

            //key.KeyData = new byte[72];
            //random.NextBytes(key.KeyData);

            var newKey = _provider.KeyCreate(key);

            Assert.IsNotNull(newKey);
            Assert.IsFalse(newKey.Id.Equals(Guid.Empty));

            Assert.AreEqual(key.BlobId, newKey.BlobId);
            Assert.AreEqual(key.Secure, newKey.Secure);
            Assert.AreEqual(key.OwnerUserId, newKey.OwnerUserId);

            //Assert.IsTrue(key.KeyData.SequenceEqual(newKey.KeyData));

            newKey.BlobId = Guid.NewGuid();
            newKey.OwnerUserId = Guid.NewGuid();
            newKey.Secure = false;
            //random.NextBytes(newKey.KeyData);

            Assert.IsTrue(_provider.KeyUpdate(newKey));

            key = newKey;

            newKey = _provider.KeyRetrieve(key.Id);

            Assert.IsNotNull(newKey);
            Assert.IsFalse(newKey.Id.Equals(Guid.Empty));

            Assert.AreEqual(key.BlobId, newKey.BlobId);
            Assert.AreEqual(key.Secure, newKey.Secure);
            Assert.AreEqual(key.OwnerUserId, newKey.OwnerUserId);

            //Assert.IsTrue(key.KeyData.SequenceEqual(newKey.KeyData));

            Assert.IsTrue(_provider.KeyDelete(newKey));

            var k = _provider.KeyRetrieve(newKey.Id);

            Assert.IsNull(k);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Registers the specified username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="publicPrivateKey">The public private key.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException">
        /// Could not create user.
        /// or
        /// Could not create public/private keys.
        /// or
        /// Could not create public key.
        /// or
        /// Could not save user.
        /// </exception>
        public ISdkUser Register(string username, string password, out Stream publicPrivateKey)
        {
            LogVerbose("OjibweClient.Register entering.");
            ISdkUser sdkUser = null;
            ISdkKey sdkPublicKey = null;

            try
            {
                bool success = true;
                IUser user = new User();
                user.Username = username;
                user.PasswordHash = password;
                user.Role = "User";

                sdkUser = _api.Execute<SdkUser>(user, HttpMethod.Post, "users");

                success = sdkUser != null;

                if (!success)
                    throw new ApplicationException("Could not create user.");

                sdkUser = Login(username, password);

                Stream publicKeyStream;
                if (!CryptoUtility.TryGenerateNewAsymetricKeys(out publicKeyStream, out publicPrivateKey))
                    throw new ApplicationException("Could not create public/private keys.");

                IKey publicKey = new Key();
                publicKey.OwnerUserId = sdkUser.Id;
                publicKey.Secure = false;
                publicKey.KeyData = StreamUtility.GetBytes(publicKeyStream);

                sdkPublicKey = _api.Execute<SdkKey>(publicKey, HttpMethod.Post, "keys");

                success = sdkPublicKey != null;

                if (!success)
                    throw new ApplicationException("Could not create public key.");

                sdkUser.PublicKey = sdkPublicKey;
                success = sdkUser.Save();

                sdkUser.PrivateKey = publicPrivateKey;

                if (!success)
                    throw new ApplicationException("Could not save user.");

                LogVerbose("OjibweClient.Register leaving.");
                return sdkUser;
            }
            catch (Exception ex)
            {
                LogError("Failed to Register user.", ex);

                //Cleanup
                publicPrivateKey = Stream.Null;
                if (sdkPublicKey != null)
                    _api.Execute<bool>(null, HttpMethod.Delete, "keys/{id}", "id", sdkPublicKey.Id.ToString());
                if (sdkUser != null)
                    _api.Execute<bool>(null, HttpMethod.Delete, "users/{id}", "id", sdkUser.Id.ToString());
                throw;
            }
        }