public async Task SearchForVirgilCards_ValidationWithServiceKey_ShouldPassValidation()
        {
            var crypto = new VirgilCrypto();
            var client = IntergrationHelper.GetVirgilClient();

            client.SetCardValidator(new CardValidator(crypto));

            // CREATING A VIRGIL CARD

            var appKey = crypto.ImportPrivateKey(IntergrationHelper.AppKey, IntergrationHelper.AppKeyPassword);

            var aliceKeys         = crypto.GenerateKeys();
            var exportedPublicKey = crypto.ExportPublicKey(aliceKeys.PublicKey);

            var aliceIdentity = "alice-" + Guid.NewGuid();
            var request       = new CreateCardRequest(aliceIdentity, "member", exportedPublicKey);

            var requestSigner = new RequestSigner(crypto);

            requestSigner.SelfSign(request, aliceKeys.PrivateKey);
            requestSigner.AuthoritySign(request, IntergrationHelper.AppID, appKey);

            var aliceCard = await client.CreateCardAsync(request);

            // VALIDATING A VIRGIL CARD

            var cards = await client.SearchCardsAsync(SearchCriteria.ByIdentity(aliceIdentity));

            aliceCard.ShouldBeEquivalentTo(cards.Single());

            await IntergrationHelper.RevokeCard(aliceCard.Id);
        }
示例#2
0
        public async Task GetRevokedCard_ExistingCard_ShouldThrowException()
        {
            VirgilConfig.Initialize(IntergrationHelper.AppAccessToken);
            VirgilConfig.SetKeyStorage(new KeyStorageFake());

            // Application Credentials

            var appKey = VirgilKey.FromFile(IntergrationHelper.AppKeyPath, IntergrationHelper.AppKeyPassword);
            var appID  = IntergrationHelper.AppID;

            // Create a Virgil Card

            var          identity = "Alice-" + Guid.NewGuid();
            const string type     = "member";

            var aliceKey = VirgilKey.Create("alice_key");
            var request  = aliceKey.BuildCardRequest(identity, type);

            appKey.SignRequest(request, appID);
            var aliceCard = await VirgilCard.CreateAsync(request);

            // Revoke a Virgil Card

            await IntergrationHelper.RevokeCard(aliceCard.Id);

            aliceKey.Destroy();

            Assert.ThrowsAsync <VirgilClientException>(async() => await VirgilCard.GetAsync(aliceCard.Id));
        }
        public async Task GetSignleVirgilCard_ByGivenId_ShouldReturnVirgilCard()
        {
            var crypto        = new VirgilCrypto();
            var client        = IntergrationHelper.GetVirgilClient();
            var requestSigner = new RequestSigner(crypto);

            var appKey = crypto.ImportPrivateKey(IntergrationHelper.AppKey, IntergrationHelper.AppKeyPassword);

            // Prepare Requests

            var aliceIdentity  = "alice-" + Guid.NewGuid();
            var aliceKeys      = crypto.GenerateKeys();
            var alicePublicKey = crypto.ExportPublicKey(aliceKeys.PublicKey);

            var aliceRequest = new CreateCardRequest(aliceIdentity, "member", alicePublicKey);

            requestSigner.SelfSign(aliceRequest, aliceKeys.PrivateKey);
            requestSigner.AuthoritySign(aliceRequest, IntergrationHelper.AppID, appKey);

            var aliceCard = await client.CreateCardAsync(aliceRequest);

            var foundAliceCard = await client.GetCardAsync(aliceCard.Id);

            aliceCard.ShouldBeEquivalentTo(foundAliceCard);

            await IntergrationHelper.RevokeCard(aliceCard.Id);
        }
        public async Task CreateNewVirgilCard_SignatureValidation_ShouldPassValidation()
        {
            var crypto = new VirgilCrypto();
            var client = IntergrationHelper.GetVirgilClient();

            // CREATING A VIRGIL CARD

            var appKey = crypto.ImportPrivateKey(IntergrationHelper.AppKey, IntergrationHelper.AppKeyPassword);

            var aliceKeys         = crypto.GenerateKeys();
            var exportedPublicKey = crypto.ExportPublicKey(aliceKeys.PublicKey);

            var aliceIdentity = "alice-" + Guid.NewGuid();
            var request       = new CreateCardRequest(aliceIdentity, "member", exportedPublicKey);

            var requestSigner = new RequestSigner(crypto);

            requestSigner.SelfSign(request, aliceKeys.PrivateKey);
            requestSigner.AuthoritySign(request, IntergrationHelper.AppID, appKey);

            var aliceCard = await client.CreateCardAsync(request);

            // VALIDATING A VIRGIL CARD

            var appPublicKey         = crypto.ExtractPublicKey(appKey);
            var exportedAppPublicKey = crypto.ExportPublicKey(appPublicKey);

            var validator = new CardValidator(crypto);

            validator.AddVerifier(IntergrationHelper.AppID, exportedAppPublicKey);

            validator.Validate(aliceCard).Should().BeTrue();

            await IntergrationHelper.RevokeCard(aliceCard.Id);
        }
        public async Task CreateNewVirgilCard_IdentityAndPublicKeyGiven_ShouldBeFoundByIdentity()
        {
            var crypto = new VirgilCrypto();
            var client = IntergrationHelper.GetVirgilClient();

            var appKey = crypto.ImportPrivateKey(IntergrationHelper.AppKey, IntergrationHelper.AppKeyPassword);

            var aliceKeys         = crypto.GenerateKeys();
            var exportedPublicKey = crypto.ExportPublicKey(aliceKeys.PublicKey);

            var aliceIdentity = "alice-" + Guid.NewGuid();

            var request = new CreateCardRequest(aliceIdentity, "member", exportedPublicKey);

            var requestSigner = new RequestSigner(crypto);

            requestSigner.SelfSign(request, aliceKeys.PrivateKey);
            requestSigner.AuthoritySign(request, IntergrationHelper.AppID, appKey);

            var newCard = await client.CreateCardAsync(request);

            var cards = await client.SearchCardsAsync(new SearchCriteria { Identities = new[] { aliceIdentity } });

            cards.Should().HaveCount(1);
            var foundCard = cards.Single();

            newCard.ShouldBeEquivalentTo(foundCard);
        }
        public async Task SearchForTheVirgilCards_MultipleIdentitiesGiven_ShouldReturnVirgilCards()
        {
            // Initialization

            var crypto        = new VirgilCrypto();
            var client        = IntergrationHelper.GetVirgilClient();
            var requestSigner = new RequestSigner(crypto);

            var appKey = crypto.ImportPrivateKey(IntergrationHelper.AppKey, IntergrationHelper.AppKeyPassword);

            // Prepare Requests

            var aliceIdentity  = "alice-" + Guid.NewGuid();
            var aliceKeys      = crypto.GenerateKeys();
            var alicePublicKey = crypto.ExportPublicKey(aliceKeys.PublicKey);

            var bobIdentity  = "bob-" + Guid.NewGuid();
            var bobKeys      = crypto.GenerateKeys();
            var bobPublicKey = crypto.ExportPublicKey(bobKeys.PublicKey);

            var aliceRequest = new CreateCardRequest(aliceIdentity, "member", alicePublicKey);
            var bobRequest   = new CreateCardRequest(bobIdentity, "member", bobPublicKey);

            requestSigner.SelfSign(aliceRequest, aliceKeys.PrivateKey);
            requestSigner.AuthoritySign(aliceRequest, IntergrationHelper.AppID, appKey);

            requestSigner.SelfSign(bobRequest, bobKeys.PrivateKey);
            requestSigner.AuthoritySign(bobRequest, IntergrationHelper.AppID, appKey);

            // Publish Virgil Cards

            var aliceCard = await client.CreateCardAsync(aliceRequest);

            var bobCard = await client.CreateCardAsync(bobRequest);

            // Search for the Virgil Cards

            var foundCards = await client.SearchCardsAsync(new SearchCriteria
            {
                Identities = new[] { bobIdentity, aliceIdentity }
            });

            // Assertions

            foundCards.Should().HaveCount(2);

            foundCards.Single(it => it.Id == aliceCard.Id).ShouldBeEquivalentTo(aliceCard);
            foundCards.Single(it => it.Id == bobCard.Id).ShouldBeEquivalentTo(bobCard);

            await IntergrationHelper.RevokeCard(aliceCard.Id);

            await IntergrationHelper.RevokeCard(bobCard.Id);
        }
        public async Task CreateNewVirgilCard_DuplicateCardCreation_ShouldThrowException()
        {
            var crypto = new VirgilCrypto();
            var client = IntergrationHelper.GetVirgilClient();

            var appKey = crypto.ImportPrivateKey(IntergrationHelper.AppKey, IntergrationHelper.AppKeyPassword);

            var aliceKeys         = crypto.GenerateKeys();
            var exportedPublicKey = crypto.ExportPublicKey(aliceKeys.PublicKey);

            var aliceIdentity = "alice-" + Guid.NewGuid();
            var request       = new CreateCardRequest(aliceIdentity, "member", exportedPublicKey);

            var requestSigner = new RequestSigner(crypto);

            requestSigner.SelfSign(request, aliceKeys.PrivateKey);
            requestSigner.AuthoritySign(request, IntergrationHelper.AppID, appKey);

            var virgilCard = await client.CreateCardAsync(request);

            Assert.ThrowsAsync <VirgilClientException>(async() => await client.CreateCardAsync(request));
        }
示例#8
0
        public async Task EncryptAndSignData_MultipleRecipients_ShouldDecryptAndVerifyDataSuccessfully()
        {
            VirgilConfig.Initialize(IntergrationHelper.AppAccessToken);
            VirgilConfig.SetKeyStorage(new KeyStorageFake());

            var appKey = IntergrationHelper.GetVirgilAppKey();

            var aliceKey = VirgilKey.Create("alice_key");
            var bobKey   = VirgilKey.Create("bob_key");

            var aliceIdentity = $"Alice-{Guid.NewGuid()}";
            var bobIdentity   = $"Bob-{Guid.NewGuid()}";

            var aliceCardRequest = aliceKey.BuildCardRequest(aliceIdentity, "member");
            var bobCardRequest   = bobKey.BuildCardRequest(bobIdentity, "member");

            appKey.SignRequest(aliceCardRequest, IntergrationHelper.AppID);
            appKey.SignRequest(bobCardRequest, IntergrationHelper.AppID);

            await VirgilCard.CreateAsync(aliceCardRequest);

            await VirgilCard.CreateAsync(bobCardRequest);

            var cards     = (await VirgilCard.FindAsync(new[] { aliceIdentity, bobIdentity })).ToList();
            var plaintext = Encoding.UTF8.GetBytes("Hello Bob!");

            var cipherData    = aliceKey.SignThenEncrypt(plaintext, cards);
            var decryptedData = bobKey.DecryptThenVerify(cipherData, cards.Single(it => it.Identity == aliceIdentity));

            decryptedData.ShouldBeEquivalentTo(plaintext);

            await Task.WhenAll(cards.Select(it => IntergrationHelper.RevokeCard(it.Id)));

            aliceKey.Destroy();
            bobKey.Destroy();
        }