Skip to content

Commit 86092fb

Browse files
authored
Update auth header logic (#842)
* Update auth header logic
1 parent d3f5876 commit 86092fb

File tree

3 files changed

+29
-69
lines changed

3 files changed

+29
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dropbox",
3-
"version": "10.14.0",
3+
"version": "10.15.0",
44
"registry": "npm",
55
"description": "The Dropbox JavaScript SDK is a lightweight, promise based interface to the Dropbox v2 API that works in both nodejs and browser environments.",
66
"main": "cjs/index.js",

src/dropbox.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export default class Dropbox {
8686
auth = TEAM_AUTH;
8787
} else if (authTypes.includes(APP_AUTH)) {
8888
auth = APP_AUTH;
89+
} else {
90+
auth = USER_AUTH; // Default to user auth
8991
}
9092
}
9193

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

117-
let authHeader;
118-
switch (auth) {
119-
case APP_AUTH:
120-
if (!this.auth.clientId || !this.auth.clientSecret) {
121-
throw new Error('A client id and secret is required for this function');
122-
}
123-
authHeader = b64(`${this.auth.clientId}:${this.auth.clientSecret}`);
124-
fetchOptions.headers.Authorization = `Basic ${authHeader}`;
125-
break;
126-
case TEAM_AUTH:
127-
case USER_AUTH:
128-
fetchOptions.headers.Authorization = `Bearer ${this.auth.getAccessToken()}`;
129-
break;
130-
case NO_AUTH:
131-
break;
132-
case COOKIE:
133-
break;
134-
default:
135-
throw new Error(`Unhandled auth type: ${auth}`);
136-
}
137-
119+
this.setAuthHeaders(auth, fetchOptions);
138120
this.setCommonHeaders(fetchOptions);
121+
139122
return fetchOptions;
140123
})
141124
.then((fetchOptions) => this.fetch(
@@ -148,18 +131,14 @@ export default class Dropbox {
148131
downloadRequest(path, args, auth, host) {
149132
return this.auth.checkAndRefreshAccessToken()
150133
.then(() => {
151-
if (auth !== USER_AUTH) {
152-
throw new Error(`Unexpected auth type: ${auth}`);
153-
}
154-
155134
const fetchOptions = {
156135
method: 'POST',
157136
headers: {
158-
Authorization: `Bearer ${this.auth.getAccessToken()}`,
159137
'Dropbox-API-Arg': httpHeaderSafeJson(args),
160138
},
161139
};
162140

141+
this.setAuthHeaders(auth, fetchOptions);
163142
this.setCommonHeaders(fetchOptions);
164143

165144
return fetchOptions;
@@ -174,23 +153,19 @@ export default class Dropbox {
174153
uploadRequest(path, args, auth, host) {
175154
return this.auth.checkAndRefreshAccessToken()
176155
.then(() => {
177-
if (auth !== USER_AUTH) {
178-
throw new Error(`Unexpected auth type: ${auth}`);
179-
}
180-
181156
const { contents } = args;
182157
delete args.contents;
183158

184159
const fetchOptions = {
185160
body: contents,
186161
method: 'POST',
187162
headers: {
188-
Authorization: `Bearer ${this.auth.getAccessToken()}`,
189163
'Content-Type': 'application/octet-stream',
190164
'Dropbox-API-Arg': httpHeaderSafeJson(args),
191165
},
192166
};
193167

168+
this.setAuthHeaders(auth, fetchOptions);
194169
this.setCommonHeaders(fetchOptions);
195170

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

180+
setAuthHeaders(auth, fetchOptions) {
181+
switch (auth) {
182+
case APP_AUTH:
183+
if (this.auth.clientId && this.auth.clientSecret) {
184+
const authHeader = b64(`${this.auth.clientId}:${this.auth.clientSecret}`);
185+
fetchOptions.headers.Authorization = `Basic ${authHeader}`;
186+
}
187+
break;
188+
case TEAM_AUTH:
189+
case USER_AUTH:
190+
if (this.auth.getAccessToken()) {
191+
fetchOptions.headers.Authorization = `Bearer ${this.auth.getAccessToken()}`;
192+
}
193+
break;
194+
case NO_AUTH:
195+
case COOKIE:
196+
break;
197+
default:
198+
throw new Error(`Unhandled auth type: ${auth}`);
199+
}
200+
}
201+
205202
setCommonHeaders(options) {
206203
if (this.selectUser) {
207204
options.headers['Dropbox-API-Select-User'] = this.selectUser;

test/unit/dropbox.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,6 @@ describe('Dropbox', () => {
134134
chai.assert.equal('path', dbx.uploadRequest.getCall(0).args[0]);
135135
chai.assert.deepEqual({}, dbx.uploadRequest.getCall(0).args[1]);
136136
});
137-
138-
it('throws an error for team auth', () => {
139-
const dbx = new Dropbox();
140-
return chai.assert.isRejected(dbx.uploadRequest('path', {}, TEAM_AUTH, 'api'), Error, `Unexpected auth type: ${TEAM_AUTH}`);
141-
});
142-
it('throws an error for app auth', () => {
143-
const dbx = new Dropbox();
144-
return chai.assert.isRejected(dbx.uploadRequest('path', {}, APP_AUTH, 'api'), Error, `Unexpected auth type: ${APP_AUTH}`);
145-
});
146-
it('throws an error for no-auth', () => {
147-
const dbx = new Dropbox();
148-
return chai.assert.isRejected(dbx.uploadRequest('path', {}, NO_AUTH, 'api'), Error, `Unexpected auth type: ${NO_AUTH}`);
149-
});
150-
it('throws an error for cookie auth', () => {
151-
const dbx = new Dropbox();
152-
return chai.assert.isRejected(dbx.uploadRequest('path', {}, COOKIE, 'api'), Error, `Unexpected auth type: ${COOKIE}`);
153-
});
154137
});
155138

156139
describe('Download Requests', () => {
@@ -165,26 +148,6 @@ describe('Dropbox', () => {
165148
chai.assert.equal('path', dbx.downloadRequest.getCall(0).args[0]);
166149
chai.assert.deepEqual({}, dbx.downloadRequest.getCall(0).args[1]);
167150
});
168-
169-
it('throws an error for team auth', () => {
170-
const dbx = new Dropbox();
171-
return chai.assert.isRejected(dbx.downloadRequest('path', {}, TEAM_AUTH, 'api'), Error, `Unexpected auth type: ${TEAM_AUTH}`);
172-
});
173-
174-
it('throws an error for app auth', () => {
175-
const dbx = new Dropbox();
176-
return chai.assert.isRejected(dbx.downloadRequest('path', {}, APP_AUTH, 'api'), Error, `Unexpected auth type: ${APP_AUTH}`);
177-
});
178-
179-
it('throws an error for no-auth', () => {
180-
const dbx = new Dropbox();
181-
return chai.assert.isRejected(dbx.downloadRequest('path', {}, NO_AUTH, 'api'), Error, `Unexpected auth type: ${NO_AUTH}`);
182-
});
183-
184-
it('throws an error for cookie auth', () => {
185-
const dbx = new Dropbox();
186-
return chai.assert.isRejected(dbx.downloadRequest('path', {}, COOKIE, 'api'), Error, `Unexpected auth type: ${COOKIE}`);
187-
});
188151
});
189152

190153
describe('pathRoot', () => {

0 commit comments

Comments
 (0)