Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support for Authenticated Datafiles #498

Merged
merged 8 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/datafile-manager/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
Changes that have landed but are not yet released.

### Changed
- Added support for authenticated datafiles. `NodeDatafileManager` now accepts `datafileAccessToken` to be able to fetch authentication datafiles.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Added support for authenticated datafiles. `NodeDatafileManager` now accepts `datafileAccessToken` to be able to fetch authentication datafiles.
- Added support for authenticated datafiles. `NodeDatafileManager` now accepts `datafileAccessToken` to be able to fetch authenticated datafiles.


## [0.5.0] - April 17, 2020

### Breaking Changes
Expand Down
76 changes: 76 additions & 0 deletions packages/datafile-manager/__test__/nodeDatafileManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,80 @@ describe('nodeDatafileManager', () => {

await manager.stop();
});

it('uses authenticated default datafile url when auth token is provided', async () => {
makeGetRequestSpy.mockReturnValue({
abort: jest.fn(),
responsePromise: Promise.resolve({
statusCode: 200,
body: '{"foo":"bar"}',
headers: {},
}),
});
const manager = new NodeDatafileManager({
sdkKey: '1234',
datafileAccessToken: 'abcdefgh',
});
manager.start();
expect(makeGetRequestSpy).toBeCalledTimes(1);
expect(makeGetRequestSpy).toBeCalledWith('https://www.optimizely-cdn.com/datafiles/auth/1234.json', expect.anything());
await manager.stop();
});

it('uses public default datafile url when auth token is not provided', async () => {
makeGetRequestSpy.mockReturnValue({
abort: jest.fn(),
responsePromise: Promise.resolve({
statusCode: 200,
body: '{"foo":"bar"}',
headers: {},
}),
});
const manager = new NodeDatafileManager({
sdkKey: '1234',
});
manager.start();
expect(makeGetRequestSpy).toBeCalledTimes(1);
expect(makeGetRequestSpy).toBeCalledWith('https://cdn.optimizely.com/datafiles/1234.json', expect.anything());
await manager.stop();
});

it('adds authorization header with bearer token when auth token is provided', async () => {
makeGetRequestSpy.mockReturnValue({
abort: jest.fn(),
responsePromise: Promise.resolve({
statusCode: 200,
body: '{"foo":"bar"}',
headers: {},
}),
});
const manager = new NodeDatafileManager({
sdkKey: '1234',
datafileAccessToken: 'abcdefgh',
});
manager.start();
expect(makeGetRequestSpy).toBeCalledTimes(1);
expect(makeGetRequestSpy).toBeCalledWith(expect.anything(), { 'Authorization': 'Bearer abcdefgh'});
await manager.stop();
});

it('prefers user provided url template over defaults', async () => {
makeGetRequestSpy.mockReturnValue({
abort: jest.fn(),
responsePromise: Promise.resolve({
statusCode: 200,
body: '{"foo":"bar"}',
headers: {},
}),
});
const manager = new NodeDatafileManager({
sdkKey: '1234',
datafileAccessToken: 'abcdefgh',
urlTemplate: 'https://myawesomeurl/'
});
manager.start();
expect(makeGetRequestSpy).toBeCalledTimes(1);
expect(makeGetRequestSpy).toBeCalledWith('https://myawesomeurl/', expect.anything());
await manager.stop();
});
});
2 changes: 2 additions & 0 deletions packages/datafile-manager/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const MIN_UPDATE_INTERVAL = 1000;

export const DEFAULT_URL_TEMPLATE = `https://cdn.optimizely.com/datafiles/%s.json`;

export const DEFAULT_AUTHENTICATED_URL_TEMPLATE = `https://www.optimizely-cdn.com/datafiles/auth/%s.json`;

export const BACKOFF_BASE_WAIT_SECONDS_BY_ERROR_COUNT = [0, 8, 16, 32, 64, 128, 256, 512];

export const REQUEST_TIMEOUT_MS = 60 * 1000; // 1 minute
4 changes: 4 additions & 0 deletions packages/datafile-manager/src/datafileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ export interface DatafileManagerConfig {
urlTemplate?: string;
cache?: PersistentKeyValueCache;
}

export interface NodeDatafileManagerConfig extends DatafileManagerConfig {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if NodeDatafileManagerConfig is the right name for this.
Also, not sure if it would be easier to make changes in DatafileManagerConfig and HttpPollingDatafileManager directly instead of extending them. It looks clean, but for instance, it does not make sense to do something like that in go-sdk, so maybe we can have language specific implementation.

I would like to have @mjc1283 input on that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pawels-optimizely It is the right name. This class gets pulled in via a specific entry point which is intended for use in Node. We already had these browser/node-specific subclasses as a way of implementing the datafile GET request differently in each environment. This is another behavior that's different for browser vs. Node, so I think it's appropriate. I'm not strongly opposed to putting this in HttpPollingDatafileManager, and just advising against using this in client-side. But since we already have these classes in place, I don't mind having it here either.

datafileAccessToken?: string;
}
31 changes: 29 additions & 2 deletions packages/datafile-manager/src/nodeDatafileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,41 @@
* limitations under the License.
*/

import { getLogger } from '@optimizely/js-sdk-logging';
import { makeGetRequest } from './nodeRequest';
import HttpPollingDatafileManager from './httpPollingDatafileManager';
import { Headers, AbortableRequest } from './http';
import { DatafileManagerConfig } from './datafileManager';
import {
NodeDatafileManagerConfig,
DatafileManagerConfig,
} from './datafileManager';
import {
DEFAULT_URL_TEMPLATE,
DEFAULT_AUTHENTICATED_URL_TEMPLATE,
} from './config';

const logger = getLogger('NodeDatafileManager');

export default class NodeDatafileManager extends HttpPollingDatafileManager {

private accessToken?: string;

constructor(config: NodeDatafileManagerConfig) {
const defaultUrlTemplate = config.datafileAccessToken ? DEFAULT_AUTHENTICATED_URL_TEMPLATE : DEFAULT_URL_TEMPLATE;
super({
... config,
urlTemplate: config.urlTemplate || defaultUrlTemplate,
});
this.accessToken = config.datafileAccessToken;
}

protected makeGetRequest(reqUrl: string, headers: Headers): AbortableRequest {
return makeGetRequest(reqUrl, headers);
let requestHeaders = Object.assign({}, headers);
if (this.accessToken) {
logger.debug('Adding Authorization header with Bearer Token');
requestHeaders['Authorization'] = `Bearer ${this.accessToken}`;
}
return makeGetRequest(reqUrl, requestHeaders);
}

protected getConfigDefaults(): Partial<DatafileManagerConfig> {
Expand Down