Allow for settings-based overrides

Created on 22 November 2018, almost 7 years ago
Updated 18 June 2025, 3 months ago

Consumers are content entities and that does make sense I think.

However, it's also a common use case I think that consumer data (specifically the password and redirect URL, which I think are actually provided by the simple_oauth module, so that's a bit tricky) need to vary per environment, e.g. you might be testing locally and currently, every time you sync the DB, you need to fix those values.

I'm not quite sure how this could look like, maybe UUID-based overrides in $settings?

$settings['consumers']['UUID-UUID-...']['password'] = 'foobar';

And then a specific API that fetches field values and somehow considers those overrides.

Thoughts?

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇨🇭Switzerland berdir Switzerland

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇭🇺Hungary czigor

    Just in case someone finds this useful. I put the client id and secret into config (since that's easy to override by environment) and used the following hook:

    /**
     * Implements hook_ENTITY_TYPE_load().
     */
    function mymodule_consumer_load($entities): void {
      /** @var \Drupal\consumers\Entity\ConsumerInterface $consumer */
      foreach ($entities as $consumer) {
        if ($consumer->uuid() !== SomeClass::UUID_CONSTANT) {
          continue;
        }
        
        $config = \Drupal::config('mymodule.settings');
        if ($config->get('oauth_client_id')) {
          $consumer->set('client_id', $config->get('oauth_client_id'));
        }
        if ($config->get('oauth_client_secret')) {
          $hash = \Drupal::service('password')->hash(trim($config->get('oauth_client_secret')));
          $consumer->set('secret', $hash);
        }
      }
    }
    
  • 🇬🇧United Kingdom jefflogan

    Unfortunately, I wasn't able to get the overriding of the consumer settings working from comment #8 for 2 reasons.

    Firstly when updating the 'secret', the BaseFieldDefinition provided by the simple_oauth is a password field type. Therefore the password hashing is handled on saving the consumer (ie by using $consumer->set('secret', 'plain_text_secret');).

    Secondly, the secret was updated on each entity load, meaning a new hash was generated... which caused my client_credentials authentication to fail.

    However, it steered me in the right direction. I created a drush command to implement the above, which I run whenever I copy 'Live' back to a 'Pre-production' site.

Production build 0.71.5 2024