|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
3 | 3 |
|
4 | 4 | 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'; |
6 | 6 |
|
7 | 7 | /**
|
8 | 8 | * 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
|
47 | 47 | /**
|
48 | 48 | * This method is used by the transports to gets the most current device credentials in the form of a `TransportConfig` object.
|
49 | 49 | *
|
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. |
51 | 52 | */
|
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); |
67 | 70 | }
|
68 | 71 |
|
69 | 72 | /**
|
|
0 commit comments