Exemplo n.º 1
0
 private bool ValidateAuthCheckRequest(AuthenticationCheckRequest req)
 {
     if (req.LoginToken == null || req.LoginToken.Equals("x''") || req.LoginToken.Equals(""))
     {
         return(false);
     }
     if (req.AuthToken == null || req.AuthToken.Equals("x''") || req.AuthToken.Equals(""))
     {
         return(false);
     }
     if (req.UserId <= 0)
     {
         return(false);
     }
     return(true);
 }
Exemplo n.º 2
0
 private void HandleAuthCheckRequest(HttpListenerContext ctx, AuthenticationCheckRequest req)
 {
     try
     {
         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;
             }
             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 is incorrect or expired");
                 return;
             }
             if (!UserVerificationUtil.AuthTokenValid(user, req.AuthToken))
             {
                 WriteBodylessResponse(ctx, 401, "Unauthorized");
                 return;
             }
             WriteBodylessResponse(ctx, 200, "OK");
         }
     }
     catch (HttpListenerException)
     {
         //HttpListeners dispose themselves when an exception occurs, so we can do no more.
     }
     catch (Exception e)
     {
         WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Request for a user to receive authentication to make edits to content they have access to. Documention is found in the Web API Enumeration file
        /// in the /RepairJob/Requirements tab, starting at row 21
        /// </summary>
        /// <param name="ctx">The HttpListenerContext to respond to</param>
        private void HandlePutRequest(HttpListenerContext ctx)
        {
            try
            {
                #region Input Validation
                if (!ctx.Request.HasEntityBody)
                {
                    WriteBodyResponse(ctx, 400, "No Body", "Request lacked a body");
                    return;
                }
                string reqString          = new StreamReader(ctx.Request.InputStream).ReadToEnd();
                AuthenticationRequest req = JsonDataObjectUtil <AuthenticationRequest> .ParseObject(reqString);

                if (req == null)
                {
                    WriteBodyResponse(ctx, 400, "Incorrect Format", "Request was in the wrong format");
                    return;
                }
                if (!ValidateAuthenticationRequest(req))
                {
                    AuthenticationCheckRequest req2 = JsonDataObjectUtil <AuthenticationCheckRequest> .ParseObject(reqString);

                    if (req2 != null && ValidateAuthCheckRequest(req2))
                    {
                        HandleAuthCheckRequest(ctx, req2);
                        return;
                    }
                    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 User Validation
                    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 is incorrect or expired");
                        return;
                    }
                    if (!UserVerificationUtil.VerifyAuthentication(user, req.SecurityQuestion, req.SecurityAnswer))
                    {
                        WriteBodyResponse(ctx, 401, "Unauthorized", "Security Answer was incorrect");
                        return;
                    }
                    #endregion

                    #region Action Handling
                    LoginStatusTokens tokens = UserVerificationUtil.ExtractLoginTokens(user);
                    UserVerificationUtil.GenerateNewAuthToken(tokens);
                    if (!connection.UpdateUsersLoginToken(user, tokens))
                    {
                        WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Failed to write login token to database");
                        return;
                    }
                    JsonDictionaryStringConstructor retConstructor = new JsonDictionaryStringConstructor();
                    retConstructor.SetMapping("token", tokens.AuthToken);
                    WriteBodyResponse(ctx, 200, "OK", retConstructor.ToString());
                    #endregion
                }
            }
            catch (HttpListenerException)
            {
                //HttpListeners dispose themselves when an exception occurs, so we can do no more.
            }
            catch (Exception e)
            {
                WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message);
            }
        }