Ejemplo n.º 1
0
        public async Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "nonce")] HttpRequest req,
            ILogger log, CancellationToken cancellationToken)
        {
            try
            {
                var data = await req.DeserializeJsonBody <StoreNonceRequest>();

                var verificationQuery = new VerifyRequestQuery(data, req);
                var isVerified        = await mediator.Send(verificationQuery, cancellationToken);

                if (!isVerified)
                {
                    return(new UnauthorizedResult());
                }
                var command = new StoreNonceCommand(data.CovidPass);

                await mediator.Send(command, cancellationToken);

                var query      = new RetrieveNonceQuery(data.CovidPass);
                var nonceEntry = await mediator.Send(query, cancellationToken);

                return(new OkObjectResult(new StoreNonceResponse
                {
                    Nonce = nonceEntry.Nonce
                }));
            }
            catch (DomainException ex)
            {
                var errors = validation.ProcessErrors(ex);
                return(new BadRequestObjectResult(errors));
            }
        }
Ejemplo n.º 2
0
        protected override async Task Handle(SendHeartbeatCommand request, CancellationToken cancellationToken)
        {
            var profile = await repository.GetProfileAsync(request.ProfileId, request.DeviceId, request.CovidPass, cancellationToken);

            if (profile == null)
            {
                throw new DomainException("Profile not found");
            }

            if (!profile.ActiveQuarantine(DateTime.UtcNow))
            {
                throw new DomainException("Profile not in quarantine");
            }

            var nonceQuery = new RetrieveNonceQuery(profile.CovidPass);
            var nonceCache = await mediator.Send(nonceQuery, cancellationToken);

            if (nonceCache == null)
            {
                throw new DomainException("Invalid nonce");
            }

            if (nonceCache.Nonce != request.Nonce)
            {
                throw new DomainException("Invalid nonce");
            }

            profile.UpdateLastPositionReportTime(DateTime.UtcNow);

            await repository.UnitOfWork.SaveChangesAsync(cancellationToken);
        }
        protected override async Task Handle(UpdatePresenceCheckCommand request, CancellationToken cancellationToken)
        {
            var profile = await repository.GetProfileAsync(request.ProfileId, request.DeviceId, request.CovidPass, cancellationToken);

            if (profile == null)
            {
                throw new DomainException("Profile not found");
            }
            var nonceQuery = new RetrieveNonceQuery(profile.CovidPass);
            var nonceCache = await mediator.Send(nonceQuery, cancellationToken);

            if (nonceCache == null)
            {
                throw new DomainException("Invalid nonce");
            }

            if (nonceCache.Nonce != request.Nonce)
            {
                throw new DomainException("Invalid nonce");
            }

            var now = DateTime.UtcNow;
            var pendingSuspectedPresenceCheck = await context.PresenceChecks
                                                .Where(x => x.ProfileId == profile.Id)
                                                .Where(x => x.Status == PresenceCheckStatus.SUSPECTED)
                                                .Where(x => x.CreatedOn <= now && now <= x.DeadLineCheck)
                                                .FirstOrDefaultAsync(cancellationToken);

            if (pendingSuspectedPresenceCheck == null)
            {
                throw new DomainException("There is no active check for given covid pass");
            }

            var newStatus = request.Status switch
            {
                Contracts.Requests.PresenceCheckStatus.OK => PresenceCheckStatus.OK,
                Contracts.Requests.PresenceCheckStatus.LEFT => PresenceCheckStatus.LEFT,
                _ => PresenceCheckStatus.SUSPECTED
            };

            pendingSuspectedPresenceCheck.UpdateStatus(newStatus);
            await context.SaveChangesAsync(cancellationToken);
        }
    }