How to do Certificate-based authentication in React Native using Rest API?

Shahanshah Alam 0 Reputation points
2024-07-16T09:53:07.2866667+00:00

Hi
I want to do certificate based authentication in React Native, for that I am using Rest API.
And from the docs https://video2.skills-academy.com/en-us/azure/iot-dps/how-to-control-access#certificate-based-authentication I found out this cURL, and I need to send certificate and key file in API, as https.agent is not supported by React Native, how can I manage to do that?

curl -L -i -X PUT –cert ./[device_cert].pem –key ./[device_cert_private_key].pem -H 'Content-Type: application/json' -H 'Content-Encoding:  utf-8' -d '{"registrationId": "[registration_id]"}' https://global.azure-devices-provisioning.net/[ID_Scope]/registrations/[registration_id]/register?api-version=2021-06-01

So far what I have tried is

 const data = {
    registrationId: registrationId,
  };
  const cert = await RNFS.readFile(certificatePath, 'utf8');
  const key = await RNFS.readFile(keyPath, 'utf8');
  console.log('cert', cert);
  console.log('key', key);
  try {
    const httpsAgent = new https.Agent({
      cert: cert,
      key: key,
      keepAlive: true,
    });
    console.log('httpsAgent', httpsAgent);
    const response = await axios({
      method: 'PUT',
      url: `https://global.azure-devices-provisioning.net/${scopeId}/registrations/${registrationId}/register?api-version=2021-06-01`,
      data: data,
      httpsAgent: httpsAgent,
      headers: {
        'Content-Type': 'application/json',
        'Content-Encoding': 'utf-8',
      },
    });
    return response.data;
  } catch (err) {
    console.log('err', err);
    return err;
  }

Thank you!!!

Azure IoT
Azure IoT
A category of Azure services for internet of things devices.
397 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,176 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 32,011 Reputation points MVP
    2024-07-17T07:27:49.7833333+00:00

    Hello @Shahanshah Alam,

    welcome to this moderated Azure community forum.

    The DPS is only applicable for pointing a device to an iot hub.

    Only for that (one-time) provisioning, the Device Provisioning Service is used.

    The DPS takes not part in the file upload.

    If you use an SDK like documented here, the SDK takes all the security related logic away.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.