Ejemplo n.º 1
0
            public static responseVal SendRequest(string reqMeta, string requestData, bool skipVerify)
            {
                responseVal responseData;
                string      endpoint = reqMeta + "/auth/agent/win";

                responseData.status = "failed";
                responseData.intent = "";
                responseData.data   = new string[6];
                responseData.error  = "";
                responseData.reason = "";



                try
                {
                    //  Prompt.ShowDialog(reqMeta);
                    // Create a request using a URL that can receive a post.
                    //   WebRequest request = WebRequest.Create(endpoint);

                    HttpWebRequest request = HttpWebRequest.CreateHttp(endpoint);

                    if (skipVerify)
                    {
                        request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
                    }

                    // Set the Method property of the request to POST.
                    request.Method = "POST";
                    // Create POST data and convert it to a byte array.
                    string postData  = requestData;
                    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                    // Set the ContentType property of the WebRequest.
                    request.ContentType = "application/json";
                    // Set the ContentLength property of the WebRequest.
                    request.ContentLength = byteArray.Length;
                    // Get the request stream.
                    Stream dataStream = request.GetRequestStream();
                    // Write the data to the request stream.
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    // Close the Stream object.
                    dataStream.Close();

                    // Get the response.
                    WebResponse response = request.GetResponse();
                    // Display the status.
                    // Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                    // Get the stream containing content returned by the server.
                    dataStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    StreamReader reader = new StreamReader(dataStream);
                    // Read the content.
                    string responseFromServer = reader.ReadToEnd();
                    // Display the content.
                    // Console.WriteLine(responseFromServer);

                    // responseVal responseData ;

                    responseData = JsonConvert.DeserializeObject <responseVal>(responseFromServer);
                    reader.Close();
                    dataStream.Close();
                    response.Close();
                    return(responseData);


                    //int compare = responseStruct.status.CompareTo("success");
                    //if (compare == 0)
                    //{
                    //    reader.Close();
                    //    dataStream.Close();
                    //    response.Close();
                    //    return "success";
                    //}

                    //else
                    //{
                    //    // Clean up the streams.
                    //    reader.Close();
                    //    dataStream.Close();
                    //    response.Close();
                    //    return "failed";
                    //}
                }
                catch (WebException webExcp)
                {
                    var    now = DateTime.Now;
                    string val = string.Format("{0} - Failed to contact server: {1} \r\n", now, reqMeta);
                    CreateFolder.saveFile(val, true);
                    responseData.status = "invalid";

                    WebExceptionStatus status = webExcp.Status;
                    if (status == WebExceptionStatus.ProtocolError)
                    {
                        return(responseData);
                    }
                    return(responseData);
                }
                catch (Exception e)
                {
                    responseData.status = e.ToString();
                    return(responseData);
                }
            }
Ejemplo n.º 2
0
        private void SubmitTfa_Click(object sender, EventArgs e)
        {
            trasaCPConfig configFile; //= JsonConvert.DeserializeObject<trasaCPConfig>(File.ReadAllText(@"C:\Program Files\TrasaWin\config.trasa"));

            string[] hosts = new string[] { };
            string[] users = new string[] { };
            try
            {
                configFile           = JsonConvert.DeserializeObject <trasaCPConfig>(File.ReadAllText(@"C:\Program Files\TrasaWin\config.trasa"));
                reqVals.user         = this.userValFromCP;
                reqVals.serviceID    = configFile.serviceID;
                reqVals.serviceKey   = configFile.serviceKey;
                configFile.trasaHost = configFile.trasaHost.Replace(" ", String.Empty);
                hosts = configFile.trasaHost.Split(',');
                configFile.offlineUsers = configFile.offlineUsers.Replace(" ", String.Empty);
                users = configFile.offlineUsers.Split(',');
            }
            catch (Exception je)
            {
                this.ReturnValue1 = "success";
                var    now = DateTime.Now;
                string val = string.Format("{0} - Authentication was bypassed for user {1} because invalid config file found  - error: {2} \r\n", now, this.userValFromCP, je.ToString());
                CreateFolder.saveFile(val, true);
                Application.Exit();
                return;
            }



            if (String.IsNullOrEmpty(reqVals.trasaID))
            {
                MessageBox.Show("You need to submit your email or username");

                return;
            }

            statusText.Text = "Verifying request.....";
            this.Enabled    = false;

            if (String.IsNullOrEmpty(reqVals.tfaMethod))
            {
                reqVals.tfaMethod = "U2F";
            }
            if (String.IsNullOrEmpty(reqVals.totpCode))
            {
                reqVals.totpCode = "";
            }
            if (String.IsNullOrEmpty(reqVals.userIP))
            {
                reqVals.userIP = "";
            }

            if (String.IsNullOrEmpty(reqVals.workstation))
            {
                reqVals.workstation = "";
            }



            string requestVar = JsonConvert.SerializeObject(reqVals);
            string respCode   = "failed";

            responseVal resp;

            resp = checkResp.makeTfaRequest(hosts, requestVar, configFile.skipTLSVerification);

            // if the respCode is success we return success and exit the program.
            // if the respCode is failed, we return failed and exit the program.
            // if respCode is invalid, this means that trasaWin could not connect to any of trasacore hosts. This is offline case and we should check if the user is authorized in offline mode.
            //
            if (resp.status.Equals("success"))
            {
                this.ReturnValue1 = "success";
                Application.Exit();
            }
            else if (resp.status.Equals("failed"))
            {
                statusText.BackColor = System.Drawing.Color.Maroon;
                statusText.ForeColor = System.Drawing.Color.White;
                statusText.Text      = "Failed 2FA";
                this.Enabled         = true;
                MessageBox.Show(resp.reason);
                // Prompt.ShowDialog(resp.reason);
                // this.ReturnValue1 = "failed";
                //shut.LogOff();
                //Application.Exit();
            }
            else
            {
                // we now check for offline users.
                respCode = checkOfflineUsers.check(users, this.userValFromCP);

                if (respCode.Equals("success"))
                {
                    this.ReturnValue1 = "success";
                    var    now = DateTime.Now;
                    string val = string.Format("{0} - Allowing offline access to user {1} \r\n", now, this.userValFromCP);
                    Application.Exit();
                }
                else
                {
                    this.ReturnValue1 = "invalid-offline";
                    MessageBox.Show("TrasWIN cannot contact trasacore (trasa server) and currently administrator has not authorized this user for offline access.");
                    var    now = DateTime.Now;
                    string val = string.Format("{0} - Login blocked as trasa cannot contact trasacore and offline usage has not been authorized for user {1} \r\n", now, this.userValFromCP);
                    CreateFolder.saveFile(val, true);
                    // shut.LogOff();
                    Application.Exit();
                }
            }
        }