Beispiel #1
0
        public static Tuple <Jwt, JwtGenerator> PredefinedToken(
            this Faker faker,
            VirgilAccessTokenSigner signer,
            TimeSpan lifeTime,
            out string apiPublicKeyId,
            out string apiPublicKeyBase64)
        {
            var crypto      = new VirgilCrypto();
            var apiKeyPair  = crypto.GenerateKeys();
            var fingerprint = crypto.GenerateHash(crypto.ExportPublicKey(apiKeyPair.PublicKey));

            apiPublicKeyId = Bytes.ToString(fingerprint, StringEncoding.HEX);

            apiPublicKeyBase64 = Bytes.ToString(
                crypto.ExportPublicKey(apiKeyPair.PublicKey), StringEncoding.BASE64);

            var jwtGenerator = new JwtGenerator(
                faker.AppId(),
                apiKeyPair.PrivateKey,
                apiPublicKeyId,
                lifeTime,
                signer);

            var additionalData = new Dictionary <string, string>
            {
                { "username", "some_username" }
            };
            var dict  = additionalData.ToDictionary(entry => (object)entry.Key, entry => (object)entry.Value);
            var token = jwtGenerator.GenerateToken("some_identity", dict);

            return(new Tuple <Jwt, JwtGenerator>(token, jwtGenerator));
        }
Beispiel #2
0
        private Jwt JwtSignedByWrongApiKey(string identity)
        {
            var crypto              = new VirgilCrypto();
            var wrongApiKeyPair     = crypto.GenerateKeys();
            var wrongApiPublicKeyId = faker.AppId();

            return(GenerateJwt(identity, wrongApiKeyPair.PrivateKey, wrongApiPublicKeyId));
        }
Beispiel #3
0
        public void CardManager_Should_RaiseExceptionIfGetsInvalidCard()
        {
            // STC-13
            var verifier = Substitute.For <ICardVerifier>();

            verifier.VerifyCard(Arg.Any <Card>()).Returns(false);

            var signCallBack = Substitute.For <Func <RawSignedModel, Task <RawSignedModel> > >();

            signCallBack.Invoke(Arg.Any <RawSignedModel>()).Returns(args => (RawSignedModel)args[0]);
            var jwtGenerator = new JwtGenerator(
                faker.AppId(),
                IntegrationHelper.ApiPrivateKey(),
                AppSettings.ApiPublicKeyId,
                TimeSpan.FromMinutes(10),
                new VirgilAccessTokenSigner()
                );
            var accessTokenProvider = Substitute.For <IAccessTokenProvider>();
            var identity            = faker.Random.AlphaNumeric(20);
            var token = jwtGenerator.GenerateToken(identity);

            accessTokenProvider.GetTokenAsync(Arg.Any <TokenContext>()).Returns(
                token
                );
            var model = faker.PredefinedRawSignedModel(null, true, true, true);

            var client = Substitute.For <ICardClient>();
            Func <Task <Tuple <RawSignedModel, bool> > > getStub = async() =>
            {
                return(new Tuple <RawSignedModel, bool>(model, false));
            };
            Func <Task <RawSignedModel> > publishStub = async() =>
            {
                return(model);
            };
            Func <Task <IEnumerable <RawSignedModel> > > searchStub = async() =>
            {
                return(new List <RawSignedModel>()
                {
                    model
                });
            };

            client.GetCardAsync(Arg.Any <string>(), Arg.Any <string>()).Returns(getStub.Invoke());
            client.PublishCardAsync(Arg.Any <RawSignedModel>(), Arg.Any <string>()).Returns(publishStub.Invoke());
            client.SearchCardsAsync(Arg.Any <string>(), Arg.Any <string>()).Returns(searchStub.Invoke());

            var cardId             = faker.CardId();
            var searchCardIdentity = faker.Random.AlphaNumeric(20);
            var manager            = new CardManager(new CardManagerParams()
            {
                CardCrypto          = new VirgilCardCrypto(),
                AccessTokenProvider = accessTokenProvider,
                SignCallBack        = signCallBack,
                Verifier            = verifier,
                ApiUrl = AppSettings.CardsServiceAddress
            }
                                                     )
            {
                Client = client
            };

            Assert.Throws <CardVerificationException>(() => manager.ImportCardFromJson(model.ExportAsJson()));
            Assert.Throws <CardVerificationException>(() => manager.ImportCardFromString(model.ExportAsString()));
            Assert.ThrowsAsync <CardVerificationException>(async() => await manager.GetCardAsync(cardId));
            Assert.ThrowsAsync <CardVerificationException>(async() => await manager.PublishCardAsync(model));
            Assert.ThrowsAsync <CardVerificationException>(async() => await manager.SearchCardsAsync(searchCardIdentity));
            Assert.Throws <CardVerificationException>(() => manager.ImportCard(model));
        }