Exemplo n.º 1
0
        /// <summary>
        /// Creates a new voucher generation instance.
        /// </summary>
        public async Task <(Guid Otc, string Password)> CreateGenerationRequest(
            Source source,
            VoucherCreatePayload.Content creationParameters,
            bool isPreVerified = false
            )
        {
            if (!creationParameters.SourceId.Equals(source.Id.ToString()))
            {
                throw new ArgumentException($"Incoherent POS IDs {creationParameters.SourceId} != {source.Id}");
            }

            // TODO: validate whether source is allowed to generate vouchers (check aims)

            var otc      = Guid.NewGuid();
            var password = creationParameters.Password ?? Random.GeneratePassword(DefaultAutogeneratedPasswordLength);

            var genRequest = new GenerationRequest {
                Otc               = otc,
                Amount            = creationParameters.Vouchers.Length,
                TotalVoucherCount = (from v in creationParameters.Vouchers select v.Count).Sum(),
                CreatedAt         = DateTime.UtcNow,
                Verified          = isPreVerified,
                Attempts          = RequestInitialAttempts,
                SourceId          = source.Id,
                Nonce             = creationParameters.Nonce,
                Password          = password
            };
            await Mongo.AddGenerationRequest(genRequest);

            Logger.LogDebug("Generation request {0} persisted", otc);

            var vouchers = from v in creationParameters.Vouchers
                           let secret = GenerateSecret()
                                        select new Voucher {
                Secret              = secret.ToBase64(),
                AimCode             = v.Aim,
                Position            = GeoJson.Point(GeoJson.Geographic(v.Longitude, v.Latitude)),
                Timestamp           = v.Timestamp,
                Count               = v.Count,
                InitialCount        = v.Count,
                GenerationRequestId = otc
            };
            await Mongo.AddVouchers(vouchers);

            Logger.LogDebug("{0} voucher documents stored for request {1}", vouchers.Count(), otc);

            return(otc, password);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new voucher generation instance.
        /// </summary>
        public async Task <(Guid Otc, string Password)> CreateVoucherGeneration(
            VoucherCreatePayload.Content creationParameters,
            bool isPreVerified = false
            )
        {
            // TODO: validate whether source is allowed to generate vouchers (check aims)

            var otc      = Guid.NewGuid();
            var password = creationParameters.Password ?? _random.GeneratePassword(DefaultAutogeneratedPasswordLength);

            var genRequest = new GenerationRequest {
                Amount      = (ushort)creationParameters.Vouchers.Length,
                OtcGen      = otc,
                CreatedAt   = DateTime.UtcNow,
                Verified    = isPreVerified,
                PerformedAt = null,
                Void        = false,
                SourceId    = creationParameters.SourceId.ToLong(),
                Nonce       = creationParameters.Nonce.FromBase64(),
                Password    = password
            };

            Data.GenerationRequests.Add(genRequest);
            await Data.SaveChangesAsync();

            foreach (var voucher in creationParameters.Vouchers)
            {
                for (int c = 0; c < voucher.Count; ++c)
                {
                    var v = new Voucher {
                        AimCode             = voucher.Aim,
                        Latitude            = voucher.Latitude,
                        Longitude           = voucher.Longitude,
                        Timestamp           = voucher.Timestamp,
                        GenerationRequestId = genRequest.Id,
                        Spent = false
                    };
                    _random.NextBytes(v.Secret);

                    Data.Vouchers.Add(v);
                }
            }
            await Data.SaveChangesAsync();

            return(otc, password);
        }