Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConnectToForm.xaml.cs
1using System;
2using System.Threading.Tasks;
3using System.Windows;
4using System.Windows.Controls;
8
10{
14 public partial class ConnectToForm : Window
15 {
16 private XmppClient client = null;
17 private string passwordHash = string.Empty;
18 private string passwordHashMethod = string.Empty;
19
20 public ConnectToForm()
21 {
23 this.ConnectionMethod_SelectionChanged(this, null);
24 }
25
29 public string PasswordHash
30 {
31 get => this.passwordHash;
32 set => this.passwordHash = value;
33 }
34
38 public string PasswordHashMethod
39 {
40 get => this.passwordHashMethod;
41 set => this.passwordHashMethod = value;
42 }
43
44 private void CancelButton_Click(object Sender, RoutedEventArgs e)
45 {
46 this.DialogResult = false;
47 }
48
49 private async void ConnectButton_Click(object Sender, RoutedEventArgs e)
50 {
51 try
52 {
53 XmppCredentials Credentials = new XmppCredentials()
54 {
55 Host = this.XmppServer.Text,
56 Account = this.AccountName.Text
57 };
58
59 switch ((TransportMethod)this.ConnectionMethod.SelectedIndex)
60 {
61 case TransportMethod.TraditionalSocket:
62 if (!int.TryParse(this.XmppPort.Text, out int Port) || Port <= 0 || Port > 65535)
63 {
64 MessageBox.Show(this, "Invalid port number. Valid port numbers are positive integers between 1 and 65535. The default port number is " +
65 XmppCredentials.DefaultPort.ToString() + ".", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
66 this.XmppPort.Focus();
67 return;
68 }
69
70 Credentials.Port = Port;
71 break;
72
73 case TransportMethod.WS:
74 Uri Uri;
75 try
76 {
77 Uri = new Uri(this.UrlEndpoint.Text);
78 }
79 catch (Exception)
80 {
81 MessageBox.Show(this, "Invalid URI.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
82 this.UrlEndpoint.Focus();
83 return;
84 }
85
86 string Scheme = Uri.Scheme.ToLower();
87
88 if (Scheme != "ws" && Scheme != "wss")
89 {
90 MessageBox.Show(this, "Resource must be an WS or WSS URI.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
91 this.UrlEndpoint.Focus();
92 return;
93 }
94
95 if (!Uri.IsAbsoluteUri)
96 {
97 MessageBox.Show(this, "URI must be an absolute URI.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
98 this.UrlEndpoint.Focus();
99 return;
100 }
101
102 Credentials.UriEndpoint = this.UrlEndpoint.Text;
103 break;
104
105 case TransportMethod.BOSH:
106 try
107 {
108 Uri = new Uri(this.UrlEndpoint.Text);
109 }
110 catch (Exception)
111 {
112 MessageBox.Show(this, "Invalid URI.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
113 this.UrlEndpoint.Focus();
114 return;
115 }
116
117 Scheme = Uri.Scheme.ToLower();
118
119 if (Scheme != "http" && Scheme != "https")
120 {
121 MessageBox.Show(this, "Resource must be an HTTP or HTTPS URI.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
122 this.UrlEndpoint.Focus();
123 return;
124 }
125
126 if (!Uri.IsAbsoluteUri)
127 {
128 MessageBox.Show(this, "URI must be an absolute URI.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
129 this.UrlEndpoint.Focus();
130 return;
131 }
132
133 Credentials.UriEndpoint = this.UrlEndpoint.Text;
134 break;
135 }
136
137 bool Create = this.CreateAccount.IsChecked.HasValue && this.CreateAccount.IsChecked.Value;
138 if (Create && this.Password.Password != this.RetypePassword.Password)
139 {
140 MessageBox.Show(this, "The two passwords must match.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
141 this.Password.Focus();
142 return;
143 }
144
145 await this.CloseClient();
146 this.ConnectButton.IsEnabled = false;
147 this.XmppServer.IsEnabled = false;
148 this.ConnectionMethod.IsEnabled = false;
149 this.XmppPort.IsEnabled = false;
150 this.UrlEndpoint.IsEnabled = false;
151 this.AccountName.IsEnabled = false;
152 this.Password.IsEnabled = false;
153 this.RetypePassword.IsEnabled = false;
154 this.TrustServerCertificate.IsEnabled = false;
155 this.CreateAccount.IsEnabled = false;
156
157 if (this.Password.Password == this.passwordHash && !string.IsNullOrEmpty(this.passwordHash))
158 {
159 Credentials.Password = this.passwordHash;
160 Credentials.PasswordType = this.passwordHashMethod;
161 }
162 else
163 Credentials.Password = this.Password.Password;
164
165 if (this.AllowInsecureAuthentication.IsChecked.HasValue && this.AllowInsecureAuthentication.IsChecked.Value)
166 {
167 Credentials.AllowPlain = true;
168 Credentials.AllowCramMD5 = true;
169 Credentials.AllowDigestMD5 = true;
170 }
171
172 if (this.TrustServerCertificate.IsChecked.HasValue && this.TrustServerCertificate.IsChecked.Value)
173 Credentials.TrustServer = true;
174
175 this.client = new XmppClient(Credentials, "en", typeof(App).Assembly)
176 {
177 AllowQuickLogin = true
178 };
179
180 if (Create)
181 {
182 this.client.AllowRegistration(this.ApiKey.Text, this.Secret.Password);
183 this.client.OnRegistrationForm += this.Client_OnRegistrationForm;
184 }
185
186 this.client.OnStateChanged += this.Client_OnStateChanged;
187 this.client.OnConnectionError += this.Client_OnConnectionError;
188 await this.client.Connect();
189 }
190 catch (Exception ex)
191 {
192 MainWindow.ErrorBox(ex.Message);
193 }
194 }
195
196 private async Task Client_OnRegistrationForm(object _, DataForm Form)
197 {
198 Field FormType = Form["FORM_TYPE"];
199 if (!(FormType is null) && FormType.ValueString == "urn:xmpp:captcha")
200 {
201 MainWindow.UpdateGui(async () =>
202 {
203 ParameterDialog Dialog = await ParameterDialog.CreateAsync(Form);
204 Dialog.ShowDialog();
205 });
206 }
207 else
208 await Form.Submit();
209 }
210
211 private Task Client_OnStateChanged(object Sender, XmppState NewState)
212 {
213 MainWindow.UpdateGui(this.XmppStateChanged, NewState);
214 return Task.CompletedTask;
215 }
216
217 private async Task XmppStateChanged(object P)
218 {
219 XmppState NewState = (XmppState)P;
220
221 switch (NewState)
222 {
223 case XmppState.Offline:
224 this.ConnectionState.Content = "Offline.";
225 break;
226
227 case XmppState.Connecting:
228 this.ConnectionState.Content = "Connecting.";
229 break;
230
231 case XmppState.StreamNegotiation:
232 this.ConnectionState.Content = "Stream negotiation.";
233 break;
234
235 case XmppState.StreamOpened:
236 this.ConnectionState.Content = "Stream opened.";
237 break;
238
239 case XmppState.StartingEncryption:
240 this.ConnectionState.Content = "Starting encryption.";
241 break;
242
243 case XmppState.Authenticating:
244 this.ConnectionState.Content = "Authenticating user.";
245 break;
246
247 case XmppState.Registering:
248 this.ConnectionState.Content = "Registering account.";
249 break;
250
251 case XmppState.Binding:
252 this.ConnectionState.Content = "Binding resource.";
253 break;
254
255 case XmppState.FetchingRoster:
256 this.ConnectionState.Content = "Fetching roster.";
257 break;
258
259 case XmppState.SettingPresence:
260 this.ConnectionState.Content = "Setting presence.";
261 break;
262
263 case XmppState.Connected:
264 if (this.StorePassword.IsChecked.HasValue && this.StorePassword.IsChecked.Value)
265 {
266 this.passwordHash = this.Password.Password;
267 this.passwordHashMethod = string.Empty;
268 }
269 else
270 {
271 this.passwordHash = this.client.PasswordHash;
272 this.passwordHashMethod = this.client.PasswordHashMethod;
273 }
274
275 this.ConnectionState.Content = "Connected.";
276 await this.CloseClient();
277 this.DialogResult = true;
278 break;
279
280 case XmppState.Error:
281 this.ConnectionState.Content = "Error.";
282 await this.CloseClient();
283 break;
284
285 }
286 }
287
288 private async Task CloseClient()
289 {
290 if (!(this.client is null))
291 {
292 XmppClient Client = this.client;
293 this.client = null;
294 await Client.OfflineAndDisposeAsync();
295 }
296
297 this.ConnectButton.IsEnabled = true;
298 this.XmppServer.IsEnabled = true;
299 this.ConnectionMethod.IsEnabled = true;
300 this.XmppPort.IsEnabled = true;
301 this.UrlEndpoint.IsEnabled = true;
302 this.AccountName.IsEnabled = true;
303 this.Password.IsEnabled = true;
304 this.TrustServerCertificate.IsEnabled = true;
305 this.CreateAccount.IsEnabled = true;
306
307 this.RetypePassword.IsEnabled = (this.CreateAccount.IsChecked.HasValue && this.CreateAccount.IsChecked.Value);
308 }
309
310 private Task Client_OnConnectionError(object _, Exception Exception)
311 {
312 MainWindow.UpdateGui(this.ShowError, Exception);
313 return Task.CompletedTask;
314 }
315
316 private Task ShowError(object P)
317 {
318 Exception ex = (Exception)P;
319 MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
320 return Task.CompletedTask;
321 }
322
323 public XmppClient Client => this.client;
324
325 private void CreateAccount_Click(object Sender, RoutedEventArgs e)
326 {
327 bool Create = this.CreateAccount.IsChecked.HasValue && this.CreateAccount.IsChecked.Value;
328 this.CreateParameters.Visibility = Create ? Visibility.Visible : Visibility.Collapsed;
329 this.RetypePassword.IsEnabled = Create;
330 }
331
332 private void ConnectionMethod_SelectionChanged(object Sender, SelectionChangedEventArgs e)
333 {
334 if (this.PortLabel is null)
335 return;
336
337 switch ((TransportMethod)this.ConnectionMethod.SelectedIndex)
338 {
339 case TransportMethod.TraditionalSocket:
340 this.PortLabel.Visibility = Visibility.Visible;
341 this.XmppPort.Visibility = Visibility.Visible;
342 this.UrlEndpointLabel.Visibility = Visibility.Collapsed;
343 this.UrlEndpoint.Visibility = Visibility.Collapsed;
344 break;
345
346 case TransportMethod.BOSH:
347 case TransportMethod.WS:
348 this.PortLabel.Visibility = Visibility.Collapsed;
349 this.XmppPort.Visibility = Visibility.Collapsed;
350 this.UrlEndpointLabel.Visibility = Visibility.Visible;
351 this.UrlEndpoint.Visibility = Visibility.Visible;
352 break;
353 }
354 }
355 }
356}
Interaction logic for App.xaml
Definition: App.xaml.cs:9
Interaction logic for ConnectToForm.xaml
string PasswordHash
Password hash of a successfully authenticated client.
string PasswordHashMethod
Password hash method of a successfully authenticated client.
void InitializeComponent()
InitializeComponent
Interaction logic for xaml
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
async Task Submit()
Submits the form.
Definition: DataForm.cs:798
Base class for form fields
Definition: Field.cs:16
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
Task OfflineAndDisposeAsync()
Sends an offline presence, and then disposes the object by calling DisposeAsync.
Definition: XmppClient.cs:1119
string PasswordHashMethod
Password hash method.
Definition: XmppClient.cs:3445
string PasswordHash
Hash value of password. Depends on method used to authenticate user.
Definition: XmppClient.cs:3436
Task Connect()
Connects the client.
Definition: XmppClient.cs:641
void AllowRegistration()
If registration of a new account is allowed. Requires a password. Having a password hash is not suffi...
Definition: XmppClient.cs:3533
Class containing credentials for an XMPP client connection.
const int DefaultPort
Default XMPP Server port.
FormType
Type of data form.
Definition: FormType.cs:7
XmppState
State of XMPP connection.
Definition: XmppState.cs:7