Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NodeQuestion.cs
1using System;
2using System.Collections.Generic;
3using System.Security.Cryptography.X509Certificates;
4using System.Threading.Tasks;
5using System.Windows;
6using System.Windows.Controls;
9using Waher.Things;
10
12{
13 public abstract class NodeQuestion : Question
14 {
15 private readonly Dictionary<string, X509Certificate2> certificates = new Dictionary<string, X509Certificate2>();
16 private string[] serviceTokens = null;
17 private string[] deviceTokens = null;
18 private string[] userTokens = null;
19 private string nodeId = string.Empty;
20 private string sourceId = string.Empty;
21 private string partition = string.Empty;
22
23 public NodeQuestion()
24 : base()
25 {
26 }
27
28 [DefaultValueNull]
29 public string[] ServiceTokens
30 {
31 get => this.serviceTokens;
32 set => this.serviceTokens = value;
33 }
34
35 [DefaultValueNull]
36 public string[] DeviceTokens
37 {
38 get => this.deviceTokens;
39 set => this.deviceTokens = value;
40 }
41
42 [DefaultValueNull]
43 public string[] UserTokens
44 {
45 get => this.userTokens;
46 set => this.userTokens = value;
47 }
48
49 [DefaultValueStringEmpty]
50 public string NodeId
51 {
52 get => this.nodeId;
53 set => this.nodeId = value;
54 }
55
56 [DefaultValueStringEmpty]
57 public string SourceId
58 {
59 get => this.sourceId;
60 set => this.sourceId = value;
61 }
62
63 [DefaultValueStringEmpty]
64 public string Partition
65 {
66 get => this.partition;
67 set => this.partition = value;
68 }
69
70 [IgnoreMember]
71 public bool IsNode
72 {
73 get
74 {
75 return !string.IsNullOrEmpty(this.nodeId) || !string.IsNullOrEmpty(this.sourceId) || !string.IsNullOrEmpty(this.partition);
76 }
77 }
78
79 public ThingReference GetNodeReference()
80 {
81 return new ThingReference(this.nodeId, this.sourceId, this.partition);
82 }
83
84 protected void AddNodeInfo(StackPanel Details)
85 {
86 if (this.IsNode)
87 {
88 if (!string.IsNullOrEmpty(this.NodeId))
89 this.AddKeyValue(Details, "Node ID", this.NodeId);
90
91 if (!string.IsNullOrEmpty(this.SourceId))
92 this.AddKeyValue(Details, "Source ID", this.SourceId);
93
94 if (!string.IsNullOrEmpty(this.Partition))
95 this.AddKeyValue(Details, "Partition", this.Partition);
96 }
97 }
98
99 protected void AddTokens(StackPanel Details, ProvisioningClient Client, RoutedEventHandler OnYes, RoutedEventHandler OnNo)
100 {
101 this.AddTokens(Details, Client, this.ServiceTokens, OnYes, OnNo, OperationRange.ServiceToken);
102 this.AddTokens(Details, Client, this.UserTokens, OnYes, OnNo, OperationRange.UserToken);
103 this.AddTokens(Details, Client, this.DeviceTokens, OnYes, OnNo, OperationRange.DeviceToken);
104 }
105
106 private void AddTokens(StackPanel Details, ProvisioningClient Client, string[] Tokens, RoutedEventHandler OnYes, RoutedEventHandler OnNo, OperationRange Range)
107 {
108 if (!(Tokens is null))
109 {
110 X509Certificate2 Certificate;
111
112 foreach (string Token in Tokens)
113 {
114 lock (this.certificates)
115 {
116 if (!this.certificates.TryGetValue(Token, out Certificate))
117 Certificate = null;
118 }
119
120 if (!(Certificate is null))
121 this.AddToken(Details, Token, Certificate, OnYes, OnNo, Range);
122 else
123 {
124 Client.GetCertificate(Token, (Sender, e) =>
125 {
126 if (e.Ok)
127 {
128 string Token2 = (string)e.State;
129
130 lock (this.certificates)
131 {
132 this.certificates[Token2] = e.Certificate;
133 }
134
135 MainWindow.UpdateGui(() =>
136 {
137 this.AddToken(Details, Token2, e.Certificate, OnYes, OnNo, Range);
138 return Task.CompletedTask;
139 });
140 }
141
142 return Task.CompletedTask;
143
144 }, Token);
145 }
146 }
147 }
148 }
149
150 private void AddToken(StackPanel Details, string Token, X509Certificate2 Certificate, RoutedEventHandler OnYes, RoutedEventHandler OnNo, OperationRange Range)
151 {
152 Button Button;
153
154 if (!Certificate.Verify())
155 return;
156
157 Details.Children.Add(Button = new Button()
158 {
159 Margin = new Thickness(0, 6, 0, 6),
160 Content = "Yes, for " + Certificate.FriendlyName,
161 Tag = new object[] { Token, Range }
162 });
163
164 Button.Click += OnYes;
165
166 Details.Children.Add(Button = new Button()
167 {
168 Margin = new Thickness(0, 6, 0, 6),
169 Content = "No, for " + Certificate.FriendlyName,
170 Tag = new object[] { Token, Range }
171 });
172
173 Button.Click += OnNo;
174 }
175 }
176}
Interaction logic for xaml
Implements an XMPP provisioning client interface.
Task GetCertificate(string Token, EventHandlerAsync< CertificateEventArgs > Callback, object State)
Gets the certificate the corresponds to a token. This certificate can be used to identify services,...
Contains a reference to a thing