Пример #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
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Passw0rd.RecordUpdater"/> class.
 /// </summary>
 /// <param name="token">Update token to be used for updating user's record.
 /// How to generate Update Token you will find
 /// <see href="https://github.com/passw0rd/cli#get-an-update-token">here</see>.</param>
 public RecordUpdater(string token)
 {
     Validation.NotNullOrWhiteSpace(token, "UpdateToken isn't provided.");
     this.VersionedUpdateToken = StringUpdateTokenParser.Parse(token);
     this.pheClient            = new PheClient();
 }