/// <summary> /// Sign the given payload using the Worload API of the locasl Security Deamon.async /// The signed payload is returned as Base64 string. /// </summary> private async Task <string> SignStringWithModuleKeyAsync(string payload) { string generationId = Environment.GetEnvironmentVariable(ModuleGenerationIdVariableName); Uri workloadUri = new Uri(Environment.GetEnvironmentVariable(WorkloadUriVariableName)); string signedPayload = string.Empty; using (HttpClient httpClient = Microsoft.Azure.Devices.Edge.Util.HttpClientHelper.GetHttpClient(workloadUri)) { httpClient.BaseAddress = new Uri(Microsoft.Azure.Devices.Edge.Util.HttpClientHelper.GetBaseUrl(workloadUri)); var workloadClient = new WorkloadClient(httpClient); var signRequest = new SignRequest() { KeyId = "primary", // or "secondary" Algo = SignRequestAlgo.HMACSHA256, Data = Encoding.UTF8.GetBytes(payload) }; var signResponse = await workloadClient.SignAsync(WorkloadApiVersion, _edgeModuleId, generationId, signRequest); signedPayload = Convert.ToBase64String(signResponse.Digest); } return(signedPayload); }
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <param name="api_version">The version of the API.</param> /// <param name="name">The name of the module on whose behalf the payload will be signed. (urlencoded)</param> /// <param name="genid">The generation identifier for the module as generated by IoT Hub.</param> /// <param name="payload">The data to be signed.</param> /// <returns>Ok</returns> /// <exception cref="ApiException">A server side error occurred.</exception> public async System.Threading.Tasks.Task <SignResponse> SignAsync(string api_version, string name, string genid, SignRequest payload, System.Threading.CancellationToken cancellationToken) { if (name == null) { throw new System.ArgumentNullException("name"); } if (genid == null) { throw new System.ArgumentNullException("genid"); } if (api_version == null) { throw new System.ArgumentNullException("api_version"); } var urlBuilder_ = new System.Text.StringBuilder(); urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/modules/{name}/genid/{genid}/sign?"); urlBuilder_.Replace("{name}", System.Uri.EscapeDataString(ConvertToString(name, System.Globalization.CultureInfo.InvariantCulture))); urlBuilder_.Replace("{genid}", System.Uri.EscapeDataString(ConvertToString(genid, System.Globalization.CultureInfo.InvariantCulture))); urlBuilder_.Append(System.Uri.EscapeDataString("api-version") + "=").Append(System.Uri.EscapeDataString(ConvertToString(api_version, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); urlBuilder_.Length--; var client_ = _httpClient; try { using (var request_ = new System.Net.Http.HttpRequestMessage()) { var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(payload, _settings.Value)); content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json"); request_.Content = content_; request_.Method = new System.Net.Http.HttpMethod("POST"); request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute); PrepareRequest(client_, request_, url_); var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); try { var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value); if (response_.Content != null && response_.Content.Headers != null) { foreach (var item_ in response_.Content.Headers) { headers_[item_.Key] = item_.Value; } } ProcessResponse(client_, response_); var status_ = ((int)response_.StatusCode).ToString(); if (status_ == "200") { var objectResponse_ = await ReadObjectResponseAsync <SignResponse>(response_, headers_).ConfigureAwait(false); return(objectResponse_.Object); } else if (status_ == "404") { var objectResponse_ = await ReadObjectResponseAsync <ErrorResponse>(response_, headers_).ConfigureAwait(false); throw new ApiException <ErrorResponse>("Not Found", (int)response_.StatusCode, objectResponse_.Text, headers_, objectResponse_.Object, null); } else { var objectResponse_ = await ReadObjectResponseAsync <ErrorResponse>(response_, headers_).ConfigureAwait(false); throw new ApiException <ErrorResponse>("Error", (int)response_.StatusCode, objectResponse_.Text, headers_, objectResponse_.Object, null); } } finally { if (response_ != null) { response_.Dispose(); } } } } finally { } }
/// <param name="api_version">The version of the API.</param> /// <param name="name">The name of the module on whose behalf the payload will be signed. (urlencoded)</param> /// <param name="genid">The generation identifier for the module as generated by IoT Hub.</param> /// <param name="payload">The data to be signed.</param> /// <returns>Ok</returns> /// <exception cref="ApiException">A server side error occurred.</exception> public System.Threading.Tasks.Task <SignResponse> SignAsync(string api_version, string name, string genid, SignRequest payload) { return(SignAsync(api_version, name, genid, payload, System.Threading.CancellationToken.None)); }