Example #1
0
        public async Task ImportCiphersAsync(
            List <Folder> folders,
            List <CipherDetails> ciphers,
            IEnumerable <KeyValuePair <int, int> > folderRelationships)
        {
            foreach (var cipher in ciphers)
            {
                cipher.SetNewId();

                if (cipher.UserId.HasValue && cipher.Favorite)
                {
                    cipher.Favorites = $"{{\"{cipher.UserId.ToString().ToUpperInvariant()}\":\"true\"}}";
                }
            }

            // Init. ids for folders
            foreach (var folder in folders)
            {
                folder.SetNewId();
            }

            // Create the folder associations based on the newly created folder ids
            foreach (var relationship in folderRelationships)
            {
                var cipher = ciphers.ElementAtOrDefault(relationship.Key);
                var folder = folders.ElementAtOrDefault(relationship.Value);

                if (cipher == null || folder == null)
                {
                    continue;
                }

                cipher.Folders = $"{{\"{cipher.UserId.ToString().ToUpperInvariant()}\":" +
                                 $"\"{folder.Id.ToString().ToUpperInvariant()}\"}}";
            }

            // Create it all
            await _cipherRepository.CreateAsync(ciphers, folders);

            // push
            var userId = folders.FirstOrDefault()?.UserId ?? ciphers.FirstOrDefault()?.UserId;

            if (userId.HasValue)
            {
                await _pushService.PushSyncVaultAsync(userId.Value);
            }
        }
        public async Task <Tuple <bool, string> > SignUpPremiumAsync(User user, string paymentToken,
                                                                     PaymentMethodType paymentMethodType, short additionalStorageGb, UserLicense license,
                                                                     TaxInfo taxInfo)
        {
            if (user.Premium)
            {
                throw new BadRequestException("Already a premium user.");
            }

            if (additionalStorageGb < 0)
            {
                throw new BadRequestException("You can't subtract storage!");
            }

            if ((paymentMethodType == PaymentMethodType.GoogleInApp ||
                 paymentMethodType == PaymentMethodType.AppleInApp) && additionalStorageGb > 0)
            {
                throw new BadRequestException("You cannot add storage with this payment method.");
            }

            string          paymentIntentClientSecret = null;
            IPaymentService paymentService            = null;

            if (_globalSettings.SelfHosted)
            {
                if (license == null || !_licenseService.VerifyLicense(license))
                {
                    throw new BadRequestException("Invalid license.");
                }

                if (!license.CanUse(user))
                {
                    throw new BadRequestException("This license is not valid for this user.");
                }

                var dir = $"{_globalSettings.LicenseDirectory}/user";
                Directory.CreateDirectory(dir);
                File.WriteAllText($"{dir}/{user.Id}.json", JsonConvert.SerializeObject(license, Formatting.Indented));
            }
            else
            {
                paymentIntentClientSecret = await _paymentService.PurchasePremiumAsync(user, paymentMethodType,
                                                                                       paymentToken, additionalStorageGb, taxInfo);
            }

            user.Premium      = true;
            user.RevisionDate = DateTime.UtcNow;

            if (_globalSettings.SelfHosted)
            {
                user.MaxStorageGb          = 10240; // 10 TB
                user.LicenseKey            = license.LicenseKey;
                user.PremiumExpirationDate = license.Expires;
            }
            else
            {
                user.MaxStorageGb = (short)(1 + additionalStorageGb);
                user.LicenseKey   = CoreHelpers.SecureRandomString(20);
            }

            try
            {
                await SaveUserAsync(user);

                await _pushService.PushSyncVaultAsync(user.Id);

                await _referenceEventService.RaiseEventAsync(
                    new ReferenceEvent(ReferenceEventType.UpgradePlan, user)
                {
                    Storage  = user.MaxStorageGb,
                    PlanName = PremiumPlanId,
                });
            }
            catch when(!_globalSettings.SelfHosted)
            {
                await paymentService.CancelAndRecoverChargesAsync(user);

                throw;
            }
            return(new Tuple <bool, string>(string.IsNullOrWhiteSpace(paymentIntentClientSecret),
                                            paymentIntentClientSecret));
        }
Example #3
0
        public async Task SignUpPremiumAsync(User user, string paymentToken, PaymentMethodType?paymentMethodType,
                                             short additionalStorageGb, UserLicense license)
        {
            if (user.Premium)
            {
                throw new BadRequestException("Already a premium user.");
            }

            if (additionalStorageGb < 0)
            {
                throw new BadRequestException("You can't subtract storage!");
            }

            IPaymentService paymentService = null;

            if (_globalSettings.SelfHosted)
            {
                if (license == null || !_licenseService.VerifyLicense(license))
                {
                    throw new BadRequestException("Invalid license.");
                }

                if (!license.CanUse(user))
                {
                    throw new BadRequestException("This license is not valid for this user.");
                }

                var dir = $"{_globalSettings.LicenseDirectory}/user";
                Directory.CreateDirectory(dir);
                File.WriteAllText($"{dir}/{user.Id}.json", JsonConvert.SerializeObject(license, Formatting.Indented));
            }
            else if (!string.IsNullOrWhiteSpace(paymentToken))
            {
                if (!paymentMethodType.HasValue)
                {
                    if (paymentToken.StartsWith("tok_"))
                    {
                        paymentMethodType = PaymentMethodType.Card;
                    }
                    else if (paymentToken.StartsWith("btok_"))
                    {
                        paymentMethodType = PaymentMethodType.BankAccount;
                    }
                    else
                    {
                        paymentMethodType = PaymentMethodType.PayPal;
                    }
                }

                await _paymentService.PurchasePremiumAsync(user, paymentMethodType.Value,
                                                           paymentToken, additionalStorageGb);
            }
            else
            {
                throw new InvalidOperationException("License or payment token is required.");
            }

            user.Premium      = true;
            user.RevisionDate = DateTime.UtcNow;

            if (_globalSettings.SelfHosted)
            {
                user.MaxStorageGb          = 10240; // 10 TB
                user.LicenseKey            = license.LicenseKey;
                user.PremiumExpirationDate = license.Expires;
            }
            else
            {
                user.MaxStorageGb = (short)(1 + additionalStorageGb);
                user.LicenseKey   = CoreHelpers.SecureRandomString(20);
            }

            try
            {
                await SaveUserAsync(user);

                await _pushService.PushSyncVaultAsync(user.Id);
            }
            catch when(!_globalSettings.SelfHosted)
            {
                await paymentService.CancelAndRecoverChargesAsync(user);

                throw;
            }
        }