Example #1
0
 private async Task CreateSessionAsync(Guid sessionId, User user,
                                       string ipAddress = null, string userAgent = null)
 {
     var session = new UserSession(sessionId, user.UserId,
                                   _encrypter.GetRandomSecureKey(), ipAddress, userAgent);
     await _userSessionRepository.AddAsync(session);
 }
Example #2
0
        public async Task CreateAsync(Guid id, string userId)
        {
            var isValid    = false;
            var currentTry = 0;
            var key        = string.Empty;

            while (currentTry < RetryTimes)
            {
                key     = _encrypter.GetRandomSecureKey();
                isValid = (await _repository.GetByKeyAsync(key)).HasNoValue;
                if (isValid)
                {
                    break;
                }

                currentTry++;
            }

            if (!isValid)
            {
                throw new ServiceException("Could not create an API key, please try again.");
            }

            var apiKey = new ApiKey(id, key, userId);
            await _repository.AddAsync(apiKey);
        }
Example #3
0
        private async Task CreateAsync(User user)
        {
            var apiKeysCount = await _database.ApiKeys().CountAsync(x => x.UserId == user.Id);

            if (apiKeysCount >= _featureSettings.MaxApiKeys)
            {
                throw new ServiceException($"Limit of {_featureSettings.MaxApiKeys} " +
                                           "API keys has been reached.");
            }

            var isValid    = false;
            var currentTry = 0;
            var key        = string.Empty;

            while (currentTry < RetryTimes)
            {
                key     = _encrypter.GetRandomSecureKey();
                isValid = await _database.ApiKeys().ExistsAsync(key) == false;

                if (isValid)
                {
                    break;
                }

                currentTry++;
            }

            if (!isValid)
            {
                throw new ServiceException("Could not create an API key, please try again.");
            }

            var apiKey = new ApiKey(key, user);
            await _database.ApiKeys().InsertOneAsync(apiKey);

            Logger.Info($"New API key with id: '{apiKey.Id}' was created by user: '******'.");
        }
Example #4
0
        public async Task CreateAsync(string box, string key, object value, string author, string encryptionKey)
        {
            var entryBox = await _boxRepository.GetAsync(box);

            if (entryBox == null)
            {
                throw new ArgumentException($"Box '{box}' has not been found.");
            }

            if (entryBox.Entries.Count() >= _featureSettings.EntriesPerBoxLimit)
            {
                throw new InvalidOperationException($"Box: '{box}' already contains " +
                                                    $"{_featureSettings.EntriesPerBoxLimit} entries.");
            }

            var randomSecureKey   = _encrypter.GetRandomSecureKey();
            var salt              = _encrypter.GetSalt(randomSecureKey);
            var serializedValue   = JsonConvert.SerializeObject(value);
            var encryptedValue    = _encrypter.Encrypt(serializedValue, salt, encryptionKey);
            var entry             = new Entry(key, encryptedValue, salt, author);
            var deserializedEntry = JsonConvert.SerializeObject(entry);
            var entrySize         = Encoding.UTF8.GetByteCount(deserializedEntry);

            if (entrySize > _featureSettings.EntrySizeBytesLimit)
            {
                throw new InvalidOperationException($"Entry size is too big: {entrySize} bytes " +
                                                    $"(limit: {_featureSettings.EntrySizeBytesLimit} bytes).");
            }

            await DeleteAsync(box, key);

            entryBox.AddEntry(entry);
            await _boxRepository.UpdateAsync(entryBox);

            Logger.Information($"Eentry '{key}' was added to the box '{box}'.");
        }
 public async Task CreateAsync(Guid id, string type, string user, DateTime expiry)
 {
     var token     = _encrypter.GetRandomSecureKey();
     var operation = new OneTimeSecuredOperation(id, type, user, token, expiry);
     await _oneTimeSecuredOperationRepository.AddAsync(operation);
 }