Skip to content

Conversation

@erickoledadevrel
Copy link
Contributor

I added support for arbitrary grant types primarily to support the client_credentials grant type, but I didn't special-case it at the time. In practice developers expect that setting the client ID and secret will just work when using that grant type, but the values were being ignored.

Given that mostly all providers use these values in a predictable way I decided to added a default behavior that does what the developer expects, set an Authorazation: Basic ... header. Developers not wishing to use this default behavior can avoid it by not setting the client ID and secret or by manually adding an Authorization header of their own.

@erickoledadevrel erickoledadevrel requested a review from grant August 31, 2018 14:31
Copy link
Contributor

@grant grant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK. In some places, the logic could be simplified.

src/Service.js Outdated
* this will be "client_credentials", in which case make sure to also specify an
* Authorization header if required by your OAuth provider.
* this will be "client_credentials", and a client ID and secret are set an
* "Authorization: Baseic ..." header will be added using thos values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: thos

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Service.js Outdated
};
payload = extend_(payload, this.params_);

// For the client_credentials grant type, add a basic authorization header
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest making this comment more readable:

// For the client_credentials grant type, add a basic authorization header:
// - If the client ID and client secret are set
// - No authorization header has been set yet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Utilities.js Outdated
* @return {Object} the value under that key, or undefined otherwise
*/
function getValueCaseInsensitive_(obj, key) {
if (obj == null || typeof obj !== 'object' ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • === instead of == always
  • Simply boolean logic.
    • if typeof obj !== 'object', then obj === null will always be true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Utilities.js Outdated
function getValueCaseInsensitive_(obj, key) {
if (obj == null || typeof obj !== 'object' ||
key == null || !key.toString) {
return undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined is returned by default in JavaScript.
Why don't you just return;?

Sample:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return#Description

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete.

if (key in obj) {
return obj[key];
}
return Object.keys(obj).reduce(function(result, k) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to what this semi-complicated block is doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Utilities.js Outdated
key == null || !key.toString) {
return undefined;
}
if (key in obj) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a shortcut for some cases.
It's not immediately obvious why we can return here, can you add a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete

src/Utilities.js Outdated
if (k.toLowerCase() === key.toLowerCase()) {
return obj[k];
}
}, undefined);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we could remove this , undefined) since it requires more advanced js (reduce) knowledge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete.

src/Utilities.js Outdated

/* exported getValueCaseInsensitive_ */
/**
* Gets the value stored in the object under the given key, in a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getValueCaseInsensitive_ seems like an obscure method.

Why don't we make a util method that converts an object to a lowercased-keyed object and then get that value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

@erickoledadevrel erickoledadevrel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes made, thanks for the review!

src/Service.js Outdated
* this will be "client_credentials", in which case make sure to also specify an
* Authorization header if required by your OAuth provider.
* this will be "client_credentials", and a client ID and secret are set an
* "Authorization: Baseic ..." header will be added using thos values.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Service.js Outdated
};
payload = extend_(payload, this.params_);

// For the client_credentials grant type, add a basic authorization header
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Utilities.js Outdated

/* exported getValueCaseInsensitive_ */
/**
* Gets the value stored in the object under the given key, in a
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Utilities.js Outdated
* @return {Object} the value under that key, or undefined otherwise
*/
function getValueCaseInsensitive_(obj, key) {
if (obj == null || typeof obj !== 'object' ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Utilities.js Outdated
function getValueCaseInsensitive_(obj, key) {
if (obj == null || typeof obj !== 'object' ||
key == null || !key.toString) {
return undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete.

src/Utilities.js Outdated
key == null || !key.toString) {
return undefined;
}
if (key in obj) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete

if (key in obj) {
return obj[key];
}
return Object.keys(obj).reduce(function(result, k) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/Utilities.js Outdated
if (k.toLowerCase() === key.toLowerCase()) {
return obj[k];
}
}, undefined);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete.

Copy link
Contributor

@grant grant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK after applying the fixes.

src/Utilities.js Outdated
}

/* exported getValueCaseInsensitive_ */
/* exported witLowerCaseKeys_ */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename witLowerCaseKeys_ to
toLowerCaseKeys_ or copyWithLowerCaseKeys_.

If you read this function aloud, it is not clear what the function does. By using "to lowercase keys", it's a bit clearer that like JS's toLowerCase, this function creates a copy of the parameter with lowercase keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, selected toLowerCaseKeys_

test/test.js Outdated
};
var lowerCaseData = witLowerCaseKeys_(data);

it('should contain lower-case keys', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, a stronger test would compare the exact input/output of values for the function rather than the presence and lack of presence of certain keys.

i.e. Combine these 2 tests. 'a' and 'A' could be one test like:

assert.deepEqual(lowerCaseData, {
  'a': true,
  'b': true,
  'cc': true,
  'd2': true,
  'e!@#': true,
});

This will assert everything else not in the object isUndefined too. You could also then inline lowerCaseData.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done!

@erickoledadevrel erickoledadevrel merged commit bcf34f8 into master Sep 11, 2018
@erickoledadevrel erickoledadevrel deleted the client_credentials branch September 11, 2018 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants