Example #1
0
 public static void Serialize(AttemptToken token, Stream stream)
 {
     using (BinaryWriter writer = new BinaryWriter(stream))
     {
         writer.Write(token.InterviewQuestionID.ToByteArray(), 0, 16);
         writer.Write(token.Random);
         writer.Write(token.MostRecentAttemptID.ToByteArray(), 0, 16);
     }
 }
Example #2
0
 public static void Serialize(AttemptToken token, Stream stream)
 {
     using (BinaryWriter writer = new BinaryWriter(stream))
     {
         writer.Write(token.InterviewQuestionID.ToByteArray(), 0, 16);
         writer.Write(token.Random);
         writer.Write(token.MostRecentAttemptID.ToByteArray(), 0, 16);
     }
 }
        private static void ValidateAttemptToken(DbContext context, Guid interviewQuestionId, AttemptToken attemptToken, InterviewToken interviewToken)
        {
            if (attemptToken.InterviewQuestionID != interviewQuestionId)
            {
                throw new Common.Exceptions.ValidationException("Please download the input file again.");
            }

            Guid mostRecentAttemptId = context.Attempts
                .Where(a => a.InterviewQuestionID == interviewQuestionId)
                .OrderByDescending(a => a.CreatedDate)
                .Select(a => a.ID)
                .FirstOrDefault();

            if (attemptToken.MostRecentAttemptID != mostRecentAttemptId)
            {
                throw new Common.Exceptions.ValidationException("Please download the input file again.");
            }
        }
        public CreateAttemptTokenResponse CreateAttemptToken(CreateAttemptTokenRequest request)
        {
            CreateAttemptTokenResponse response = new CreateAttemptTokenResponse();

            try
            {
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.Token, "Token");

                InterviewToken token = InterviewToken.FromBytes(EncryptionHelper.DecryptURL(Convert.FromBase64String(request.Token)));

                DbContext context = DataController.CreateDbContext();

                long randomizer = Common.Helpers.RandomHelper.RandomLong();

                Guid mostRecentAttemptId = context.Attempts
                    .Where(a => a.InterviewQuestionID == request.QuestionID)
                    .OrderByDescending(a => a.CreatedDate)
                    .Select(a => a.ID)
                    .FirstOrDefault();

                AttemptToken attemptToken = new AttemptToken(request.QuestionID, randomizer, mostRecentAttemptId);
                response.AttemptToken = Convert.ToBase64String(EncryptionHelper.EncryptToken(attemptToken.AsBytes()));
            }
            catch (AuthenticationException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Common.Exceptions.ValidationException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                ExceptionHelper.Log(ex, null);
                throw new WebFaultException<string>("An unknown error has occurred.", System.Net.HttpStatusCode.InternalServerError);
            }

            return response;
        }
        private static E::Attempt CreateAttempt(Guid interviewQuestionId, string code, string output, AttemptToken attemptToken)
        {
            E::Attempt attempt = new E::Attempt();

            attempt.ID = Guid.NewGuid();
            attempt.InterviewQuestionID = interviewQuestionId;
            attempt.Code = Guid.NewGuid();
            attempt.Output = Guid.NewGuid();
            attempt.CreatedBy = string.Empty;
            attempt.LastUpdatedBy = string.Empty;
            attempt.CreatedDate = DateTime.UtcNow;
            attempt.LastUpdatedDate = DateTime.UtcNow;
            attempt.Randomizer = attemptToken.Random;

            DataController.UploadBlob(attempt.Code.ToString(), code);
            DataController.UploadBlob(attempt.Output.ToString(), output);

            return attempt;
        }