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

OAuth2 support in salesforce #470

Merged
merged 11 commits into from
Jan 30, 2024
Merged

Conversation

mtuchi
Copy link
Collaborator

@mtuchi mtuchi commented Jan 25, 2024

Summary

Add support for OAuth2 in salesforce adaptor

Details

Improved the createConnection function to handle both accessToken connection and Basic Auth connection. I have created two functions [createBasicAuthConnection and createAccessTokenConnection] that gets called in createConnection depending on configuration-schema. I have also created another configuration-schema for OAuth which requires other_params.instance_url and access_token.

Also in configuration-schema I have removed the securityToken from required property since it's an optional value

Issues #410 #423

Review Checklist

Before merging, the reviewer should check the following items:

  • Does the PR do what it claims to do?
  • Are there any unit tests? Should there be?
  • Is there a changeset associated with this PR? Should there be? Note that
    dev only changes don't need a changeset.

@mtuchi mtuchi changed the title Add oauth support in salesforce Add oauth2 support in salesforce Jan 26, 2024
@mtuchi mtuchi changed the title Add oauth2 support in salesforce OAuth2 support in salesforce Jan 26, 2024
@mtuchi mtuchi requested a review from josephjclark January 26, 2024 12:21
@mtuchi mtuchi marked this pull request as ready for review January 26, 2024 12:21
@@ -55,7 +55,6 @@
"required": [
"loginUrl",
"username",
"password",
"securityToken"
Copy link
Member

Choose a reason for hiding this comment

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

@mtuchi , is securityToken not required anymore?

Copy link
Collaborator Author

@mtuchi mtuchi Jan 26, 2024

Choose a reason for hiding this comment

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

@taylordowns2000 according to jsforce, securityToken is optional - https://jsforce.github.io/document/#username-and-password-login

Screenshot 2024-01-26 at 3 58 17 PM

Copy link
Member

Choose a reason for hiding this comment

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

ah wow. @aleksa-krolls , can you confirm that we don't need users to provide their security token when creating a new Salesforce credential? (is this an admin setting in a client's salesforce system? something like, "require security token for API access"?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think most salesforce instance are configured to require securityToken but if a client have a salesforce instance that does not require securityToken then the job should pass.

The change i introduced also helps with credential creation on lighting which make the Security Token input optional

function createConnection(state) {
const { loginUrl, apiVersion } = state.configuration;
async function createBasicAuthConnection(state) {
console.log('Attempting to connect with Basic Auth');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we log anywhere when the connection is successful?

I would much prefer to say:

Connected to salesforce as ${user}

Or

Failed to connected to salesforce as ${user}

Ie, print the result, not the process.

When we say "Attempting to..." it sort of shows a lack of confidence on our part, and when there's no log to follow it up it's gonna look pretty flaky.

For something routine, like logging in, we should be super confident in our logging. And when something goes wrong we should be super clear.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree on with the Connected to salesforce as ${username} but on Failure i don't think we should log Failed to connect as ${username} because this suggest that the username is wrong while it could be a number of reasons.

I would suggest we leave the error message that comes from salesforce which looks like this

INVALID_LOGIN: Invalid username, password, security token; or user locked out.

cc @josephjclark @taylordowns2000

Copy link
Member

@taylordowns2000 taylordowns2000 Jan 26, 2024

Choose a reason for hiding this comment

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

@mtuchi , we added the username there (if i'm remembering this correctly) because it's incredibly helpful for the openfn end-user (the salesforce admin) to know which SF user they have to debug! users do get locked out, tokens become invalid, etc., etc.

printing the username is great for the salesforce admin. i don't think it will make them think that the username is wrong, i think it will help them figure out whether the password, token, or user status in salesforce is wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think that error implies that username is the fault. I agree with taylor that it's a useful bit of information to report.

I also agree that the original salesforce message is fine.

We can do both!

console.error(`Failed to connected to salesforce as ${user}`)
console.error(e.message)  // or whatever

Actually, if we we throw the error, I think the runtime should report it (and if it doesn't, that's something I need to fix!)

console.error(`Failed to connected to salesforce as ${user}`)
throw e

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay sounds good 👍🏽 , I will use this 👇🏽

console.error(`Failed to connected to salesforce as ${user}`)
throw e

Copy link
Member

Choose a reason for hiding this comment

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

hrmmm, i think we're not supposed to use console.error, right? isn't it a different stream and won't it cause timing/printout issues?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@taylordowns2000 I was going to say: console.errorshould be fine - we redirect it to stdout anyway.

... but then I remembered that this doesn't use our logger, so yes it will go to a different stream and cause timing problems.

I don't think that's the fault of the adaptor though. Let me just dig into something here.

@mtuchi mtuchi requested a review from josephjclark January 26, 2024 17:03
accessToken: access_token,
});

const connection = new jsforce.Connection(connectionOptions);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we not need to do any sort of login here?

The connection is synchronous, how do we know we're actually connected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No login there, It's a different strategy based on auth type,
So for basic-auth you will setup the connection then do connection.login(connectionOptions). And for accessToken auth, getConnection(state, connOpts) will have the connection with session type oauth

For Basic Auth - https://jsforce.github.io/document/#username-and-password-login
For OAuth - https://jsforce.github.io/document/#access-token

@mtuchi mtuchi requested a review from josephjclark January 30, 2024 13:08
Copy link
Collaborator

@josephjclark josephjclark left a comment

Choose a reason for hiding this comment

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

Approved subject to something in console.error that I just want to look into.

@josephjclark josephjclark changed the base branch from main to release/salesforce January 30, 2024 17:44
@josephjclark
Copy link
Collaborator

I don't have a quick solution to the console.error thing. For the moment I've just renamed it to console.log.

@josephjclark josephjclark merged commit d30a0f7 into release/salesforce Jan 30, 2024
1 check passed
@josephjclark josephjclark deleted the 410-sf-add-oauth branch January 30, 2024 17:45
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.

3 participants