private CompanionAppService() { Credentials = CompanionAppCredentials.FromFile(); // Need to work out our current state. //If we're missing username and password then we need to log in again if (string.IsNullOrEmpty(Credentials.email) || string.IsNullOrEmpty(Credentials.password)) { CurrentState = State.NEEDS_LOGIN; } else if (string.IsNullOrEmpty(Credentials.machineId) || string.IsNullOrEmpty(Credentials.machineToken)) { CurrentState = State.NEEDS_LOGIN; } else { // Looks like we're ready but test it to find out CurrentState = State.READY; try { Profile(); } catch (EliteDangerousCompanionAppException ex) { Logging.Warn("Failed to obtain profile: " + ex.ToString()); } } }
private static void AddMachineTokenCookie(CookieContainer cookies, CompanionAppCredentials credentials) { if (cookies != null && credentials.machineToken != null) { var machineTokenCookie = new Cookie(); machineTokenCookie.Domain = "companion.orerve.net"; machineTokenCookie.Path = "/"; machineTokenCookie.Name = "mtk"; machineTokenCookie.Value = credentials.machineToken; machineTokenCookie.Secure = true; // The expiry is embedded in the cookie value if (credentials.machineToken.IndexOf("%7C") == -1) { machineTokenCookie.Expires = DateTime.Now.AddDays(7); } else { string expiryseconds = credentials.machineToken.Substring(0, credentials.machineToken.IndexOf("%7C")); DateTime expiryDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); try { expiryDateTime = expiryDateTime.AddSeconds(Convert.ToInt64(expiryseconds)); machineTokenCookie.Expires = expiryDateTime; } catch (Exception) { Logging.Warn("Failed to handle machine token expiry seconds " + expiryseconds); machineTokenCookie.Expires = DateTime.Now.AddDays(7); } } cookies.Add(machineTokenCookie); } }
/// <summary> /// Obtain credentials from a file. If the file name is not supplied the the default /// path of Constants.Data_DIR\credentials.json is used /// </summary> public static CompanionAppCredentials FromFile(string filename = null) { if (filename == null) { filename = Constants.DATA_DIR + @"\credentials.json"; } CompanionAppCredentials credentials = null; string data = Files.Read(filename); if (data != null) { try { credentials = JsonConvert.DeserializeObject <CompanionAppCredentials>(data); } catch (Exception ex) { Logging.Debug("Failed to read companion app credentials", ex); } } if (credentials == null) { credentials = new CompanionAppCredentials(); } credentials.dataPath = filename; return(credentials); }
/// <summary> /// Obtain credentials from a file. If filepath is not supplied then defaultPath is used /// </summary> public static CompanionAppCredentials Load(string filepath = null) { CompanionAppCredentials credentials = null; filepath = filepath ?? defaultPath; string data = Files.Read(filepath); if (data != null) { try { credentials = JsonConvert.DeserializeObject <CompanionAppCredentials>(data); } catch (Exception ex) { Logging.Debug("Failed to read companion app credentials", ex); } } if (credentials == null) { credentials = new CompanionAppCredentials() { dataPath = filepath }; credentials.Save(); } credentials.dataPath = filepath; return(credentials); }
/// <summary> /// Log out of the companion API and remove local credentials /// </summary> public void Logout() { // Remove everything other than the local email address Credentials = CompanionAppCredentials.FromFile(); Credentials.machineToken = null; Credentials.machineId = null; Credentials.appId = null; Credentials.password = null; Credentials.ToFile(); CurrentState = State.NEEDS_LOGIN; }
///<summary>Confirm a login. Throws an exception if it fails</summary> public void Confirm() { if (CurrentState != State.NEEDS_CONFIRMATION) { // Shouldn't be here throw new EliteDangerousCompanionAppIllegalStateException("Service in incorrect state to confirm login (" + CurrentState + ")"); } string code = CompanionAppCredentials.Instance().code; HttpWebRequest request = GetRequest(BASE_URL + CONFIRM_URL); request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; string encodedCode = WebUtility.UrlEncode(code); byte[] data = Encoding.UTF8.GetBytes("code=" + encodedCode); request.ContentLength = data.Length; using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(data, 0, data.Length); } using (HttpWebResponse response = GetResponse(request)) { if (response == null) { throw new EliteDangerousCompanionAppException("Failed to contact API server"); } foreach (string key in response.Headers.AllKeys) { logger.log(key + " ==> " + response.Headers[key]); } //logger.log(response.StatusCode.ToString()); if (response.StatusCode == HttpStatusCode.Found && response.Headers["Location"] == ROOT_URL) { CurrentState = State.READY; } else if (response.StatusCode == HttpStatusCode.Found && response.Headers["Location"] == LOGIN_URL) { CurrentState = State.NEEDS_LOGIN; throw new EliteDangerousCompanionAppAuthenticationException("Confirmation code incorrect or expired"); } else if (response.StatusCode == HttpStatusCode.OK) { //CurrentState = State.NEEDS_LOGIN; throw new EliteDangerousCompanionAppAuthenticationException("Confirmation code incorrect"); } } }
private static void AddCompanionAppCookie(CookieContainer cookies, CompanionAppCredentials credentials) { if (cookies != null && credentials.appId != null) { var appCookie = new Cookie(); appCookie.Domain = "companion.orerve.net"; appCookie.Path = "/"; appCookie.Name = "CompanionApp"; appCookie.Value = credentials.appId; appCookie.Secure = false; cookies.Add(appCookie); } }
/// <summary> /// Obtain credentials from a file. If the file name is not supplied the the default /// path of Constants.Data_DIR\credentials.json is used /// </summary> public static CompanionAppCredentials FromFile(string filename=null) { if (filename == null) { filename = Constants.DATA_DIR + @"\credentials.json"; } CompanionAppCredentials credentials = new CompanionAppCredentials(); try { string credentialsData = File.ReadAllText(filename); credentials = JsonConvert.DeserializeObject<CompanionAppCredentials>(credentialsData); } catch {} credentials.dataPath = filename; return credentials; }
/// <summary> /// Obtain credentials from a file. If the file name is not supplied the the default /// path of Constants.Data_DIR\credentials.json is used /// </summary> public static CompanionAppCredentials FromFile(string filename = null) { if (filename == null) { filename = Constants.DATA_DIR + @"\credentials.json"; } CompanionAppCredentials credentials = new CompanionAppCredentials(); try { string credentialsData = File.ReadAllText(filename); credentials = JsonConvert.DeserializeObject <CompanionAppCredentials>(credentialsData); } catch {} credentials.dataPath = filename; return(credentials); }
private CompanionAppService() { Credentials = CompanionAppCredentials.Load(); // Need to work out our current state. //If we're missing username and password then we need to log in again /* * if (string.IsNullOrEmpty(Credentials.email) || string.IsNullOrEmpty(Credentials.password)) * { * CurrentState = State.NEEDS_LOGIN; * } * else */ if (string.IsNullOrEmpty(Credentials.machineId) || string.IsNullOrEmpty(Credentials.machineToken)) { CurrentState = State.NEEDS_LOGIN; } else if (string.IsNullOrEmpty(Credentials.appId)) { CurrentState = State.NEEDS_CONFIRMATION; } else { // Looks like we're ready but test it to find out CurrentState = State.READY; /* * try * { * Profile(); * } * catch (EliteDangerousCompanionAppException ex) * { * logger.log("Failed to obtain profile: " + ex.ToString()); * } */ } }