Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LegalIdentityUser.cs
1using Paiwise;
2using System;
4using System.Text;
5using System.Threading.Tasks;
6using System.Xml;
8using Waher.Events;
11using Waher.Security;
14using Waher.Things;
15
17{
22 {
23 private readonly LegalComponent legalComponent;
24 private readonly LegalIdentity identity;
25 private readonly QuickLoginUser user;
26 private readonly XmppAddress from;
27
37 {
38 this.identity = Identity;
39 this.from = From;
40 this.legalComponent = LegalComponent;
41 this.user = User;
42 }
43
51 public static async Task<LegalIdentityUser> Create(LegalIdentity Identity, XmppAddress From, LegalComponent LegalComponent)
52 {
53 string Xml = null;
54
55 try
56 {
57 Dictionary<string, string> AttachmentUrls = new Dictionary<string, string>();
58 StringBuilder sb = new StringBuilder();
59 Identity.Serialize(sb, true, true, true, true, true, true, true, AttachmentUrls, LegalComponent);
60 Xml = sb.ToString();
61
62 Networking.XMPP.Contracts.LegalIdentity Identity2 = Networking.XMPP.Contracts.LegalIdentity.Parse(Xml);
63
64 LegalIdentityInfo IdentityInfo = await QuickLogin.PrepareQuickLoginIdentity(Identity2, "Session", true, null);
65 QuickLoginUser User = new QuickLoginUser(Identity2, string.Empty, IdentityInfo.Properties, IdentityInfo.Attachments);
66
68 }
69 catch (XmlException ex)
70 {
71 ex = XML.AnnotateException(ex, Xml);
72 Log.Exception(ex);
74 }
75 catch (Exception ex)
76 {
77 Log.Exception(ex);
79 }
80 }
81
85 public ILegalIdentity LegalIdentity => this.identity;
86
90 public LegalIdentity Identity => this.identity;
91
95 public XmppAddress From => this.from;
96
100 public IUser User => (IUser)this.user ?? this;
101
105 public string UserName
106 {
107 get
108 {
109 string s = this.user?.UserName;
110 if (!string.IsNullOrEmpty(s))
111 return s;
112
113 s = this.identity[PersonalInformation.JidTag];
114 if (string.IsNullOrEmpty(s))
115 s = this.identity.Id;
116
117 int i = s.IndexOf('@');
118 if (i > 0 && Gateway.IsDomain(s[(i + 1)..], true))
119 return s[..i];
120
121 return s;
122 }
123 }
124
128 public string FederatedUserName
129 {
130 get
131 {
132 string s = this.user?.FederatedUserName;
133 if (!string.IsNullOrEmpty(s))
134 return s;
135
136 s = this.identity[PersonalInformation.JidTag];
137 return string.IsNullOrEmpty(s) ? this.identity.Id.Value : s;
138 }
139 }
140
144 public string FriendlyName
145 {
146 get
147 {
148 string s = this.user?.FriendlyName;
149 if (!string.IsNullOrEmpty(s))
150 return s;
151
152 s = this.identity[PersonalInformation.FullNameTag];
153 if (!string.IsNullOrEmpty(s))
154 return s;
155
156 return this.UserName;
157 }
158 }
159
163 public string PasswordHash => throw new InvalidOperationException();
164
168 public string PasswordHashType => throw new InvalidOperationException();
169
175 public bool HasPrivilege(string Privilege)
176 {
177 if (Privilege.StartsWith("iotid:"))
178 return this.legalComponent.IsAccessToIdentityAuthorized(this.from.BareJid, Privilege[6..]);
179 else if (Privilege.StartsWith("iotsc:"))
180 return this.legalComponent.IsAccessToContractAuthorized(this.from.BareJid, Privilege[6..]);
181 else
182 return false;
183 }
184
189 public Task<RequestOrigin> GetOrigin()
190 {
191 return Task.FromResult(new RequestOrigin(this.from.BareJid, null, null, null, this));
192 }
193
199 public Task<IEnumerable<KeyValuePair<string, object>>> CreateClaims(bool Encrypted)
200 {
201 if (this.user is null)
202 return Task.FromResult<IEnumerable<KeyValuePair<string, object>>>(null);
203 else
204 return this.user.CreateClaims(Encrypted);
205 }
206
214 public Task<string> CreateToken(JwtFactory Factory, bool Encrypted,
215 params KeyValuePair<string, object>[] AdditionalClaims)
216 {
217 if (this.user is null)
218 return Task.FromResult<string>(null);
219 else
220 return this.user.CreateToken(Factory, Encrypted, AdditionalClaims);
221 }
222
223 }
224}
Contains personal information found in a legal identity.
const string FullNameTag
FULLNAME
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
Definition: XML.cs:1762
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static bool IsDomain(string DomainOrHost, bool IncludeAlternativeDomains)
If a domain or host name represents the gateway.
Definition: Gateway.cs:5174
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
A factory that can create and validate JWT tokens.
Definition: JwtFactory.cs:66
Tokens available in request.
Definition: RequestOrigin.cs:9
Basic interface for a user.
Definition: IUser.cs:7
A User that can participate in distributed operations, where the user is identified using a JWT token...
Interface for requestors that can act as an origin for distributed requests.