2using System.Collections.Generic;
3using System.Collections.Specialized;
4using System.Runtime.ExceptionServices;
5using System.Security.Cryptography.X509Certificates;
7using System.Threading.Tasks;
23 private static string defaultPurpose =
string.Empty;
24 private static int defaultTimeoutMilliseconds = 60000;
26 private readonly Dictionary<string, TaskCompletionSource<LegalIdentityPetitionResponseEventArgs>> identityPetitions =
new Dictionary<string, TaskCompletionSource<LegalIdentityPetitionResponseEventArgs>>();
27 private readonly
object syncObj =
new object();
28 private bool handlerAdded =
false;
42 get => defaultPurpose;
45 if (
string.IsNullOrEmpty(value))
46 throw new ArgumentException(
"Default purpose cannot be empty.", nameof(
DefaultPurpose));
48 defaultPurpose = value;
57 get => defaultTimeoutMilliseconds;
63 defaultTimeoutMilliseconds = value;
80 if (
string.Compare(Uri.Scheme,
"iotid",
true) == 0)
99 if (this.handlerAdded)
101 Gateway.ContractsClient.PetitionedIdentityResponseReceived -= this.Client_PetitionedIdentityResponseReceived;
102 this.handlerAdded =
false;
116 params KeyValuePair<string, string>[] Headers)
118 return this.
GetAsync(Uri, Certificate, RemoteCertificateValidator, defaultTimeoutMilliseconds, Headers);
131 int TimeoutMs, params KeyValuePair<string, string>[] Headers)
134 ??
throw new NotSupportedException(
"Contracts feature not supported or activated on this system.");
138 if (!this.handlerAdded)
140 Client.PetitionedIdentityResponseReceived += this.Client_PetitionedIdentityResponseReceived;
141 this.handlerAdded =
true;
145 string LegalId = Uri.AbsolutePath;
153 TaskCompletionSource<LegalIdentityPetitionResponseEventArgs> Result =
new TaskCompletionSource<LegalIdentityPetitionResponseEventArgs>();
154 string PetitionId = Guid.NewGuid().ToString();
155 string Purpose =
null;
157 if (!
string.IsNullOrEmpty(Uri.Query))
159 NameValueCollection Parameters = HttpUtility.ParseQueryString(Uri.Query);
161 foreach (
string Parameter in Parameters.AllKeys)
173 if (
string.IsNullOrEmpty(Purpose))
174 Purpose =
"Processing referenced identity.";
178 this.identityPetitions[PetitionId] = Result;
185 Task _ = Task.Delay(TimeoutMs).ContinueWith((T) =>
187 Result.TrySetException(
new TimeoutException(
"Response to petition not received within allotted time."));
188 return Task.CompletedTask;
195 this.identityPetitions.Remove(PetitionId);
198 ExceptionDispatchInfo.Capture(ex).Throw();
211 if (this.identityPetitions.TryGetValue(e.
PetitionId, out TaskCompletionSource<LegalIdentityPetitionResponseEventArgs> Result))
212 Result.TrySetResult(e);
215 return Task.CompletedTask;
226 public Task<KeyValuePair<string, TemporaryStream>>
GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
229 return this.
GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, defaultTimeoutMilliseconds, Headers);
241 public async Task<KeyValuePair<string, TemporaryStream>>
GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
245 StringBuilder Xml =
new StringBuilder();
246 Identity.
Serialize(Xml,
true,
true,
true,
true,
true,
true,
true);
247 byte[] Bin = Encoding.UTF8.GetBytes(Xml.ToString());
257 ExceptionDispatchInfo.Capture(ex).Throw();
261 return new KeyValuePair<string, TemporaryStream>(
"application/xml; charset=utf-8", Result);
Adds support for legal identities, smart contracts and signatures to an XMPP client.
Task PetitionIdentityAsync(string LegalId, string PetitionId, string Purpose)
Sends a petition to the owner of a legal identity, to access the information in the identity....
Task< LegalIdentity > GetLegalIdentityAsync(string LegalIdentityId)
Gets legal identity registered with the account.
Event arguments for legal identity petition responses
string PetitionId
Petition ID
LegalIdentity RequestedIdentity
Requested identity, if accepted, null if rejected.
void Serialize(StringBuilder Xml, bool IncludeNamespace, bool IncludeIdAttribute, bool IncludeClientSignature, bool IncludeAttachments, bool IncludeStatus, bool IncludeServerSignature, bool IncludeAttachmentReferences)
Serializes the identity to XML
Abstract base class for contractual parameters
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Dispose(bool disposing)
Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed res...
override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within...
async Task< object > GetAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
Task< KeyValuePair< string, TemporaryStream > > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
string[] UriSchemes
Supported URI schemes.
void Dispose()
IDisposable.Dispose
LegalIdentityGetter()
Gets legal identities.
bool CanGet(Uri Uri, out Grade Grade)
If the getter is able to get a resource, given its URI.
static string DefaultPurpose
Default purpose string.
async Task< KeyValuePair< string, TemporaryStream > > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
Task< object > GetAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
static int DefaultTimeoutMilliseconds
Default petition timeout, in milliseconds.
Basic interface for Internet Content getters. A class implementing this interface and having a defaul...
delegate void RemoteCertificateEventHandler(object Sender, RemoteCertificateEventArgs e)
Delegate for remote certificate event handlers.