Support multiple keys with simpler constructor for \Drupal\jwt\Transcoder\JwtTranscoder

Created on 1 November 2017, over 6 years ago
Updated 17 March 2023, over 1 year ago

Problem/Motivation

The constructor for \Drupal\jwt\Transcoder\JwtTranscoder hard-codes the config name and makes it more difficult than necessary to instantiate this class for use with different keys.

Also, class \Drupal\jwt\Transcoder\JwtTranscoder is also not really ideal for use as a service since it's mutable via methods like setSecret()

Proposed resolution

It seems you could have a much simplified constructor.

For example:

  public function __construct(KeyInterface $key) {
    $this->transcoder = new JWT();
    $keyConfig = $key->getPlugin('key_type')->getConfiguration();
    $algorithm = $keyConfig['algorithm'];
    $this->setAlgorithm($algorithm);

    $key_value = $key->getKeyValue();
    if (!empty($key_value)) {
      if ($this->algorithmType == 'jwt_hs') {
        // Symmetric algorithm so we set the secret.
        $this->setSecret($key_value);
      }
      elseif ($this->algorithmType == 'jwt_rs') {
        // Asymmetric algorithm so we set the private key.
        $this->setPrivateKey($key_value);
      }
    }
  }

The JWT class from the library only has static methods - I'm not sure why you are even instantiating it or providing it as a service?

Possibly there could be a new base class with a simple constructor and the existing class changed to a subclass?

Remaining tasks

Decide on what's the right approach

User interface changes

n/a

API changes

n/a

✨ Feature request
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States pwolanin

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.

Production build 0.69.0 2024