Skip to content

Commit 313fb23

Browse files
author
Eric Koleda
committed
Switch to witLowerCaseKeys_ method, and other small fixes.
1 parent daa1258 commit 313fb23

File tree

3 files changed

+57
-58
lines changed

3 files changed

+57
-58
lines changed

src/Service.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ Service_.prototype.lockable_ = function(func) {
699699
/**
700700
* Obtain an access token using the custom grant type specified. Most often
701701
* this will be "client_credentials", and a client ID and secret are set an
702-
* "Authorization: Baseic ..." header will be added using thos values.
702+
* "Authorization: Basic ..." header will be added using those values.
703703
*/
704704
Service_.prototype.exchangeGrant_ = function() {
705705
validate_({
@@ -711,15 +711,16 @@ Service_.prototype.exchangeGrant_ = function() {
711711
};
712712
payload = extend_(payload, this.params_);
713713

714-
// For the client_credentials grant type, add a basic authorization header
715-
// if the client ID and client secret are set and no authorization header has
716-
// been set yet (AKA do the expected thing).
714+
// For the client_credentials grant type, add a basic authorization header:
715+
// - If the client ID and client secret are set.
716+
// - No authorization header has been set yet.
717+
var lowerCaseHeaders = witLowerCaseKeys_(this.tokenHeaders_);
717718
if (this.grantType_ === 'client_credentials' &&
718719
this.clientId_ &&
719720
this.clientSecret_ &&
720-
!getValueCaseInsensitive_(this.tokenHeaders_, 'Authorization')) {
721+
(!lowerCaseHeaders || !lowerCaseHeaders.authorization)) {
721722
this.tokenHeaders_ = this.tokenHeaders_ || {};
722-
this.tokenHeaders_.Authorization = 'Basic ' +
723+
this.tokenHeaders_.authorization = 'Basic ' +
723724
Utilities.base64Encode(this.clientId_ + ':' + this.clientSecret_);
724725
}
725726

src/Utilities.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,21 @@ function extend_(destination, source) {
7777
return destination;
7878
}
7979

80-
/* exported getValueCaseInsensitive_ */
80+
/* exported witLowerCaseKeys_ */
8181
/**
82-
* Gets the value stored in the object under the given key, in a
83-
* case-insensitive way.
84-
* @param {Object} obj The object to search in.
85-
* @param {string} key The key to search for.
86-
* @return {Object} the value under that key, or undefined otherwise
82+
* Gets a copy of an object with all the keys converted to lower-case strings.
83+
*
84+
* @param {Object} obj The object to copy.
85+
* @return {Object} a shallow copy of the object with all lower-case keys.
8786
*/
88-
function getValueCaseInsensitive_(obj, key) {
89-
if (obj == null || typeof obj !== 'object' ||
90-
key == null || !key.toString) {
91-
return undefined;
92-
}
93-
if (key in obj) {
94-
return obj[key];
87+
function witLowerCaseKeys_(obj) {
88+
if (obj === null || typeof obj !== 'object') {
89+
return obj;
9590
}
91+
// For each key in the source object, add a lower-case version to a new
92+
// object, and return it.
9693
return Object.keys(obj).reduce(function(result, k) {
97-
if (result) return result;
98-
if (k.toLowerCase() === key.toLowerCase()) {
99-
return obj[k];
100-
}
101-
}, undefined);
94+
result[k.toLowerCase()] = obj[k];
95+
return result;
96+
}, {});
10297
}

test/test.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,12 @@ describe('Service', function() {
243243
});
244244

245245
describe('#exchangeGrant_()', function() {
246-
var getValueCaseInsensitive_ = OAuth2.getValueCaseInsensitive_;
246+
var witLowerCaseKeys_ = OAuth2.witLowerCaseKeys_;
247247

248248
it('should not set auth header if the grant type is not client_credentials',
249249
function(done) {
250250
mocks.UrlFetchApp.resultFunction = function(url, urlOptions) {
251-
assert.isUndefined(
252-
getValueCaseInsensitive_(urlOptions.headers, 'Authorization'));
251+
assert.isUndefined(witLowerCaseKeys_(urlOptions.headers).authorization);
253252
done();
254253
};
255254
var service = OAuth2.createService('test')
@@ -261,8 +260,7 @@ describe('Service', function() {
261260
it('should not set auth header if the client ID is not set',
262261
function(done) {
263262
mocks.UrlFetchApp.resultFunction = function(url, urlOptions) {
264-
assert.isUndefined(
265-
getValueCaseInsensitive_(urlOptions.headers, 'Authorization'));
263+
assert.isUndefined(witLowerCaseKeys_(urlOptions.headers).authorization);
266264
done();
267265
};
268266
var service = OAuth2.createService('test')
@@ -274,8 +272,7 @@ describe('Service', function() {
274272
it('should not set auth header if the client secret is not set',
275273
function(done) {
276274
mocks.UrlFetchApp.resultFunction = function(url, urlOptions) {
277-
assert.isUndefined(
278-
getValueCaseInsensitive_(urlOptions.headers, 'Authorization'));
275+
assert.isUndefined(witLowerCaseKeys_(urlOptions.headers).authorization);
279276
done();
280277
};
281278
var service = OAuth2.createService('test')
@@ -288,7 +285,8 @@ describe('Service', function() {
288285
it('should not set auth header if it is already set',
289286
function(done) {
290287
mocks.UrlFetchApp.resultFunction = function(url, urlOptions) {
291-
assert.equal(urlOptions.headers.Authorization, 'something');
288+
assert.equal(witLowerCaseKeys_(urlOptions.headers).authorization,
289+
'something');
292290
done();
293291
};
294292
var service = OAuth2.createService('test')
@@ -297,7 +295,7 @@ describe('Service', function() {
297295
.setClientId('abc')
298296
.setClientSecret('def')
299297
.setTokenHeaders({
300-
Authorization: 'something'
298+
authorization: 'something'
301299
});
302300
service.exchangeGrant_();
303301
});
@@ -306,7 +304,8 @@ describe('Service', function() {
306304
'the client ID and client secret are set and the authorization header' +
307305
'is not already set', function(done) {
308306
mocks.UrlFetchApp.resultFunction = function(url, urlOptions) {
309-
assert.equal(urlOptions.headers.Authorization, 'Basic YWJjOmRlZg==');
307+
assert.equal(witLowerCaseKeys_(urlOptions.headers).authorization,
308+
'Basic YWJjOmRlZg==');
310309
done();
311310
};
312311
var service = OAuth2.createService('test')
@@ -337,34 +336,38 @@ describe('Utilities', function() {
337336
});
338337
});
339338

340-
describe('#getValueCaseInsensitive_()', function() {
341-
var getValueCaseInsensitive_ = OAuth2.getValueCaseInsensitive_;
342-
343-
it('should find identical keys', function() {
344-
assert.isTrue(getValueCaseInsensitive_({'a': true}, 'a'));
345-
assert.isTrue(getValueCaseInsensitive_({'A': true}, 'A'));
346-
assert.isTrue(getValueCaseInsensitive_({'Ab': true}, 'Ab'));
347-
});
348-
349-
it('should find matching keys of different cases', function() {
350-
assert.isTrue(getValueCaseInsensitive_({'a': true}, 'A'));
351-
assert.isTrue(getValueCaseInsensitive_({'A': true}, 'a'));
352-
assert.isTrue(getValueCaseInsensitive_({'Ab': true}, 'aB'));
353-
assert.isTrue(getValueCaseInsensitive_({'a2': true}, 'A2'));
339+
describe('#witLowerCaseKeys_()', function() {
340+
var witLowerCaseKeys_ = OAuth2.witLowerCaseKeys_;
341+
var data = {
342+
'a': true,
343+
'A': true,
344+
'B': true,
345+
'Cc': true,
346+
'D2': true,
347+
'E!@#': true
348+
};
349+
var lowerCaseData = witLowerCaseKeys_(data);
350+
351+
it('should contain lower-case keys', function() {
352+
assert.isTrue(lowerCaseData['a']);
353+
assert.isTrue(lowerCaseData['b']);
354+
assert.isTrue(lowerCaseData['cc']);
355+
assert.isTrue(lowerCaseData['d2']);
356+
assert.isTrue(lowerCaseData['e!@#']);
354357
});
355358

356-
it('should work with non-alphabetic keys', function() {
357-
assert.isTrue(getValueCaseInsensitive_({'A2': true}, 'a2'));
358-
assert.isTrue(getValueCaseInsensitive_({'2': true}, '2'));
359-
assert.isTrue(getValueCaseInsensitive_({2: true}, 2));
360-
assert.isTrue(getValueCaseInsensitive_({'!@#': true}, '!@#'));
359+
it('should not contain upper-case keys', function() {
360+
assert.isUndefined(lowerCaseData['A']);
361+
assert.isUndefined(lowerCaseData['B']);
362+
assert.isUndefined(lowerCaseData['Cc']);
363+
assert.isUndefined(lowerCaseData['D2']);
364+
assert.isUndefined(lowerCaseData['E!@#']);
361365
});
362366

363-
it('should work null and undefined', function() {
364-
assert.isUndefined(getValueCaseInsensitive_(null, 'key'));
365-
assert.isUndefined(getValueCaseInsensitive_(undefined, 'key'));
366-
assert.isUndefined(getValueCaseInsensitive_({'a': true}, null));
367-
assert.isUndefined(getValueCaseInsensitive_({'a': true}, undefined));
367+
it('should handle null, undefined, and empty objects', function() {
368+
assert.isNull(witLowerCaseKeys_(null));
369+
assert.isUndefined(witLowerCaseKeys_(undefined));
370+
assert.isEmpty(witLowerCaseKeys_({}));
368371
});
369372
});
370373
});

0 commit comments

Comments
 (0)