node.js - 如何通过 Javascript (node.js) 生成 Azure SAS token

标签 node.js azure urlencode sha256 azure-iot-hub

我正在尝试生成有效的 SAS token (共享访问签名)来连接到 Azure IoT 中心,但我不断收到错误 Connection refused: Not authorized使用 token 时。

我知道我的连接方式有效,因为当使用 Microsoft Device Explorer ( https://github.com/Azure/azure-iot-sdk-csharp/tree/master/tools/DeviceExplorer ) 生成的 SAS token 时,它会按预期工作。

为了编写 Javascript 代码,我从本文档 https://learn.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1 开始与一些工作实现进行比较:

在下面的 Javascript 实现中我做错了什么?我认为这可能与 SHA256 签名结合 URL 转义字符串有关,但我不确定如何测试/验证这一点。

const crypto = require('crypto');

// on the Azure Portal:
// sharedAccessKeyName is the "policy" field
// sharedAccessKeyValue is the "primary key"

const sign = (iotHubName, expirtyDeltaInSecondsFromNow, sharedAccessKeyName, sharedAccessKeyValue) => {
  // URL encode the IoT Hub host
  const encodedSasUrl = encodeURI(`${iotHubName}.azure-devices.net`.toLowerCase());
  // calculate the expiry end datetime as an epoch
  const expiry = parseInt(Date.now() / 1000 + expirtyDeltaInSecondsFromNow);
  // combine the previous two pieces of information
  const stringToSign = `${encodedSasUrl}\n${expiry}`;
  // sign the string using the primary key (sharedAccessKeyValue) and SHA256
  const hmac = crypto.createHmac('sha256', sharedAccessKeyValue);
  hmac.update(stringToSign);
  const hash = hmac.digest('hex');
  // encode the signed hash to base64 (making sure it's URL escaped)
  const encodedSignature = hash.toString('base64');
  // put all together into the SAS token
  const sasToken = `SharedAccessSignature sig=${encodedSignature}&se=${expiry}&skn=${sharedAccessKeyName}&sr=${encodedSasUrl}`;
  console.log("sasToken:", sasToken);
  return sasToken;
}

最佳答案

Here是通过node.js生成SAS token 的示例。

var generateSasToken = function(resourceUri, signingKey, policyName, expiresInMins) {
    resourceUri = encodeURIComponent(resourceUri);

    // Set expiration in seconds
    var expires = (Date.now() / 1000) + expiresInMins * 60;
    expires = Math.ceil(expires);
    var toSign = resourceUri + '\n' + expires;

    // Use crypto
    var hmac = crypto.createHmac('sha256', new Buffer(signingKey, 'base64'));
    hmac.update(toSign);
    var base64UriEncoded = encodeURIComponent(hmac.digest('base64'));

    // Construct autorization string
    var token = "SharedAccessSignature sr=" + resourceUri + "&sig="
    + base64UriEncoded + "&se=" + expires;
    if (policyName) token += "&skn="+policyName;
    return token;
};

What am I doing wrong in the following Javascript implementation?

我不是 javascript(node.js) 专家。也许你可以通过比较上面的代码和你的代码来找出答案。

关于node.js - 如何通过 Javascript (node.js) 生成 Azure SAS token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51728649/

相关文章:

node.js - 我如何将 GPS 数据发送到 parrot ar 无人机并转到我的无人机的 GPS 位置?

node.js - NodeJS 记录器 : winston. transports.DailyRotateFile 不是函数

mysql - 使用sequelize 连接到aws rds 时连接被拒绝

azure - 当前面的所有任务都成功(少数任务除外)时,运行任务 B(如果任务列表成功则运行任务)

c# - 有没有办法对连接到 Azure SignalR 的用户进行身份验证?

php - 如何显示与其他数据对应的数据 MySQL PHP

javascript - Mapnik 无法确定图像类型

azure - 逻辑应用订阅事件网格域内的主题

zend-framework - URL 编码的参数破坏了默认的 Zend 重写规则

ios - Swift 4 中 '(撇号) 的百分比编码输出不正确