Exemplo n.º 1
0
        public async Task <KeyVm> GetUserKeyAsync(long publicKeyId, long userId, bool isSignKey)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                KeyType keyType = isSignKey ? KeyType.SignAsymKey : KeyType.EncryptionAsymKey;
                Key     key     = await context.Keys.FirstOrDefaultAsync(opt => opt.UserId == userId && opt.KeyId == publicKeyId && opt.Type == keyType).ConfigureAwait(false);

                KeyVm userKey = KeyConverter.GetKeyVm(key);
                if (userKey == null)
                {
                    using (BlockchainDbContext blockchainCtx = new BlockchainDbContext())
                    {
                        var userKeys = await blockchainCtx.BlockSegments
                                       .Where(opt => opt.SegmentHeader.Type == ObjectsLibrary.Blockchain.BlockSegmentType.NewUserKeys && opt.SegmentHeader.Uid == userId)
                                       .Where(opt => ((NewUserKeysBlockData)opt.PublicData).Keys.Any(p => p.KeyId == publicKeyId && p.Type == keyType))
                                       .Select(opt => (NewUserKeysBlockData)opt.PublicData)
                                       .FirstOrDefaultAsync()
                                       .ConfigureAwait(false);

                        if (userKeys == null)
                        {
                            throw new ObjectDoesNotExistsException();
                        }
                        userKey = userKeys.Keys.FirstOrDefault(opt => opt.KeyId == publicKeyId);
                    }
                }
                return(userKey);
            }
        }
Exemplo n.º 2
0
 public static Key GetKey(KeyVm key)
 {
     return(key == null
         ? null
         : new Key
     {
         ChatId = key.ChatId,
         UserId = key.UserId,
         KeyData = key.Data,
         KeyId = key.KeyId,
         Version = key.Version,
         GenerationTimeSeconds = key.GenerationTime,
         ExpirationTimeSeconds = key.GenerationTime + key.Lifetime.GetValueOrDefault(),
         Type = key.Type
     });
 }
Exemplo n.º 3
0
        public async Task <KeyVm> SetNewSymmetricKeyForChat(KeyVm key, long chatId, long userId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                ChatUser chatUser = await context.ChatUsers
                                    .Where(opt => opt.ChatId == chatId &&
                                           opt.UserId == userId &&
                                           opt.UserRole >= UserRole.Admin &&
                                           !opt.Deleted && !opt.Banned)
                                    .FirstOrDefaultAsync()
                                    .ConfigureAwait(false);

                if (chatUser == null)
                {
                    throw new UserIsNotInConversationException();
                }

                Key chatKey = await context.Keys.FirstOrDefaultAsync(opt => opt.ChatId == chatId).ConfigureAwait(false);

                if (chatKey == null)
                {
                    chatKey        = KeyConverter.GetKey(key);
                    chatKey.ChatId = chatId;
                    chatKey.UserId = null;
                    chatKey.KeyId  = RandomExtensions.NextInt64();
                    await context.AddAsync(chatKey).ConfigureAwait(false);
                }
                else
                {
                    chatKey.ChatId  = chatId;
                    chatKey.KeyData = key.Data;
                    chatKey.Version = 1;
                    chatKey.UserId  = null;
                    chatKey.GenerationTimeSeconds = key.GenerationTime;
                    chatKey.ExpirationTimeSeconds = key.GenerationTime + key.Lifetime;
                    context.Update(chatKey);
                }
                await context.SaveChangesAsync().ConfigureAwait(false);

                return(KeyConverter.GetKeyVm(chatKey));
            }
        }