Skip to content

Commit

Permalink
feat(redirects): pass github token to data sources
Browse files Browse the repository at this point in the history
fixes #480 (the "pass the x-github-token along" part)
  • Loading branch information
trieloff committed Apr 15, 2021
1 parent bdbc771 commit 16aacf0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/BaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class BaseConfig {
return this;
}

/**
* Empty method to allow subclasses to intercept setting the
* GitHub token, if authentication option are provided in the
* repo options.
*/
withGithubToken() {
return this;
}

/**
* Set the base repository to fetch a config from
* @param {string} owner username or org
Expand Down Expand Up @@ -87,6 +96,9 @@ class BaseConfig {
url,
options,
};
if (options && options.headers && /^token /.test(options.headers.authorization)) {
this.withGithubToken(options.headers.authorization.replace(/token /, ''));
}
return this;
}

Expand Down
7 changes: 7 additions & 0 deletions src/DynamicRedirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ class DynamicRedirect {
this._data = null;
this._logger = logger;
this._transactionID = null;
this._githubToken = null;
}

withTransactionID(id) {
this._transactionID = id;
return this;
}

withGithubToken(token) {
this._githubToken = token;
return this;
}

async fetch() {
if (!this._data) {
try {
Expand All @@ -78,6 +84,7 @@ class DynamicRedirect {
const res = await fetch(url.href, {
headers: {
'x-request-id': this._transactionID,
'x-github-token': this._githubToken,
},
});
const text = await res.text();
Expand Down
5 changes: 5 additions & 0 deletions src/RedirectConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class RedirectConfig extends SchemaDerivedConfig {
return this;
}

withGithubToken(token) {
this.rrhandler.withGithubToken(token);
return this;
}

async match(path) {
const resolved = await Promise.all(this.redirects.map((redirect) => redirect.match(path)));
return resolved.find((o) => o);
Expand Down
6 changes: 6 additions & 0 deletions src/RedirectRuleHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const RedirectRuleHandler = () => ({
return this;
},

withGithubToken(token) {
this._token = token;
return this;
},

get(target, prop) {
const index = Number.parseInt(prop, 10);
if (!Number.isNaN(index) && index >= 0) {
Expand All @@ -32,6 +37,7 @@ const RedirectRuleHandler = () => ({
return new Redirect(item);
}
return new DynamicRedirect(item, this.logger)
.withGithubToken(this._token)
.withTransactionID(this._transactionID);
}
return target[prop];
Expand Down
2 changes: 2 additions & 0 deletions test/redirectconfigs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('Redirects Config Loading (from GitHub)', () => {

server.get('https://adobeioruntime.net/api/v1/web/helix/helix-services/:path').intercept((req, res) => {
assert.equal(req.headers['x-request-id'], 'random');
assert.equal(req.headers['x-github-token'], 'fake');

if (req.query.src.startsWith('https://adobe.sharepoint.com/')) {
return res.status(200).json([
Expand Down Expand Up @@ -100,6 +101,7 @@ describe('Redirects Config Loading (from GitHub)', () => {
.withCache({ maxSize: 1 })
.withConfigPath(path.resolve(SPEC_ROOT, 'dynamic.yaml'))
.withTransactionID('random')
.withGithubToken('fake')
.init();

assert.deepEqual(config.toJSON().redirects, [
Expand Down

0 comments on commit 16aacf0

Please sign in to comment.