예제 #1
0
        /// <summary>
        /// Request for retrieving a user's security question. Documention is found in the Web API Enumeration file
        /// in the /User/Auth tab, starting at row 1
        /// </summary>
        /// <param name="ctx">The HttpListenerContext to respond to</param>
        private void HandlePostRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "No Body", "Request lacked a body");
                    return;
                }
                SecurityQuestionRequest req = JsonDataObjectUtil <SecurityQuestionRequest> .ParseObject(ctx);

                if (req == null)
                {
                    WriteBodyResponse(ctx, 400, "Incorrect Format", "Request was in the wrong format");
                    return;
                }
                if (!ValidateSecurityQuestionRequest(req))
                {
                    WriteBodyResponse(ctx, 400, "Incorrect Format", "Not all fields of the request were filled");
                    return;
                }
                #endregion

                MySqlDataManipulator connection = new MySqlDataManipulator();
                using (connection)
                {
                    bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString());
                    if (!res)
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected ServerError", "Connection to database failed");
                        return;
                    }
                    #region Action Handling
                    var user = connection.GetUserById(req.UserId);
                    if (user == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(user, req.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Unauthorized", "Login Token was incorrect or expired");
                        return;
                    }
                    WriteBodyResponse(ctx, 200, "OK", user.SecurityQuestion);
                    #endregion
                }
            }
            catch (HttpListenerException)
            {
                //HttpListeners dispose themselves when an exception occurs, so we can do no more.
            }
            catch (Exception)
            {
                WriteBodylessResponse(ctx, 500, "Internal Server Error");
            }
        }
예제 #2
0
 private bool ValidateSecurityQuestionRequest(SecurityQuestionRequest req)
 {
     if (req == null)
     {
         return(false);
     }
     if (req.LoginToken == null || req.LoginToken.Equals("") || req.LoginToken.Equals("x''"))
     {
         return(false);
     }
     return(req.UserId > 0);
 }