Ejemplo n.º 1
0
        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);
        }
        private static string MakeToken(AuthorizedTask authorizedTask)
        {
            var formatter = new AuthorizedTaskTokenFormatter();

            return(formatter.Format(new AuthorizedTaskTokenParts()
            {
                AuthorizationCode = authorizedTask.AuthorizationCode,
                AuthorizedTaskId = authorizedTask.AuthorizedTaskId
            }));
        }
        private bool IsExpired(AuthorizedTask task, IExecutionContext executionContext)
        {
            // Null indicates expiry disabled
            if (!task.ExpiryDate.HasValue)
            {
                return(false);
            }

            return(task.ExpiryDate < executionContext.ExecutionDate);
        }
        private AuthorizedTaskTokenValidationResult Validate(
            AuthorizedTask authorizedTask,
            AuthorizedTaskTokenParts tokenParts,
            ValidateAuthorizedTaskTokenQuery query,
            IExecutionContext executionContext
            )
        {
            if (authorizedTask == null || !ConstantEquals(authorizedTask.AuthorizationCode, tokenParts.AuthorizationCode))
            {
                return(NotFoundResult());
            }

            if (authorizedTask.User.IsSystemAccount)
            {
                throw new InvalidAuthorizedTaskTokenException(query, "The system account cannot be used for authorized tasks");
            }

            if (authorizedTask.CompletedDate.HasValue)
            {
                return(new AuthorizedTaskTokenValidationResult(AuthorizedTaskValidationErrors.TokenValidation.AlreadyComplete.Create()));
            }

            if (authorizedTask.InvalidatedDate.HasValue || !authorizedTask.User.IsEnabled())
            {
                return(new AuthorizedTaskTokenValidationResult(AuthorizedTaskValidationErrors.TokenValidation.Invalidated.Create()));
            }

            if (IsExpired(authorizedTask, executionContext))
            {
                return(new AuthorizedTaskTokenValidationResult(AuthorizedTaskValidationErrors.TokenValidation.Expired.Create()));
            }

            return(new AuthorizedTaskTokenValidationResult()
            {
                IsSuccess = true
            });
        }