5using System.Threading.Tasks;
19 private readonly
string sessionId;
20 private readonly
bool usesChallengeUri;
22 private string serviceId;
23 private string[] domainParts;
25 private string legalId;
26 private string domain;
27 private string subject;
28 private bool hasLegalId;
30 private EventHandlerAsync<RemoteAuthenticationEventArgs> callback;
38 public QuickLoginRemoteAuthentication(
string Jid,
string LegalId)
41 this.legalId = LegalId;
43 this.hasLegalId = !
string.IsNullOrEmpty(this.legalId);
44 this.hasJid = !
string.IsNullOrEmpty(this.jid);
45 this.usesChallengeUri = !(this.hasLegalId || this.hasJid);
49 this.sessionId = this.legalId;
50 this.serviceId =
null;
54 this.sessionId = this.jid;
55 this.serviceId =
null;
59 this.sessionId = Guid.NewGuid().
ToString();
60 this.serviceId = QuickLogin.GetServiceID(this.sessionId);
67 public string Key => this.sessionId;
73 public Task<bool> IsValidAddress()
75 if (this.usesChallengeUri)
76 return Task.FromResult(
true);
78 if (!(this.hasLegalId || this.hasJid))
79 return Task.FromResult(
false);
81 this.address =
new XmppAddress(this.hasLegalId ? this.legalId : this.jid);
83 return Task.FromResult(
false);
85 this.domain = this.address.
Domain;
87 int i = this.domain.IndexOf(
'.');
88 if (i < 0 || i >= this.domain.Length - 1)
89 return Task.FromResult(
false);
93 if (!Guid.TryParse(
this.address.Account, out
_))
94 return Task.FromResult(
false);
96 this.domain = this.domain[(i + 1)..];
97 this.subject = this.legalId;
100 this.subject = this.jid;
102 this.domainParts = this.domain.Split(
'.');
104 return Task.FromResult(
true);
111 public async Task<bool> IsLegalIdAvailable()
113 if (this.usesChallengeUri)
119 if (this.address is
null)
122 string[] Identities = await XmppServerModule.Legal.GetIdentityReferences(this.address);
123 if (Identities.Length == 0)
126 this.legalId = Identities[0];
127 this.hasLegalId =
true;
137 public Task<bool> IsPermitted(
IUser User)
139 if (this.usesChallengeUri)
140 return Task.FromResult(
true);
142 int i, c = this.domainParts?.Length ?? 0;
144 StringBuilder sb =
new StringBuilder();
145 sb.Append(nameof(RemoteLogin));
146 sb.Append(
".Domain");
148 for (i = c - 1; i >= 0; i--)
151 sb.Append(this.domainParts[i]);
154 string Privilege = sb.ToString();
170 public Task<Uri> GetChallengeUri(
string Purpose,
string RemoteEndPoint)
172 if (!this.usesChallengeUri)
173 return Task.FromResult<Uri>(
null);
175 if (
string.IsNullOrEmpty(this.serviceId))
176 this.serviceId = QuickLogin.GetServiceID(
this.sessionId);
178 string TagSignUrl = QuickLogin.GetTagSignUri(
string.Empty,
179 this.serviceId, Purpose, RemoteEndPoint,
null,
null,
180 this.QuickLoginCallback,
null);
182 if (
string.IsNullOrEmpty(TagSignUrl))
187 if (Uri.TryCreate(Url, UriKind.Absolute, out Uri Result))
188 return Task.FromResult(Result);
190 return Task.FromResult<Uri>(
null);
203 public async Task<string> Start(
string Purpose,
string RemoteEndPoint,
204 EventHandlerAsync<RemoteAuthenticationEventArgs> Callback,
object State)
206 if (this.usesChallengeUri)
208 this.callback = Callback;
211 return this.sessionId;
214 string PetitionId = await QuickLogin.StartQuickLogin(this.legalId, Purpose,
215 null,
null, RemoteEndPoint, async (sender, e) =>
221 if (e.Response && !(e.RequestedIdentity is null))
223 e2 = new RemoteAuthenticationEventArgs(e.State,
224 new KeyValuePair<string, object>(JwtClaims.ClientId, e.RequestedIdentity.Id),
225 new KeyValuePair<string, object>(JwtClaims.Subject, this.subject));
229 string Jid = e.RequestedIdentity[PersonalInformation.JidTag];
231 if (!string.IsNullOrEmpty(Jid))
244 !(e.StanzaError is Networking.XMPP.StanzaErrors.RecipientUnavailableException));
247 await Callback.Raise(
this, e2);
254 private async Task QuickLoginCallback(
object Sender, QuickLoginResponseEventArgs e)
256 EventHandlerAsync<RemoteAuthenticationEventArgs> Callback = this.callback;
258 object State = this.state;
260 if (Callback is
null)
263 this.callback =
null;
268 if (e.Response && !(e.RequestedIdentity is
null))
270 this.subject = e.RequestedIdentity.Id;
280 if (!
string.IsNullOrEmpty(Jid))
293 !(e.StanzaError is Networking.XMPP.StanzaErrors.RecipientUnavailableException));
296 await Callback.Raise(
this, e2);
Event arguments for Remote Authentication callback methods.
Static class managing the runtime environment of the IoT Gateway.
static string GetUrl(string LocalResource)
Gets a URL for a resource.
Contains information about one XMPP address.
override string ToString()
object.ToString()
bool IsBareJID
If the address is a Bare JID.
CaseInsensitiveString Domain
Domain
Static class containing predefined JWT claim names.
const string Subject
Subject of the JWT (the user)
const string ClientId
Client identifier
Interface for remote authentications.
bool HasPrivilege(string Privilege)
If the object has a given privilege.
Basic interface for a user.