When an authentication provider authenticates via provider form, the form submit handlers take care of two things:
1) fetching a valid auth (and refresh) token from Salesforce
2) using that token to fetch a Salesforce identity, which defines the API endpoints used to access various APIs used by this module.

This works fine when authenticating on the auth provider form.
But, when you need to revoke or reset a token (e.g. after syncing environments from prod to sandbox), the identity is never reset.

Auth providers should refresh identity every time an access token is refreshed or reset, not just in the form submit handlers.

Issue fork salesforce-3102133

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

darksnow created an issue. See original summary.

darksnow’s picture

StatusFileSize
new3.38 KB

This patch adds three commands while allow the user to list, revoke and crucially, refresh authentication tokens from the command line.

In my use case I've edited the Salesforce YAML files to remove any authentication credentials and then changes the settings file to set this config entries from environment variables. This leaves the settings form in an invalid state so I can't use the UI to get a token since form validation fails.

The revoke and refresh commands themselves can take the machine name of the authentication provider as an argument. If no argument is given they both find the default provider and act on that.

darksnow’s picture

Title: Drush command to revoke and refresh aithentication token » Drush command to revoke and refresh authentication token
aaronbauman’s picture

Interesting idea, thank you for the patch.

Fetching the auth provider is a tad overcomplicated in both listAuthProviders and refreshToken:

+    foreach($this->authMan->getProviders() as $provider) {
+      if ($providerName == $provider->id()) {
+        $auth = $provider->getPlugin();

In each case, simplify this with:

if ($provider = SalesforceAuthConfig::load($providerName)) {
  $auth = $provider->getPlugin();

While I appreciate these additional commands, how do these help address your original concern?
Are you envisioning additional commands to perform the initial oauth handshake as well?

aaronbauman’s picture

Status: Active » Needs work

Changed to "needs work" to address feedback in prior comment.

darksnow’s picture

Status: Needs work » Needs review
StatusFileSize
new3.64 KB

Thanks, that's much neater :)

  • AaronBauman committed 32541d0 on 8.x-4.x authored by darksnow
    Issue #3102133 by darksnow: Drush command to revoke and refresh...
aaronbauman’s picture

Status: Needs review » Active

This is in.

Resetting to "active", since these commands don't actually provide an alternative to entering credentials via admin page.

darksnow’s picture

Thanks Aaron.

Now you've set this back to active, what more would you like me to add?

The original idea was that you can now add credentials to your settings.php file and this command would then pick them up, this seems like an alternative to the settings form.

aaronbauman’s picture

This doesn't solve the problem of initial authentication - you still need to enter your creds to the settings form tologin to salesforce, complete the OAuth handshake, and generate the initial access and refresh tokens.

Unless you have another way to get those?

darksnow’s picture

OK. I see what you mean now and you're partially correct.

This code was added to allow for an automated deployment without including any private data in any exported code. So, on my dev machine I did what you described and added my full details to the form and got the token. This was just to allow me to then export the configuration to YAML. I edited the YAML files to remove the private data, so my consumer key in the YAML file is dummy_key for example.

I then reimported the config. Convoluted perhaps but it allows me to protect the key, user and URL by removing them from the config before I commit to git. This of course now means the form will not submit since the data in there is invalid.

But, I then add the following to settings.php

$config['salesforce.salesforce_auth.salesforce_auth']['provider_settings']['consumer_key'] = getenv('SALESFORCE_KEY');
$config['salesforce.salesforce_auth.salesforce_auth']['provider_settings']['login_user'] = getenv('SALESFORCE_LOGIN');
$config['salesforce.salesforce_auth.salesforce_auth']['provider_settings']['login_url'] = getenv('SALESFORCE_URL');

I could also replace the PEM file location, for example, depending on my environment.

Now, having done that, the form will still not work as it shows the contents of the config files, not the running settings. It's not affected by the $config changes above. The new command however, can get at these new config details and will run correctly.

So, in this way I can add dummy, invalid data to the YAML files and safely commit them to git, then have my new command pick up the correct config from the running environment and allow a deployment script to get the required access token securely.

It is a little convoluted since the token is generated in form validation, so I can't add incorrect keys to the form but I protecting those keys is exactly why I created this command.

I hope that clarifies my use case.

Ali_KC’s picture

Hello,

That's still an issue. The Salesforce(SF) Identity wont change after overriding it the settings.php file which result an issue with SF connection. I am using JWT too.

aaronbauman’s picture

Version: 8.x-4.x-dev » 5.0.x-dev
Category: Feature request » Bug report

I have run into this recently as well.
The identity is only refreshed when re-saving the Auth Config form.
This is a problem when revoking an access token via drush or otherwise.

Identity should be refreshed whenever access token is refreshed or regenerated.
This issue persists into 5.0.x

bob.hinrichs’s picture

have encountered this bug, and am following, thank you much

aaronbauman’s picture

Issue summary: View changes

Updated IS

aaronbauman’s picture

Title: Drush command to revoke and refresh authentication token » Refresh identity whenever token is refreshed

  • AaronBauman committed bfabc33 on 5.0.x
    Issue #3102133 by AaronBauman: Refresh identity whenever token is...

aaronbauman’s picture

Version: 5.0.x-dev » 8.x-4.x-dev
Status: Active » Needs review
Issue tags: -Salesforce, -Drush-9
dswier’s picture

Thanks for the patch. I tried it on 8.x-4.x-dev, and it works for me. With the patch, I am seeing the identity get refreshed whenever the token is refreshed.

  • AaronBauman committed bdf17e5 on 8.x-4.x
    Issue #3102133 by AaronBauman: Refresh identity whenever token is...
  • AaronBauman committed c846d10 on 8.x-4.x authored by darksnow
    Issue #3102133 by AaronBauman, darksnow: Refresh identity whenever token...
aaronbauman’s picture

Version: 8.x-4.x-dev » 5.0.x-dev
Status: Needs review » Fixed

Committed, thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.