private static void RunWith(OnboardResponse onboardResponse) { var httpClient = HttpClientFactory.AuthenticatedHttpClient(onboardResponse); var capabilitiesServices = new CapabilitiesService(new HttpMessagingService(httpClient)); var capabilitiesParameters = new CapabilitiesParameters { OnboardResponse = onboardResponse, ApplicationId = ApplicationId, CertificationVersionId = CertificationVersionId, EnablePushNotifications = CapabilitySpecification.Types.PushNotification.Disabled, CapabilityParameters = new List <CapabilityParameter>() }; var capabilitiesParameter = new CapabilityParameter { Direction = CapabilitySpecification.Types.Direction.SendReceive, TechnicalMessageType = TechnicalMessageTypes.Iso11783TaskdataZip }; capabilitiesParameters.CapabilityParameters.Add(capabilitiesParameter); capabilitiesServices.Send(capabilitiesParameters); Thread.Sleep(TimeSpan.FromSeconds(5)); var fetchMessageService = new FetchMessageService(httpClient); var fetch = fetchMessageService.Fetch(onboardResponse); Assert.Single(fetch); var decodedMessage = DecodeMessageService.Decode(fetch[0].Command.Message); Assert.Equal(201, decodedMessage.ResponseEnvelope.ResponseCode); }
/// <summary> /// Fetch messages from the inbox using the given onboarding response. /// </summary> /// <param name="onboardResponse">All the messages that are in the inbox.</param> /// <returns>-</returns> /// <exception cref="CouldNotFetchMessagesException">Will be thrown if the messages can not be fetched.</exception> public List <MessageResponse> Fetch(OnboardResponse onboardResponse) { Log.Debug("Begin fetching messages."); var httpRequestMessage = new HttpRequestMessage { RequestUri = new Uri(onboardResponse.ConnectionCriteria.Commands) }; httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json)); var httpResponseMessage = _httpClient .SendAsync(httpRequestMessage).Result; if (!httpResponseMessage.IsSuccessStatusCode) { throw new CouldNotFetchMessagesException(httpResponseMessage.StatusCode, httpResponseMessage.Content.ReadAsStringAsync().Result); } var messageResponses = JsonConvert.DeserializeObject <List <MessageResponse> >(httpResponseMessage.Content.ReadAsStringAsync() .Result); Log.Debug("Finished fetching messages."); return(messageResponses); }
/// <summary> /// Create a single HTTP client using the given onboarding response. /// </summary> /// <param name="onboardResponse">The current onboarding response.</param> /// <returns>-</returns> public static HttpClient AuthenticatedNonLoggingHttpClient(OnboardResponse onboardResponse) { var httpClientHandler = new HttpClientHandler(); httpClientHandler.ClientCertificates.Add(X509CertificateService.GetCertificate(onboardResponse)); var httpClient = new HttpClient(httpClientHandler); return(httpClient); }
protected void LogDebugInformation(OnboardResponse onboardResponse) { Log.Debug("******************************************************************************"); Log.Debug($"* [ACCOUNT_ID]: {AccountId}"); Log.Debug($"* [APPLICATION_ID]: {ApplicationId}"); Log.Debug($"* [CERTIFICATION_VERSION_ID]: {ApplicationId}"); Log.Debug($"* [SENSOR_ALTERNATE_ID]: {onboardResponse.SensorAlternateId}"); Log.Debug($"* [DEVICE_ALTERNATE_ID]: {onboardResponse.DeviceAlternateId}"); Log.Debug("******************************************************************************"); }
/// <summary> /// Create a certificate for the given onboarding response from the AR. /// </summary> /// <param name="onboardResponse">-</param> /// <returns>A X509 certificate to use for the communication between endpoint and AR.</returns> /// <exception cref="CouldNotCreateCertificateForTypeException">-</exception> public static X509Certificate GetCertificate(OnboardResponse onboardResponse) { switch (onboardResponse.Authentication.Type) { case "P12": return(new X509Certificate2( Convert.FromBase64String(onboardResponse.Authentication.Certificate), onboardResponse.Authentication.Secret)); case "PEM": { var pemReader = new PemReader( new StringReader(onboardResponse.Authentication.Certificate), new PasswordFinder(onboardResponse.Authentication.Secret)); RSA privateKey; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { privateKey = ToRSA((RsaPrivateCrtKeyParameters)pemReader.ReadObject()); } else { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { privateKey = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)pemReader.ReadObject()); } else { throw new CouldNotCreateCertificateForOsException( $"Could not create a certificate for '${RuntimeInformation.OSDescription}'"); } } var certificate = pemReader.ReadPemObject(); if (certificate.Type == "CERTIFICATE") { return(new X509Certificate2(certificate.Content).CopyWithPrivateKey(privateKey)); } break; } } throw new CouldNotCreateCertificateForTypeException( $"Could not create a certificate for the type '${onboardResponse.Authentication.Type}'"); }
/// <summary> /// Connect the client to the AR. The given options suit the testcases but have to be reviewed for a production environment regarding reconnecting for example. /// </summary> /// <param name="mqttClient">-</param> /// <param name="onboardResponse">-</param> /// <returns>No dedicated response.</returns> public static async Task ConnectMqttClient(IMqttClient mqttClient, OnboardResponse onboardResponse) { var tlsParameters = new MqttClientOptionsBuilderTlsParameters { Certificates = new[] { ReadRootCertificates(), ReadClientCertificate(onboardResponse) }, UseTls = true }; var options = new MqttClientOptionsBuilder() .WithClientId(onboardResponse.ConnectionCriteria.ClientId) .WithTcpServer(onboardResponse.ConnectionCriteria.Host, int.Parse(onboardResponse.ConnectionCriteria.Port)) .WithTls(tlsParameters) .WithCommunicationTimeout(TimeSpan.FromSeconds(20)) .Build(); await mqttClient.ConnectAsync(options); }
/// <summary> /// Reading the client certificate, currently only possible for P12 certificates. /// </summary> /// <param name="onboardResponse">-</param> /// <returns>Certificate</returns> private static X509Certificate2 ReadClientCertificate(OnboardResponse onboardResponse) { return(new X509Certificate2(new X509Certificate2( Convert.FromBase64String(onboardResponse.Authentication.Certificate), onboardResponse.Authentication.Secret))); }
/// <summary> /// Subscribing to a topic to receive the messages from the AR. /// </summary> /// <param name="mqttClient">-</param> /// <param name="onboardResponse">-</param> /// <returns>No dedicated response.</returns> public static async Task SubscribeToTopics(IMqttClient mqttClient, OnboardResponse onboardResponse) { Assert.True(mqttClient.IsConnected); await mqttClient.SubscribeAsync(onboardResponse.ConnectionCriteria.Commands); }