示例#1
0
        public async Task <IList <UserKeyPair> > ListAsync()
        {
            IList <UserKeyPair> localKeys = await _localService.ListAsync().Free();

            if (New <AxCryptOnlineState>().IsOffline)
            {
                return(localKeys);
            }

            try
            {
                IList <UserKeyPair> remoteKeys = new List <UserKeyPair>();
                try
                {
                    remoteKeys = await _remoteService.ListAsync().Free();
                }
                catch (PasswordException pex)
                {
                    if (localKeys.Count == 0)
                    {
                        throw;
                    }
                    New <IReport>().Exception(pex);
                    return(localKeys);
                }

                IList <UserKeyPair> allKeys = remoteKeys.Union(localKeys).ToList();
                if (allKeys.Count() == 0)
                {
                    UserKeyPair currentKeyPair = await _remoteService.CurrentKeyPairAsync().Free();

                    if (currentKeyPair != null)
                    {
                        remoteKeys.Add(currentKeyPair);
                    }
                    allKeys = remoteKeys;
                }
                if (allKeys.Count() > remoteKeys.Count())
                {
                    await _remoteService.SaveAsync(allKeys).Free();
                }
                if (allKeys.Count() > localKeys.Count())
                {
                    await _localService.SaveAsync(allKeys).Free();
                }
                return(allKeys);
            }
            catch (ApiException aex)
            {
                await aex.HandleApiExceptionAsync();
            }
            return(localKeys);
        }
        private IList <UserKeyPair> TryLoadUserKeyPairs()
        {
            IEnumerable <AccountKey> userAccountKeys = LoadUserAccount().AccountKeys;
            List <UserKeyPair>       userKeys        = LoadValidUserKeysFromAccountKeys(userAccountKeys).ToList();

            if (!userKeys.Any())
            {
                IEnumerable <UserKeyPair> fromKeyPairFiles = UserKeyPair.Load(UserKeyPairFiles(), Identity.UserEmail, Identity.Passphrase);
                fromKeyPairFiles = userKeys.Where(uk => !userAccountKeys.Any(ak => new PublicKeyThumbprint(ak.Thumbprint) == uk.KeyPair.PublicKey.Thumbprint));
                userKeys.AddRange(fromKeyPairFiles);
            }

            return(userKeys);
        }
示例#3
0
        public async Task <UserKeyPair> CurrentKeyPairAsync()
        {
            if (New <AxCryptOnlineState>().IsOnline)
            {
                try
                {
                    UserKeyPair currentUserKeyPair = await _remoteService.CurrentKeyPairAsync().Free();

                    return(currentUserKeyPair);
                }
                catch (ApiException aex)
                {
                    await aex.HandleApiExceptionAsync();
                }
            }
            return(await _localService.CurrentKeyPairAsync().Free());
        }
        public async Task <UserKeyPair> CurrentKeyPairAsync()
        {
            if (Identity.UserEmail == EmailAddress.Empty)
            {
                throw new InvalidOperationException("The account service requires a user.");
            }

            UserAccount userAccount = LoadUserAccount();
            UserKeyPair keyPair     = userAccount.AccountKeys.Select(ak => ak.ToUserKeyPair(Identity.Passphrase)).OrderByDescending(ukp => ukp.Timestamp).FirstOrDefault();

            if (keyPair == null)
            {
                AccountStorage store = new AccountStorage(New <LogOnIdentity, IAccountService>(Identity));
                keyPair = new UserKeyPair(Identity.UserEmail, New <INow>().Utc, New <KeyPairService>().New());
                await store.ImportAsync(keyPair);
            }

            return(keyPair);
        }