This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
iothub-explorer-sas-token.js
61 lines (50 loc) · 2.29 KB
/
iothub-explorer-sas-token.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
// External dependencies
var program = require('commander');
// Local dependencies
var inputError = require('./common.js').inputError;
var serviceError = require('./common.js').serviceError;
var printSuccess = require('./common.js').printSuccess;
var getHostFromSas = require('./common.js').getHostFromSas;
var getSas = require('./common.js').getSas;
// Azure IoT SDK dependencies
var deviceSas = require('azure-iot-device').SharedAccessSignature;
var Registry = require('azure-iothub').Registry;
var showDeprecationText = require('./common.js').showDeprecationText;
showDeprecationText('az iot hub generate-sas-token');
program
.description('Generate a shared access signature token for the given device with an expiry time <num-seconds> from now')
.option('-l, --login <connectionString>', 'use the connection string provided as argument to use to authenticate with your IoT hub')
.option('-d, --duration <durationInSeconds>', 'expiration time (in seconds): if not specified, the default is one hour', parseInt)
.parse(process.argv);
if(!program.args[0]) inputError('You must specify a device id.');
var deviceId = program.args[0];
var nowInSeconds = Math.floor(Date.now() / 1000);
var expiry = program.duration ? nowInSeconds + program.duration : nowInSeconds + 3600;
if (isNaN(new Date(expiry * 1000))) {
inputError('Invalid duration');
}
var serviceSas = getSas(program.login);
var registry = Registry.fromSharedAccessSignature(serviceSas);
registry.get(deviceId, function (err, device) {
if (err)
serviceError(err);
else {
var key = device.authentication.symmetricKey.primaryKey || device.authentication.symmetricKey.secondaryKey;
if (!key) {
inputError('Cannot create a SAS for this device. It does not use symmetric key authentication.');
}
var host = getHostFromSas(serviceSas);
var sas = deviceSas.create(host, deviceId, key, expiry);
if (program.raw) {
console.log(sas.toString());
} else {
printSuccess('Shared Access Key for ' + deviceId + ':');
console.log(sas.toString());
printSuccess('is valid until: ' + new Date(expiry * 1000).toString());
}
}
});