public async Task <Dictionary <string, string> > GetTokensAsync(string userName) { TcpClient client = null; ServiceCommunication.SendNetworkMessage(ref client, JsonConvert.SerializeObject(new NetworkMessage(MessageType.GetState) { Username = userName })); var response = await Task <string> .Factory.StartNew(() => { return(ServiceCommunication.ReadNetworkMessage(ref client)); }); if (string.IsNullOrEmpty(response)) { return(new Dictionary <string, string>()); } UserServerState userServerState = JsonConvert.DeserializeObject <UserServerState>(response); _logger.Trace($"GetTokensAsync: {JsonConvert.SerializeObject(userServerState.UserConfiguration.Tokens)}"); return(userServerState.UserConfiguration.Tokens ?? new Dictionary <string, string>()); }
public void ReadNetwork(object tc) { TcpClient client = tc as TcpClient; while ((state == ServiceState.Running || state == ServiceState.Starting) && client != null && client.Connected) { // do a read try { string message = ServiceCommunication.ReadNetworkMessage(ref client); // do something with data the provider sent us if (message == "") { goto EndConnection; } else { NetworkMessage nm; Log("Message received from network: " + message); try { nm = JsonConvert.DeserializeObject <NetworkMessage>(message); } catch (Exception ex) { Log(message); continue; } // the first message should probably be the type of message we're receiving switch (nm.Type) { case MessageType.CancelRegistration: { SystemStatus.AwaitingToken = false; break; } case MessageType.GetToken: { // we don't need to do anything here except store this connection and wait for a ring swipe // SystemStatus.CredentialData.Client SystemStatus.RegistrationClient = client; SystemStatus.AwaitingToken = true; break; } case MessageType.RegisterToken: { // save this token against this username to be selected from the list later RegisterToken(nm.Username, nm.Token, nm.TokenFriendlyName); break; } case MessageType.GetState: { // return the current configuration for this user bool userfound = false; UserServerState uss = new UserServerState(); if (ApplicationConfiguration.Users != null) { foreach (User u in ApplicationConfiguration.Users) { if (u.Username == nm.Username) { uss.UserConfiguration = u; // also need to send a list of plugins. userfound = true; break; } } } if (!userfound) { uss.UserConfiguration = new User(); } uss.Plugins = new List <PluginInfo>(); foreach (Lazy <INFCRingServicePlugin> p in plugins) { uss.Plugins.Add(new PluginInfo() { Name = p.Value.GetPluginName(), Parameters = p.Value.GetParameters() }); } ServiceCommunication.SendNetworkMessage(ref client, JsonConvert.SerializeObject(uss)); break; } case MessageType.Message: { Log(nm.Message); break; } case MessageType.AssociatePluginToToken: { // get a plugin name, token, and if this plugin requires a credential, do the provider swap and lock the pc RegisterCredential(nm.Username, nm.Password, nm.Token, nm.PluginName); break; } case MessageType.UserCredential: { //Log(message); Log("Credential received"); if (SystemStatus.RegistrationClient != null && SystemStatus.RegistrationClient.Connected) { TcpClient otc = SystemStatus.RegistrationClient; ServiceCommunication.SendNetworkMessage(ref otc, JsonConvert.SerializeObject(new NetworkMessage(MessageType.UserCredential) { Username = nm.Username, Password = Convert.ToBase64String(Encoding.UTF8.GetBytes(nm.Password)) })); SystemStatus.RegistrationClient = otc; } // so this is where the user registration credential provider sends an actual user credential break; } case MessageType.Delete: { Log("Deleting item"); if (String.IsNullOrEmpty(nm.Username)) { break; // no username? lets not modify the config } if (!String.IsNullOrEmpty(nm.Token) && !String.IsNullOrEmpty(nm.PluginName)) { // delete an event RemoveEvent(nm.Username, nm.Token, nm.PluginName); } else if (!String.IsNullOrEmpty(nm.Token)) { // delete a token entirely (this will also delete all its events) RemoveToken(nm.Username, nm.Token); } break; } case MessageType.RegisterAll: { Log("Registering new token against all plugins"); string dht = RegisterToken(nm.Username, nm.Token, nm.TokenFriendlyName); foreach (Lazy <INFCRingServicePlugin> p in plugins) { RegisterCredential(nm.Username, nm.Password, dht, p.Value.GetPluginName()); } break; } case MessageType.UpdateFriendlyName: { Log("Update token friendly name"); UpdateFriendlyName(nm); break; } default: // failed Log("Unknown network message received: " + message); break; } } } catch (Exception ex) { Log("TCP Client disconnected"); if (client.Connected) { client.Close(); } } } EndConnection: Log("TCP Client network loop ended"); if (client.Connected) { client.Close(); } client = null; if (SystemStatus.RegistrationClient == null || !SystemStatus.RegistrationClient.Connected) { SystemStatus.AwaitingToken = false; } if (SystemStatus.CredentialData.Client == null || !SystemStatus.CredentialData.Client.Connected) { SystemStatus.CredentialData.ProviderActive = false; } }
private void LoadConfig() { startAgain: ServiceCommunication.SendNetworkMessage(ref client, JsonConvert.SerializeObject(new NetworkMessage(MessageType.GetState) { Username = ClientCommon.GetCurrentUsername() })); var task = Task <string> .Factory.StartNew(() => { return(ServiceCommunication.ReadNetworkMessage(ref client)); }); if (task.Result == "") { if (MessageBox.Show("Unable to connect to service. Please make sure its running", "NFC Ring Service Registration App", MessageBoxButtons.RetryCancel) == DialogResult.Cancel) { Application.Exit(); } // RETRY ISNT GOING TO DO ANYTHING YET goto startAgain; } else { tvwConfig.Nodes.Clear(); uss = JsonConvert.DeserializeObject <UserServerState>(task.Result); TreeNode user = tvwConfig.Nodes.Add(uss.UserConfiguration.Username); TreeNode tokens = user.Nodes.Add("Tokens"); if (uss.UserConfiguration.Tokens != null) { foreach (KeyValuePair <string, string> tok in uss.UserConfiguration.Tokens) { TreeNode tokenNode = tokens.Nodes.Add(tok.Value + " - " + tok.Key); tokenNode.Tag = "Token"; } } TreeNode plugins = user.Nodes.Add("Plugins"); if (uss.Plugins != null) { foreach (PluginInfo p in uss.Plugins) { TreeNode aPlugin = plugins.Nodes.Add(p.Name); aPlugin.Tag = "Plugin"; } } TreeNode events = user.Nodes.Add("Events"); if (uss.UserConfiguration.Events != null) { foreach (Event ev in uss.UserConfiguration.Events) { TreeNode eventNode = events.Nodes.Add(ev.PluginName + " - " + ev.Token); eventNode.Tag = "Event"; foreach (KeyValuePair <string, object> param in ev.Parameters) { if (param.Value != null) { TreeNode e = eventNode.Nodes.Add(param.Key + " = " + param.Value.ToString()); //e.Tag = "Event"; } } } } if ((uss.UserConfiguration.Tokens != null && uss.UserConfiguration.Tokens.Count > 0) && (uss.Plugins != null && uss.Plugins.Count > 0)) { btnAddEvent.Enabled = true; } tvwConfig.ExpandAll(); } }