Пример #1
0
        public static ProtocolContext Create(string appId, string accessToken,
                                             string serverPublicKey, string clientSecretKey, string[] updateTokens = null)
        {
            var phe = new PheCrypto();

            var(pkSVer, pkS) = EnsureServerPublicKey(serverPublicKey, phe);
            var(skCVer, skC) = EnsureClientSecretKey(clientSecretKey, phe);

            if (pkSVer != skCVer)
            {
                throw new ArgumentException("Incorrect versions for Server/Client keys.");
            }

            var serializer = new HttpBodySerializer();
            var client     = new PheClient(serializer)
            {
                AccessToken = accessToken,
                BaseUri     = new Uri("https://api.passw0rd.io/")
            };

            var ctx = new ProtocolContext
            {
                AppId  = appId,
                Client = client,
                Crypto = phe
            };

            var serverPksDictionary = new Dictionary <int, PublicKey> {
                [pkSVer] = pkS
            };
            var clientSksDictionary = new Dictionary <int, SecretKey> {
                [skCVer] = skC
            };

            if (updateTokens != null && updateTokens.Length > 0)
            {
                var updateTokenList = updateTokens.Select(UpdateToken.Decode)
                                      .Where(it => it.Version > skCVer)
                                      .OrderBy(it => it.Version)
                                      .ToList();

                ctx.UpdateTokens = updateTokenList;

                foreach (var token in updateTokenList)
                {
                    pkS = phe.RotatePublicKey(pkS, token.A, token.B);
                    skC = phe.RotateSecretKey(skC, token.A, token.B);

                    serverPksDictionary.Add(token.Version, pkS);
                    clientSksDictionary.Add(token.Version, skC);
                }
            }

            ctx.clientSecretKeys = clientSksDictionary;
            ctx.serverPublicKeys = serverPksDictionary;

            return(ctx);
        }
Пример #2
0
        private ProtocolContext InitContext(string applicationToken, string servicePubKey, string clientPrivKey, string serviceSubdomain)
        {
            var serializer = new HttpBodySerializer();
            var serviceUrl = ServiceUrl.ProvideByToken(applicationToken).Replace("api", serviceSubdomain);
            var client     = new PheHttpClient(serializer, applicationToken, serviceUrl);
            var context    = new ProtocolContext(applicationToken, client, servicePubKey, clientPrivKey);

            return(context);
        }
        /// <summary>
        /// Create the context with passw0rd's application credentials.
        /// How to get passw0rd's application credentials
        /// you will find <see href="https://github.com/passw0rd/cli">here</see>.
        /// </summary>
        /// <returns>The new instance of the <see cref="ProtocolContext"/>
        ///  which contains application credentials.</returns>
        /// <param name="appToken">Application token.</param>
        /// <param name="servicePublicKey">Service public key.</param>
        /// <param name="appSecretKey">Application Secret Key.</param>
        /// <param name="updateToken">Update token.
        /// How to generate Update Token you will find
        /// <see href="https://github.com/passw0rd/cli#get-an-update-token">here</see>.</param>
        public static ProtocolContext Create(
            string appToken,
            string servicePublicKey,
            string appSecretKey,
            string updateToken = null)
        {
            Validation.NotNullOrWhiteSpace(appToken, "Application token isn't provided.");
            Validation.NotNullOrWhiteSpace(servicePublicKey, "Service Public Key isn't provided.");
            Validation.NotNullOrWhiteSpace(appSecretKey, "Application Secret Key isn't provided.");

            var serializer = new HttpBodySerializer();

            var client = new PheHttpClient(serializer, appToken, ServiceUrl.ProvideByToken(appToken));

            var ctx = new ProtocolContext(appToken, client, servicePublicKey, appSecretKey);

            if (!string.IsNullOrWhiteSpace(updateToken))
            {
                ctx.UpdatePheClients(updateToken);
            }

            return(ctx);
        }