AZ_NODISCARD az_result az_iot_hub_client_sas_get_signature()

in sdk/src/azure/iot/az_iot_hub_client_sas.c [36:81]


AZ_NODISCARD az_result az_iot_hub_client_sas_get_signature(
    az_iot_hub_client const* client,
    uint64_t token_expiration_epoch_time,
    az_span signature,
    az_span* out_signature)
{
  _az_PRECONDITION_NOT_NULL(client);
  _az_PRECONDITION(token_expiration_epoch_time > 0);
  _az_PRECONDITION_VALID_SPAN(signature, 1, false);
  _az_PRECONDITION_NOT_NULL(out_signature);

  az_span remainder = signature;
  int32_t signature_size = az_span_size(signature);

  _az_RETURN_IF_FAILED(
      _az_span_copy_url_encode(remainder, client->_internal.iot_hub_hostname, &remainder));

  _az_RETURN_IF_NOT_ENOUGH_SIZE(remainder, az_span_size(devices_string));
  remainder = az_span_copy(remainder, devices_string);

  _az_RETURN_IF_FAILED(
      _az_span_copy_url_encode(remainder, client->_internal.device_id, &remainder));

  if (az_span_size(client->_internal.options.module_id) > 0)
  {
    _az_RETURN_IF_NOT_ENOUGH_SIZE(remainder, az_span_size(modules_string));
    remainder = az_span_copy(remainder, modules_string);

    _az_RETURN_IF_FAILED(
        _az_span_copy_url_encode(remainder, client->_internal.options.module_id, &remainder));
  }

  _az_RETURN_IF_NOT_ENOUGH_SIZE(
      remainder,
      1 + // LF
          _az_iot_u64toa_size(token_expiration_epoch_time));

  remainder = az_span_copy_u8(remainder, LF);

  _az_RETURN_IF_FAILED(az_span_u64toa(remainder, token_expiration_epoch_time, &remainder));

  *out_signature = az_span_slice(signature, 0, signature_size - az_span_size(remainder));
  _az_LOG_WRITE(AZ_LOG_IOT_SAS_TOKEN, *out_signature);

  return AZ_OK;
}