Example #1
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);
        }