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 2 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
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_DATAFILE_URL_TEMPLATE = `https://www.optimizely-cdn.com/datafiles/auth/%s.json`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest DEFAULT_AUTHENTICATED_URL_TEMPLATE - everything pertains to datafiles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


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
1 change: 1 addition & 0 deletions packages/datafile-manager/src/datafileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ export interface DatafileManagerConfig {
updateInterval?: number;
urlTemplate?: string;
cache?: PersistentKeyValueCache;
authDatafileToken?: string
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to isolate this functionality to Node, this shouldn't be added here. DatafileManagerConfig is used by the browser datafile manager. We should create a new interface accepted only by the Node datafile manager.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}
19 changes: 19 additions & 0 deletions packages/datafile-manager/src/nodeDatafileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,28 @@ import { makeGetRequest } from './nodeRequest';
import HttpPollingDatafileManager from './httpPollingDatafileManager';
import { Headers, AbortableRequest } from './http';
import { DatafileManagerConfig } from './datafileManager';
import {
DEFAULT_URL_TEMPLATE,
DEFAULT_AUTHENTICATED_DATAFILE_URL_TEMPLATE,
} from './config';

export default class NodeDatafileManager extends HttpPollingDatafileManager {

private authToken?: string;

constructor(config: DatafileManagerConfig) {
const defaultUrlTemplate = config.authDatafileToken ? DEFAULT_AUTHENTICATED_DATAFILE_URL_TEMPLATE : DEFAULT_URL_TEMPLATE;
super({
... config,
urlTemplate: config.urlTemplate || defaultUrlTemplate,
});
this.authToken = config.authDatafileToken;
}

protected makeGetRequest(reqUrl: string, headers: Headers): AbortableRequest {
if (this.authToken) {
headers['Authorization'] = `Bearer ${this.authToken}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Let's not mutate the headers object, rather make a shallow copy and add this header.
  • Let's log at debug level, of course NOT included the secret in the log message, but the message should indicate that we added an Authorization: Bearer header.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}
return makeGetRequest(reqUrl, headers);
}

Expand Down