Skip to content

Commit

Permalink
Update auth header logic (#842)
Browse files Browse the repository at this point in the history
* Update auth header logic
  • Loading branch information
rogebrd authored Oct 27, 2021
1 parent d3f5876 commit 86092fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dropbox",
"version": "10.14.0",
"version": "10.15.0",
"registry": "npm",
"description": "The Dropbox JavaScript SDK is a lightweight, promise based interface to the Dropbox v2 API that works in both nodejs and browser environments.",
"main": "cjs/index.js",
Expand Down
59 changes: 28 additions & 31 deletions src/dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export default class Dropbox {
auth = TEAM_AUTH;
} else if (authTypes.includes(APP_AUTH)) {
auth = APP_AUTH;
} else {
auth = USER_AUTH; // Default to user auth
}
}

Expand Down Expand Up @@ -114,28 +116,9 @@ export default class Dropbox {
fetchOptions.headers['Content-Type'] = 'application/json';
}

let authHeader;
switch (auth) {
case APP_AUTH:
if (!this.auth.clientId || !this.auth.clientSecret) {
throw new Error('A client id and secret is required for this function');
}
authHeader = b64(`${this.auth.clientId}:${this.auth.clientSecret}`);
fetchOptions.headers.Authorization = `Basic ${authHeader}`;
break;
case TEAM_AUTH:
case USER_AUTH:
fetchOptions.headers.Authorization = `Bearer ${this.auth.getAccessToken()}`;
break;
case NO_AUTH:
break;
case COOKIE:
break;
default:
throw new Error(`Unhandled auth type: ${auth}`);
}

this.setAuthHeaders(auth, fetchOptions);
this.setCommonHeaders(fetchOptions);

return fetchOptions;
})
.then((fetchOptions) => this.fetch(
Expand All @@ -148,18 +131,14 @@ export default class Dropbox {
downloadRequest(path, args, auth, host) {
return this.auth.checkAndRefreshAccessToken()
.then(() => {
if (auth !== USER_AUTH) {
throw new Error(`Unexpected auth type: ${auth}`);
}

const fetchOptions = {
method: 'POST',
headers: {
Authorization: `Bearer ${this.auth.getAccessToken()}`,
'Dropbox-API-Arg': httpHeaderSafeJson(args),
},
};

this.setAuthHeaders(auth, fetchOptions);
this.setCommonHeaders(fetchOptions);

return fetchOptions;
Expand All @@ -174,23 +153,19 @@ export default class Dropbox {
uploadRequest(path, args, auth, host) {
return this.auth.checkAndRefreshAccessToken()
.then(() => {
if (auth !== USER_AUTH) {
throw new Error(`Unexpected auth type: ${auth}`);
}

const { contents } = args;
delete args.contents;

const fetchOptions = {
body: contents,
method: 'POST',
headers: {
Authorization: `Bearer ${this.auth.getAccessToken()}`,
'Content-Type': 'application/octet-stream',
'Dropbox-API-Arg': httpHeaderSafeJson(args),
},
};

this.setAuthHeaders(auth, fetchOptions);
this.setCommonHeaders(fetchOptions);

return fetchOptions;
Expand All @@ -202,6 +177,28 @@ export default class Dropbox {
.then((res) => parseResponse(res));
}

setAuthHeaders(auth, fetchOptions) {
switch (auth) {
case APP_AUTH:
if (this.auth.clientId && this.auth.clientSecret) {
const authHeader = b64(`${this.auth.clientId}:${this.auth.clientSecret}`);
fetchOptions.headers.Authorization = `Basic ${authHeader}`;
}
break;
case TEAM_AUTH:
case USER_AUTH:
if (this.auth.getAccessToken()) {
fetchOptions.headers.Authorization = `Bearer ${this.auth.getAccessToken()}`;
}
break;
case NO_AUTH:
case COOKIE:
break;
default:
throw new Error(`Unhandled auth type: ${auth}`);
}
}

setCommonHeaders(options) {
if (this.selectUser) {
options.headers['Dropbox-API-Select-User'] = this.selectUser;
Expand Down
37 changes: 0 additions & 37 deletions test/unit/dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,6 @@ describe('Dropbox', () => {
chai.assert.equal('path', dbx.uploadRequest.getCall(0).args[0]);
chai.assert.deepEqual({}, dbx.uploadRequest.getCall(0).args[1]);
});

it('throws an error for team auth', () => {
const dbx = new Dropbox();
return chai.assert.isRejected(dbx.uploadRequest('path', {}, TEAM_AUTH, 'api'), Error, `Unexpected auth type: ${TEAM_AUTH}`);
});
it('throws an error for app auth', () => {
const dbx = new Dropbox();
return chai.assert.isRejected(dbx.uploadRequest('path', {}, APP_AUTH, 'api'), Error, `Unexpected auth type: ${APP_AUTH}`);
});
it('throws an error for no-auth', () => {
const dbx = new Dropbox();
return chai.assert.isRejected(dbx.uploadRequest('path', {}, NO_AUTH, 'api'), Error, `Unexpected auth type: ${NO_AUTH}`);
});
it('throws an error for cookie auth', () => {
const dbx = new Dropbox();
return chai.assert.isRejected(dbx.uploadRequest('path', {}, COOKIE, 'api'), Error, `Unexpected auth type: ${COOKIE}`);
});
});

describe('Download Requests', () => {
Expand All @@ -165,26 +148,6 @@ describe('Dropbox', () => {
chai.assert.equal('path', dbx.downloadRequest.getCall(0).args[0]);
chai.assert.deepEqual({}, dbx.downloadRequest.getCall(0).args[1]);
});

it('throws an error for team auth', () => {
const dbx = new Dropbox();
return chai.assert.isRejected(dbx.downloadRequest('path', {}, TEAM_AUTH, 'api'), Error, `Unexpected auth type: ${TEAM_AUTH}`);
});

it('throws an error for app auth', () => {
const dbx = new Dropbox();
return chai.assert.isRejected(dbx.downloadRequest('path', {}, APP_AUTH, 'api'), Error, `Unexpected auth type: ${APP_AUTH}`);
});

it('throws an error for no-auth', () => {
const dbx = new Dropbox();
return chai.assert.isRejected(dbx.downloadRequest('path', {}, NO_AUTH, 'api'), Error, `Unexpected auth type: ${NO_AUTH}`);
});

it('throws an error for cookie auth', () => {
const dbx = new Dropbox();
return chai.assert.isRejected(dbx.downloadRequest('path', {}, COOKIE, 'api'), Error, `Unexpected auth type: ${COOKIE}`);
});
});

describe('pathRoot', () => {
Expand Down

0 comments on commit 86092fb

Please sign in to comment.