Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/197 access token provided #273

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

fazbyte
Copy link

@fazbyte fazbyte commented Sep 11, 2023

PR details

What is the purpose of this pull request? (put an "X" next to an item)

  • Fix
  • Improvements
  • New Feature
  • Documentation updates
  • Other, please explain:

What changes did you make? (Give an overview)

Allow for passing an existing access token, for example in web app context

lib/auth.js Outdated
if (!authObject) {
throw new Error('authObject are required. see readme.');
} else if (grant_type && grant_type == GRANT_TYPE_AUTHORIZATION_CODE) {
Copy link
Owner

Choose a reason for hiding this comment

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

one thought here is that, since you are providing grant_type as a default, the additional check here is not needed since it will always have one. I would also prefer that since you only check for the error here, that this be included in the else if clause, rather than opening a new block to avoid nested if/else

Copy link
Author

@fazbyte fazbyte Sep 18, 2023

Choose a reason for hiding this comment

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

Thanks for the comment.
I have removed the null check for the grant_type.
If I change the condition without nested if like the following
else if (grant_type == GRANT_TYPE_AUTHORIZATION_CODE && !authObject.access_token)

then the validation would fail asking for the client_id to be present if grant_type=authorization_code and access_token is provided.

Otherwise I have to add an exclusion of the grant_type!=authorization_code in the other conditions.

lib/auth.js Outdated
) {
if (this.options?.eventHandlers?.onConnectionError) {
this.options.eventHandlers.onConnectionError(error, remainingAttempts);
if (!(this.grant_type && this.grant_type === GRANT_TYPE_AUTHORIZATION_CODE)) {
Copy link
Owner

Choose a reason for hiding this comment

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

I have a preference for "positive" checks, since they are easier to read. in this case, we could check if it is a specific grant type (without null check since we set default) and if so, return early before moving to default logic

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the comment. Fixed it.

lib/auth.js Outdated
* @param {string[]} [authObject.scope] Array of scopes used for requests
* @param {object} options options for the SDK as a whole, for example collection of handler functions, or retry settings
*/
constructor(authObject, options) {
constructor(authObject, options, grant_type = GRANT_TYPE_CLIENT_CREDENTIALS) {
Copy link
Owner

Choose a reason for hiding this comment

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

Hey @fazbyte in docs above you say grant type is on authObject, but you pass as a param? I think the authObject object is correct for this value to be honest. you can always set the default in the first line in case missing or use object.assign to set defaults and overwrite with provided values in authObject

Copy link
Author

Choose a reason for hiding this comment

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

@DougMidgley , grant_type is not part of authObject. When the user logs in with the custom app, we then call the v2/token endpoint and after we get the response, we pass the response.data to the sfmc-sdk without changing it and pass the additional parameter grant_type='authorization_code'. This way it works with what you have already created and within the custom app where the user logs in. Let me know if this makes sense or what other change could be made.

Copy link
Author

Choose a reason for hiding this comment

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

@DougMidgley I have completely removed the grant_type parameter now. However if authObject doesn't contain access_token, then it will kickoff the validations for client_id/client_secret. Let me know your feedback

Copy link
Collaborator

Choose a reason for hiding this comment

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

thank you @fazbyte - knowing Doug's schedule, he will review this next week. Right, @DougMidgley ? ;-)

@JoernBerkefeld
Copy link
Collaborator

hey @DougMidgley & @fazbyte - is this moving PR anywhere?

lib/auth.js Outdated Show resolved Hide resolved
lib/auth.js Show resolved Hide resolved
lib/auth.js Outdated Show resolved Hide resolved
@DougMidgley
Copy link
Owner

@fazbyte - one of the test classes is failing - not sure of the cause, but maybe pulling from dev may fix it, since it doesnt look related to your code.
If this is for web/public app we could also look at just adding full support if needed. It might be tidier than the approach as it is today, but i leave to you.

@fazbyte
Copy link
Author

fazbyte commented Jun 21, 2024

@DougMidgley , I am a newbie to contributing to open source projects, so I don't understand fully on what you mean by pulling from dev. Do you want me to merge my code to the develop branch and push it again ? Or Since I have forked it, do I need to sync my fork ?

Could you explain a bit more on what do you mean by full support for web/public app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request to add support for Web and Public App Integrations with Authorization Code Grant Type
3 participants