Skip to content

Commit 65f349a

Browse files
authored
[Auth] update multiauth logic to support refresh only calls using use… (#857)
* [Auth] update multiauth logic to support refresh only calls using user auth
1 parent f121491 commit 65f349a

File tree

3 files changed

+61
-60
lines changed

3 files changed

+61
-60
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.19.0",
3+
"version": "10.20.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: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,6 @@ export default class Dropbox {
7777
}
7878

7979
request(path, args, auth, host, style) {
80-
// checks for multiauth and assigns auth based on priority to create header in switch case
81-
if (auth.split(',').length > 1) {
82-
const authTypes = auth.replace(' ', '').split(',');
83-
if (authTypes.includes(USER_AUTH) && this.auth.getAccessToken()) {
84-
auth = USER_AUTH;
85-
} else if (authTypes.includes(TEAM_AUTH) && this.auth.getAccessToken()) {
86-
auth = TEAM_AUTH;
87-
} else if (authTypes.includes(APP_AUTH)) {
88-
auth = APP_AUTH;
89-
} else {
90-
auth = USER_AUTH; // Default to user auth
91-
}
92-
}
93-
9480
switch (style) {
9581
case RPC:
9682
return this.rpcRequest(path, args, auth, host);
@@ -178,6 +164,18 @@ export default class Dropbox {
178164
}
179165

180166
setAuthHeaders(auth, fetchOptions) {
167+
// checks for multiauth and assigns auth based on priority to create header in switch case
168+
if (auth.split(',').length > 1) {
169+
const authTypes = auth.replace(' ', '').split(',');
170+
if (authTypes.includes(USER_AUTH) && this.auth.getAccessToken()) {
171+
auth = USER_AUTH;
172+
} else if (authTypes.includes(TEAM_AUTH) && this.auth.getAccessToken()) {
173+
auth = TEAM_AUTH;
174+
} else if (authTypes.includes(APP_AUTH)) {
175+
auth = APP_AUTH;
176+
}
177+
}
178+
181179
switch (auth) {
182180
case APP_AUTH:
183181
if (this.auth.clientId && this.auth.clientSecret) {

test/unit/dropbox.js

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -96,51 +96,6 @@ describe('Dropbox', () => {
9696
chai.assert.deepEqual({}, dbx.rpcRequest.getCall(0).args[1]);
9797
});
9898

99-
it('completes a multiauth RPC request with user auth when supplied with an accessToken', () => {
100-
const dbxAuth = new DropboxAuth({ accessToken: 'foo' });
101-
const dbx = new Dropbox({ auth: dbxAuth });
102-
const rpcSpy = sinon.spy(dbx, 'rpcRequest');
103-
dbx.request('path', {}, 'user, app', 'api', RPC)
104-
.catch((error) => {
105-
fail(error);
106-
});
107-
chai.assert.isTrue(rpcSpy.calledOnce);
108-
chai.assert.equal('path', dbx.rpcRequest.getCall(0).args[0]);
109-
chai.assert.deepEqual({}, dbx.rpcRequest.getCall(0).args[1]);
110-
chai.assert.equal(USER_AUTH, dbx.rpcRequest.getCall(0).args[2]);
111-
});
112-
113-
it('completes a multiauth RPC request with team auth when supplied with an accessToken', () => {
114-
const dbxAuth = new DropboxAuth({ accessToken: 'foo' });
115-
const dbx = new Dropbox({ auth: dbxAuth });
116-
const rpcSpy = sinon.spy(dbx, 'rpcRequest');
117-
dbx.request('path', {}, 'team, app', 'api', RPC)
118-
.catch((error) => {
119-
fail(error);
120-
});
121-
chai.assert.isTrue(rpcSpy.calledOnce);
122-
chai.assert.equal('path', dbx.rpcRequest.getCall(0).args[0]);
123-
chai.assert.deepEqual({}, dbx.rpcRequest.getCall(0).args[1]);
124-
chai.assert.equal(TEAM_AUTH, dbx.rpcRequest.getCall(0).args[2]);
125-
});
126-
127-
it('completes a multiauth RPC request with app auth when not supplied with an accessToken', () => {
128-
const dbxAuth = new DropboxAuth({
129-
clientID: 'foo',
130-
clientSecret: 'bar',
131-
});
132-
const dbx = new Dropbox({ auth: dbxAuth });
133-
const rpcSpy = sinon.spy(dbx, 'rpcRequest');
134-
dbx.request('path', {}, 'user, app', 'api', RPC)
135-
.catch((error) => {
136-
fail(error);
137-
});
138-
chai.assert.isTrue(rpcSpy.calledOnce);
139-
chai.assert.equal('path', dbx.rpcRequest.getCall(0).args[0]);
140-
chai.assert.deepEqual({}, dbx.rpcRequest.getCall(0).args[1]);
141-
chai.assert.equal(APP_AUTH, dbx.rpcRequest.getCall(0).args[2]);
142-
});
143-
14499
it('completes a cookie auth RPC request', () => {
145100
const dbxAuth = new DropboxAuth();
146101
const dbx = new Dropbox({ auth: dbxAuth });
@@ -248,4 +203,52 @@ describe('Dropbox', () => {
248203
chai.assert.equal(headers.cookie, 'hash');
249204
});
250205
});
206+
207+
describe('setAuthHeaders', () => {
208+
const authTypes = ['user', 'app', 'team', 'noauth', 'user, app', 'team, app', 'cookie'];
209+
for (const auth of authTypes) {
210+
for (const hasAccessToken of [true, false]) {
211+
for (const hasAppKeys of [true, false]) {
212+
it(`correctly sets auth headers given '${auth}' auth and ${hasAccessToken ? 'has' : 'does not have'} an access token`, () => {
213+
const dbx = new Dropbox({
214+
accessToken: hasAccessToken ? 'token' : undefined,
215+
clientId: hasAppKeys ? 'app_key' : undefined,
216+
clientSecret: hasAppKeys ? 'app_secret' : undefined,
217+
});
218+
219+
const fetchOptions = {
220+
headers: {},
221+
};
222+
223+
const isExpectedToHaveTokenHeader = hasAccessToken && (auth.includes('user') || auth.includes('team'));
224+
const isExpectedToHaveAppHeader = ((auth === 'app') || (auth.includes('app') && !hasAccessToken)) && hasAppKeys;
225+
226+
dbx.setAuthHeaders(auth, fetchOptions);
227+
228+
const { headers } = fetchOptions;
229+
if (isExpectedToHaveAppHeader) {
230+
chai.assert.isTrue(headers.Authorization.includes('Basic'));
231+
} else if (isExpectedToHaveTokenHeader) {
232+
chai.assert.isTrue(headers.Authorization.includes('Bearer'));
233+
} else {
234+
chai.assert.deepEqual(headers, {});
235+
}
236+
});
237+
}
238+
}
239+
}
240+
241+
it('throws an error on an invalid auth type', () => {
242+
const dbx = new Dropbox();
243+
244+
const fetchOptions = {
245+
headers: {},
246+
};
247+
248+
chai.assert.throws(
249+
Dropbox.prototype.setAuthHeaders.bind(Dropbox, 'bad auth type', fetchOptions),
250+
Error,
251+
);
252+
});
253+
});
251254
});

0 commit comments

Comments
 (0)