Azure / azure-sdk-for-ios

iOS client SDKs for Microsoft Azure
https://azure.github.io/azure-sdk-for-ios/
MIT License
184 stars 88 forks source link

How to provision device with x509 #1845

Closed Shahanshah-TA closed 1 month ago

Shahanshah-TA commented 1 month ago

Hi I need to provision device with x509 certificate, for that I am trying to use swift code in React Native. But there is multiple documentation present, some are deprecated, some doesn't support DPS. What I should do? which package should I use? is there documentation available for provisioning with x509 in swift or React Native?

Shahanshah-TA commented 1 month ago

Hi @tjprescott @scbedd @benbp @weshaggard @azure-sdk @sima-zhu Can anyone please help

tjprescott commented 1 month ago

@Shahanshah-TA from your issue it's not clear how you are trying to use the Azure SDK for iOS. What documentation are you referring to?

Shahanshah-TA commented 1 month ago

@tjprescott as per this docs https://azure.github.io/azure-sdk-for-ios/ it have AzureCommunicationChat AzureCommunicationCommon AzureCommunicationUI AzureCommunicationUICalling AzureCommunicationUIChat AzureCore AzureAIVisionCore AzureAIVisionFace but I want to provision device with x509 certificate as per this docs Thats what I am trying to understand that can I use this package to achieve this functionality because I cannot find that anywhere for iOS, like I found for Android here.

tjprescott commented 1 month ago

@Shahanshah-TA thank you for the clarification. IoT Device Provisioning isn't part of the Azure SDK for iOS package and thus it's not possible using this SDK.

You might try posting this question to the azure-iot-sdk-java repo, as the service team might have a better idea how to accomplish what you want. However, I suspect that SDK is not intended for mobile per se and that they won't have an iOS-specific solution.

In that case your best option in Swift would be to make calls directly against the REST API using the HTTP library of your choice and the REST API documentation you linked to.

Shahanshah-TA commented 1 month ago

@tjprescott I am facing one issue with REST API approach. The problem is I am unable to add certificate and private key to http request. I am able to fetch the certificate and private key from local storage but I am not sure how to add that in REST API. I am a React Native dev, so I am not that very proficient in swift, if you don't mind can you please help me with that. In node I know we use https.Agent but I am not sure about Swift.

import UIKit
import Foundation
import Security

@objc(AzureProvisionWithCertificate)
class AzureProvisionWithCertificate: NSObject {

  @objc(provisionAndUploadFile:withRegistrationId:withKey:withCertificate:withProvisionHost:withFileNameWithFolder:withModelId:withResolver:withRejecter:)
  func provisionAndUploadFile(scopeId:String, registrationId:String, key:String, certificate:String,  provisionHost:String,  fileNameWithFolder:String,  modelId:String, resolve:@escaping RCTPromiseResolveBlock, reject:@escaping RCTPromiseRejectBlock) -> Void {

    print("started: provisionAndUploadFile api")

        // Create a session with the SSLPinningDelegate
       //let sslPinningDelegate = SSLPinningDelegate(certificate: certificate as! SecCertificate, privateKey: key as! SecKey)
       //print("sslPinningDelegate: \(sslPinningDelegate)")

    // Create the URL
    let url = URL(string: "https://global.azure-devices-provisioning.net/\(scopeId)/registrations/\(registrationId)/register?api-version=2021-06-01")!

    // Create the request
    var request = URLRequest(url: url)
    request.httpMethod = "PUT"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("utf-8", forHTTPHeaderField: "Content-Encoding")
    // Create the request body
    let bodyData = ["registrationId": registrationId]
    let jsonData = try? JSONSerialization.data(withJSONObject: bodyData)

    // Set the request body
    request.httpBody = jsonData

    // Create the session configuration
    let sessionConfig = URLSessionConfiguration.default
    let session = URLSession(configuration: sessionConfig)

    // Create the data task
    let task = session.dataTask(with: request) { (data, response, error) in
        if let error = error {
            print("Error: \(error.localizedDescription)")
            reject("Error", error.localizedDescription, error)
        } else if let data = data {
            // Process the response data
            let responseString = String(data: data, encoding: .utf8)
            print("Response data: \(responseString)")
            resolve("Response data: \(responseString)")
        } else {
            print("No data received")
            reject("Error", "No data received", nil)
        }
    }

    // Start the data task
    task.resume()

  }
}
tjprescott commented 1 month ago

Unfortunately I can't help with that as this repo is for users of the AzureSDK swift package. The mechanism will depend on the service and the networking you use.

However, if you are a react dev, have you considered just using the javascript SDKs? https://www.npmjs.com/package/azure-iot-device https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-sdks#azure-iot-hub-device-sdks

There's also a link to iOS samples using the C library that might be useful: https://github.com/Azure/azure-iot-sdk-c/blob/main/iothub_client/samples/ios/CocoaPods-Samples.md

tjprescott commented 1 month ago

Since this issue is not addressable by the AzureSDK for Swift, I'm closing it.