Пример #1
0
        public async System.Threading.Tasks.Task CachingJwtProvider_Should_ReturnNewTokenIfExpired()
        {
            var provider = new CachingJwtProvider(IntegrationHelper.GetObtainToken(0.001));
            var jwt      = await provider.GetTokenAsync(new TokenContext("some_identity", "sme_operation"));

            var jwt2 = await provider.GetTokenAsync(new TokenContext("some_identity", "sme_operation"));

            Assert.AreNotEqual(jwt, jwt2);
        }
Пример #2
0
        public async System.Threading.Tasks.Task CachingJwtProvider_Should_ReturnTheSameTokenIfValidAsync()
        {
            var provider = new CachingJwtProvider(IntegrationHelper.GetObtainToken(10));
            var jwt      = await provider.GetTokenAsync(new TokenContext("some_identity", "sme_operation"));

            var jwt2 = await provider.GetTokenAsync(new TokenContext("some_identity", "sme_operation"));

            Assert.AreSame(jwt, jwt2);
        }
Пример #3
0
        public static BrainKey Initialize(Func <TokenContext, Task <string> > obtainTokenCallback)
        {
            var connection = new ServiceConnection("https://api.virgilsecurity.com");
            var client     = new PythiaClient(connection, Configuration.Serializer);
            var crypto     = new PythiaCrypto();

            var tokenProvider = new CachingJwtProvider(obtainTokenCallback);
            var brainKey      = new BrainKey(client, crypto, tokenProvider);

            return(brainKey);
        }
        public static PythiaProtocol Initialize(PythiaProtocolConfig config)
        {
            if (config.ProofKeys == null || !config.ProofKeys.Any())
            {
                throw new ArgumentException(
                          $"{nameof(config.ProofKeys)} value cannot be null or empty");
            }

            if (string.IsNullOrWhiteSpace(config.AppId))
            {
                throw new ArgumentException(
                          $"{nameof(config.AppId)} value cannot be null or empty");
            }

            if (string.IsNullOrWhiteSpace(config.ApiKeyId))
            {
                throw new ArgumentException(
                          $"{nameof(config.ApiKeyId)} value cannot be null or empty");
            }

            if (string.IsNullOrWhiteSpace(config.ApiKey))
            {
                throw new ArgumentException(
                          $"{nameof(config.ApiKey)} value cannot be null or empty");
            }

            Func <TokenContext, Task <string> > tokenCallback = (c) =>
            {
                var virgilCrypto = new VirgilCrypto();
                var signer       = new VirgilAccessTokenSigner();

                var apiKey = virgilCrypto.ImportPrivateKey(
                    Bytes.FromString(config.ApiKey, StringEncoding.BASE64));

                var generator = new JwtGenerator(config.AppId, apiKey,
                                                 config.ApiKeyId, TimeSpan.FromDays(1), signer);

                var jwt = generator.GenerateToken("PYTHIA_CLIENT");

                return(Task.FromResult(jwt.ToString()));
            };

            var connection    = new ServiceConnection(config.ApiURL);
            var tokenProvider = new CachingJwtProvider(tokenCallback);

            var client       = new PythiaClient(connection, new NewtonsoftJsonSerializer());
            var pythiaCrypto = new PythiaCrypto();

            var protocol = new PythiaProtocol(client, pythiaCrypto,
                                              tokenProvider, config.ProofKeys);

            return(protocol);
        }