Skip to content

Commit b216489

Browse files
author
Anthony Ercolano
committed
(fix) Adjust optionality of authentication providers.
1 parent b01a075 commit b216489

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

common/core/src/authentication_provider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
import { TransportConfig } from './authorization';
5+
import { Callback } from './promise_utils';
56

67
/**
78
* Designate the type of authentication used by an `AuthenticationProvider`.
@@ -23,5 +24,5 @@ export enum AuthenticationType {
2324
*/
2425
export interface AuthenticationProvider {
2526
type: AuthenticationType;
26-
getDeviceCredentials(callback: (err: Error, credentials?: TransportConfig) => void): void;
27+
getDeviceCredentials(callback?: Callback<TransportConfig>): Promise<TransportConfig> | void;
2728
}

device/core/src/sak_authentication_provider.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
import { EventEmitter } from 'events';
5-
import { AuthenticationProvider, AuthenticationType, ConnectionString, SharedAccessSignature, errors, TransportConfig, encodeUriComponentStrict } from 'azure-iot-common';
5+
import { AuthenticationProvider, AuthenticationType, ConnectionString, SharedAccessSignature, errors, TransportConfig, encodeUriComponentStrict, Callback, callbackToPromise } from 'azure-iot-common';
66

77
/**
88
* Provides an `AuthenticationProvider` object that can be created simply with a connection string and is then used by the device client and transports to authenticate
@@ -47,23 +47,26 @@ export class SharedAccessKeyAuthenticationProvider extends EventEmitter implemen
4747
/**
4848
* This method is used by the transports to gets the most current device credentials in the form of a `TransportConfig` object.
4949
*
50-
* @param callback function that will be called with either an error or a set of device credentials that can be used to authenticate with the IoT hub.
50+
* @param [callback] optional function that will be called with either an error or a set of device credentials that can be used to authenticate with the IoT hub.
51+
* @returns {Promise<TransportConfig> | void} Promise if no callback function was passed, void otherwise.
5152
*/
52-
getDeviceCredentials(callback: (err: Error, credentials?: TransportConfig) => void): void {
53-
if (this._shouldRenewToken()) {
54-
this._renewToken((err, creds) => {
55-
if (err) {
56-
callback(err);
57-
} else {
58-
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_002: [The `getDeviceCredentials` method shall start a timer that will automatically renew the token every (`tokenValidTimeInSeconds` - `tokenRenewalMarginInSeconds`) seconds if specified, or 45 minutes by default.]*/
59-
this._scheduleNextExpiryTimeout();
60-
callback(null, creds);
61-
}
62-
});
63-
} else {
64-
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_003: [The `getDeviceCredentials` should call its callback with a `null` first parameter and a `TransportConfig` object as a second parameter, containing the latest valid token it generated.]*/
65-
callback(null, this._credentials);
66-
}
53+
getDeviceCredentials(callback?: Callback<TransportConfig>): Promise<TransportConfig> | void {
54+
return callbackToPromise((_callback) => {
55+
if (this._shouldRenewToken()) {
56+
this._renewToken((err, creds) => {
57+
if (err) {
58+
_callback(err);
59+
} else {
60+
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_002: [The `getDeviceCredentials` method shall start a timer that will automatically renew the token every (`tokenValidTimeInSeconds` - `tokenRenewalMarginInSeconds`) seconds if specified, or 45 minutes by default.]*/
61+
this._scheduleNextExpiryTimeout();
62+
_callback(null, creds);
63+
}
64+
});
65+
} else {
66+
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_003: [The `getDeviceCredentials` should call its callback with a `null` first parameter and a `TransportConfig` object as a second parameter, containing the latest valid token it generated.]*/
67+
_callback(null, this._credentials);
68+
}
69+
}, callback);
6770
}
6871

6972
/**

device/core/test/device_method/_device_method_exchange_test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ describe("DeviceMethodRequest", function() {
1515
const request = new DeviceMethodRequest("requestId", "methodName");
1616
const response = new DeviceMethodResponse("otherRequestId", {});
1717
const exchange = createDeviceMethodExchange(request, response);
18-
19-
console.log(exchange);
2018
assert.deepEqual(request, exchange.request);
2119
assert.deepEqual(response, exchange.response);
2220
});

0 commit comments

Comments
 (0)