From b216489b76ad3e47446729cf52509a55ea30757b Mon Sep 17 00:00:00 2001 From: Anthony Ercolano Date: Thu, 25 Oct 2018 12:05:53 -0700 Subject: [PATCH] (fix) Adjust optionality of authentication providers. --- common/core/src/authentication_provider.ts | 3 +- .../core/src/sak_authentication_provider.ts | 37 ++++++++++--------- .../_device_method_exchange_test.js | 2 - 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/common/core/src/authentication_provider.ts b/common/core/src/authentication_provider.ts index 24e4ee599..dea78fa15 100644 --- a/common/core/src/authentication_provider.ts +++ b/common/core/src/authentication_provider.ts @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. import { TransportConfig } from './authorization'; +import { Callback } from './promise_utils'; /** * Designate the type of authentication used by an `AuthenticationProvider`. @@ -23,5 +24,5 @@ export enum AuthenticationType { */ export interface AuthenticationProvider { type: AuthenticationType; - getDeviceCredentials(callback: (err: Error, credentials?: TransportConfig) => void): void; + getDeviceCredentials(callback?: Callback): Promise | void; } diff --git a/device/core/src/sak_authentication_provider.ts b/device/core/src/sak_authentication_provider.ts index ad8b4f1f2..8ea85d8f3 100644 --- a/device/core/src/sak_authentication_provider.ts +++ b/device/core/src/sak_authentication_provider.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. import { EventEmitter } from 'events'; -import { AuthenticationProvider, AuthenticationType, ConnectionString, SharedAccessSignature, errors, TransportConfig, encodeUriComponentStrict } from 'azure-iot-common'; +import { AuthenticationProvider, AuthenticationType, ConnectionString, SharedAccessSignature, errors, TransportConfig, encodeUriComponentStrict, Callback, callbackToPromise } from 'azure-iot-common'; /** * 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 /** * This method is used by the transports to gets the most current device credentials in the form of a `TransportConfig` object. * - * @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. + * @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. + * @returns {Promise | void} Promise if no callback function was passed, void otherwise. */ - getDeviceCredentials(callback: (err: Error, credentials?: TransportConfig) => void): void { - if (this._shouldRenewToken()) { - this._renewToken((err, creds) => { - if (err) { - callback(err); - } else { - /*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.]*/ - this._scheduleNextExpiryTimeout(); - callback(null, creds); - } - }); - } else { - /*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.]*/ - callback(null, this._credentials); - } + getDeviceCredentials(callback?: Callback): Promise | void { + return callbackToPromise((_callback) => { + if (this._shouldRenewToken()) { + this._renewToken((err, creds) => { + if (err) { + _callback(err); + } else { + /*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.]*/ + this._scheduleNextExpiryTimeout(); + _callback(null, creds); + } + }); + } else { + /*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.]*/ + _callback(null, this._credentials); + } + }, callback); } /** diff --git a/device/core/test/device_method/_device_method_exchange_test.js b/device/core/test/device_method/_device_method_exchange_test.js index 95da087f7..3cbe52c12 100644 --- a/device/core/test/device_method/_device_method_exchange_test.js +++ b/device/core/test/device_method/_device_method_exchange_test.js @@ -15,8 +15,6 @@ describe("DeviceMethodRequest", function() { const request = new DeviceMethodRequest("requestId", "methodName"); const response = new DeviceMethodResponse("otherRequestId", {}); const exchange = createDeviceMethodExchange(request, response); - - console.log(exchange); assert.deepEqual(request, exchange.request); assert.deepEqual(response, exchange.response); });