private static async Task <bool> IsLoggedOn(bool online) { string jwt = GetJwt(); if (online && jwt != null) { User user = await GetUserAsync(online); if (user != null && SoftwareCoUtil.IsValidEmail(user.email)) { SoftwareCoUtil.setItem("name", user.email); SoftwareCoUtil.setItem("jwt", user.plugin_jwt); lastJwt = user.plugin_jwt; return(true); } string api = "/users/plugin/state"; HttpResponseMessage response = await SoftwareHttpManager.SendRequestAsync(HttpMethod.Get, api, jwt); if (SoftwareHttpManager.IsOk(response)) { string responseBody = await response.Content.ReadAsStringAsync(); IDictionary <string, object> jsonObj = (IDictionary <string, object>)SimpleJson.DeserializeObject(responseBody); if (jsonObj != null) { jsonObj.TryGetValue("state", out object stateObj); string state = (stateObj == null) ? "NONE" : Convert.ToString(stateObj); jsonObj.TryGetValue("jwt", out object pluginJwtObj); string pluginJwt = (pluginJwtObj == null) ? null : Convert.ToString(pluginJwtObj); if (state.Equals("OK") && pluginJwt != null) { jsonObj.TryGetValue("email", out object nameObj); string name = (nameObj == null) ? null : Convert.ToString(nameObj); if (name != null) { SoftwareCoUtil.setItem("name", name); } SoftwareCoUtil.setItem("jwt", pluginJwt); lastJwt = pluginJwt; } else if (state.Equals("NOT_FOUND")) { SoftwareCoUtil.setItem("jwt", null); lastJwt = null; } } } } SoftwareCoUtil.setItem("name", null); return(false); }
public static async Task <string> CreateAnonymousUserAsync(bool online) { // get the app jwt string app_jwt = await GetAppJwtAsync(online); if (app_jwt != null && online) { string creation_annotation = "NO_SESSION_FILE"; string osUsername = Environment.UserName; string timezone = ""; if (TimeZone.CurrentTimeZone.DaylightName != null && TimeZone.CurrentTimeZone.DaylightName != TimeZone.CurrentTimeZone.StandardName) { timezone = TimeZone.CurrentTimeZone.DaylightName; } else { timezone = TimeZone.CurrentTimeZone.StandardName; } JsonObject jsonObj = new JsonObject(); jsonObj.Add("timezone", timezone); jsonObj.Add("username", osUsername); jsonObj.Add("hostname", SoftwareCoUtil.getHostname()); jsonObj.Add("creation_annotation", creation_annotation); string api = "/data/onboard"; string jsonData = jsonObj.ToString(); HttpResponseMessage response = await SoftwareHttpManager.SendRequestAsync(HttpMethod.Post, api, jsonData, app_jwt); if (SoftwareHttpManager.IsOk(response)) { string responseBody = await response.Content.ReadAsStringAsync(); IDictionary <string, object> respObj = (IDictionary <string, object>)SimpleJson.DeserializeObject(responseBody); respObj.TryGetValue("jwt", out object jwtObj); string jwt = (jwtObj == null) ? null : Convert.ToString(jwtObj); if (jwt != null) { SoftwareCoUtil.setItem("jwt", jwt); return(jwt); } } } return(null); }