Skip to content

Commit

Permalink
Merge pull request #405 from Azure/fix-provider-definition
Browse files Browse the repository at this point in the history
(fix) Adjust optionality of authentication providers.
  • Loading branch information
anthonyvercolano authored Oct 25, 2018
2 parents b01a075 + b216489 commit e14178d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
3 changes: 2 additions & 1 deletion common/core/src/authentication_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -23,5 +24,5 @@ export enum AuthenticationType {
*/
export interface AuthenticationProvider {
type: AuthenticationType;
getDeviceCredentials(callback: (err: Error, credentials?: TransportConfig) => void): void;
getDeviceCredentials(callback?: Callback<TransportConfig>): Promise<TransportConfig> | void;
}
37 changes: 20 additions & 17 deletions device/core/src/sak_authentication_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<TransportConfig> | 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<TransportConfig>): Promise<TransportConfig> | 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit e14178d

Please sign in to comment.