private AuthorizedTask MapAuthorizedTask( AddAuthorizedTaskCommand command, IAuthorizedTaskTypeDefinition authorizedTaskTypeDefinition, User user, long?ipAddressId, IExecutionContext executionContext ) { string token = _authorizedTaskAuthorizationCodeGenerator.Generate(); var authorizedTask = new AuthorizedTask() { User = user, AuthorizedTaskId = Guid.NewGuid(), CreateDate = executionContext.ExecutionDate, IPAddressId = ipAddressId, AuthorizationCode = token, AuthorizedTaskTypeCode = authorizedTaskTypeDefinition.AuthorizedTaskTypeCode, TaskData = command.TaskData }; if (command.ExpireAfter > TimeSpan.Zero) { authorizedTask.ExpiryDate = executionContext.ExecutionDate.Add(command.ExpireAfter.Value); } return(authorizedTask); }
/// <summary> /// Constructs a new <see cref="InvalidAuthorizedTaskTypeDefinitionException"/> instance. /// </summary> /// <param name="message">The exception message.</param> /// <param name="invalidDefinition">The authorized task type definition that caused the exception.</param> /// <param name="allDefinitions">Optional collection of all the authorized task type definitions when available.</param> public InvalidAuthorizedTaskTypeDefinitionException( string message, IAuthorizedTaskTypeDefinition invalidDefinition, IEnumerable <IAuthorizedTaskTypeDefinition> allDefinitions = null ) : base(message) { InvalidDefinition = invalidDefinition; AllDefinitions = allDefinitions?.ToArray(); }
private async Task ValidateRateLimitAsync( long?ipAddressId, AddAuthorizedTaskCommand command, IAuthorizedTaskTypeDefinition authorizedTaskTypeDefinition, IExecutionContext executionContext ) { // Rate limiting may not be enabled or ip may be null if IP logging is completely disabled if (!ipAddressId.HasValue || command.RateLimit == null || !command.RateLimit.HasValidQuantity() ) { return; } var dbQuery = _dbContext .AuthorizedTasks .Where(t => t.AuthorizedTaskTypeCode == authorizedTaskTypeDefinition.AuthorizedTaskTypeCode && t.IPAddressId == ipAddressId.Value && t.CreateDate <= executionContext.ExecutionDate ); if (command.RateLimit.HasValidWindow()) { var dateToDetectAttempts = executionContext.ExecutionDate.Add(-command.RateLimit.Window); dbQuery = dbQuery.Where(t => t.CreateDate > dateToDetectAttempts); } var numTasks = await dbQuery.CountAsync(); if (numTasks >= command.RateLimit.Quantity) { AuthorizedTaskValidationErrors.Create.RateLimitExceeded.Throw(); } }