Advertisement
Guest User

login attempt

a guest
Nov 26th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.14 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class JSONInterpreter : MonoBehaviour
  6. {
  7.     string loginURL = "https://tarantula.domain.com/home/login";
  8.     string executionsURL = "https://tarantula.domain.com/executions";
  9.     public WWWForm form;
  10.     public Hashtable headers;
  11.     public byte[] rawData;
  12.     public WWW www;
  13.     public string cookie;
  14.     string token;
  15.     bool waiting = false;
  16.     bool connected = false;
  17.     bool loggedIn = false;
  18.     bool hasToken = false;
  19.     bool hasCookie = false;
  20.     bool gotExecutions = false;
  21.     string login = "";
  22.     string password = "";
  23.    
  24.     void Start()
  25.     {
  26.         form = new WWWForm();
  27.         headers = form.headers;
  28.         headers["Connection"] = "Keep-Alive";
  29.         headers["Host"] = "tarantula.domain.com";
  30.         rawData = form.data;
  31.     }
  32.    
  33.     bool getToken()
  34.     {
  35.         string htmlString = www.text;
  36.         //Debug.Log("Before cut: " + htmlString);
  37.         int cut = htmlString.IndexOf("authenticity_token\" type=\"hidden\" value=\"");
  38.         if(cut >=0)
  39.         {
  40.             htmlString = htmlString.Substring(cut+41);
  41.             //Debug.Log("First cut: " + htmlString);
  42.             cut = htmlString.IndexOf('"');
  43.             if(cut >=0)
  44.             {
  45.                 htmlString = htmlString.Substring(0, cut);
  46.                 token = htmlString;
  47.                 Debug.Log("Authorisation: " + htmlString);
  48.                 return true;
  49.             }
  50.             else
  51.             {
  52.                 Debug.Log("Token not found 2");
  53.                 return false;
  54.             }  
  55.         }
  56.         else
  57.         {
  58.             Debug.Log("Token not found 1");
  59.             return false;
  60.         }      
  61.     }
  62.    
  63.     public int isConnected()
  64.     {
  65.         if(www.isDone)
  66.         {
  67.             if(!connected)
  68.             {
  69.                 waiting = false;
  70.                 hasToken = getToken();
  71.                 if(www.responseHeaders.ContainsKey("Set-Cookie"))
  72.                 {
  73.                     cookie = www.responseHeaders["Set-Cookie"];
  74.                     Debug.Log("recieved cookie: " + cookie);
  75.                     //cookie = cookie.Substring(0, cookie.IndexOf(';'));
  76.                     //cookie = cookie.Replace("%3D", "=");
  77.                     Debug.Log("Cookie: " + cookie);
  78.                     hasCookie = true;
  79.                 }
  80.                 else
  81.                 {
  82.                     foreach(var header in www.responseHeaders)
  83.                     {
  84.                         if(header.Value.IndexOf("_tarantula_session") >= 0)
  85.                         {
  86.                             cookie = header.Value;
  87.                             Debug.Log("recieved cookie: " + cookie);
  88.                             //cookie = cookie.Substring(0, cookie.IndexOf(';'));
  89.                             //cookie = cookie.Replace("%3D", "=");
  90.                             Debug.Log("Cookie: " + cookie);
  91.                             hasCookie = true;
  92.                         }
  93.                     }
  94.                 }
  95.                 Debug.Log("Cookie: " + cookie);
  96.                 if(hasToken && hasCookie)
  97.                 {
  98.                     waiting = true;
  99.                     connected = true;
  100.                     LogIn(login, password);
  101.                 }
  102.             }
  103.             else
  104.             {
  105.                 if(www.responseHeaders.ContainsKey("STATUS"))
  106.                 {
  107.                     if(www.responseHeaders["STATUS"] == "HTTP/1.1 302 Found")
  108.                     {
  109.                         waiting = false;
  110.                         loggedIn = true;
  111.                         Debug.Log("YAY");
  112.                     }
  113.                     else
  114.                     {
  115.                         Debug.Log(www.responseHeaders["STATUS"]);
  116.                     }
  117.                 }
  118.                 else
  119.                 {
  120.                     Debug.Log("Response header not found");
  121.                 }
  122.             }
  123.         }
  124.         if(waiting)
  125.         {
  126.             return 0;
  127.         }
  128.         if(loggedIn)
  129.         {
  130.             return 1;
  131.         }
  132.         return -1;
  133.     }
  134.    
  135.     public void Connect(string theLogin, string thePassword)
  136.     {
  137.         login = theLogin;
  138.         password = thePassword;
  139.         www = new WWW(loginURL);
  140.         waiting = true;
  141.         Debug.Log("Connecting");
  142.         LoadData();
  143.     }
  144.    
  145.     public void LogIn(string theLogin, string thePassword)
  146.     {
  147.         login = theLogin;
  148.         password = thePassword;
  149.         string postString = "utf8=%E2%9C%93&authenticity_token=" + token + "&login=" + login + "&password=" + password;
  150.         byte[] postData = System.Text.Encoding.UTF8.GetBytes(postString);
  151.         headers["Cookie"] = cookie;
  152.         www = new WWW(loginURL, postData, headers);
  153.         waiting = true;
  154.         loggedIn = false;
  155.         Debug.Log("Attempting login");
  156.         LoadData() ;
  157.        
  158.     }
  159.    
  160.     IEnumerator LoadData()
  161.     {
  162.         yield return www;
  163.     }
  164.    
  165.     public GUIContent[] GetExecutions()
  166.     {
  167.         if(www.isDone)
  168.         {
  169.             if(!gotExecutions)
  170.             {
  171.                 Debug.Log("Getting executions with cookie : " + cookie);
  172.                 headers["Cookie"] = cookie;
  173.                 www = new WWW(executionsURL, null, headers);
  174.                 gotExecutions = true;
  175.                 LoadData();
  176.             }
  177.             else
  178.             {
  179.                 Debug.Log(www.text);
  180.                 foreach(DictionaryEntry header in headers)
  181.                 {
  182.                     //Debug.Log("header : " + header.Key + " -: " + header.Value);
  183.                 }
  184.             }
  185.         }
  186.         return new GUIContent[0];
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement