Problem/Motivation
Been struggling all arvo with JWT authentication issues and I finally found the problem; a malformed key on our deployment environments. However the logic in the /user/login injection and /jwt/token led me up the garden path -- it specifically checks if \Drupal::service('jwt.authentication.jwt')->generateToken() returns FALSE. However, with the malformed key in play, this method was returning NULL.
The JwtAuth::generateToken method is documented to return string|bool, but it's not PHP typed to return anything. Let's fix that.
Steps to reproduce
- Set up JWT with JWT Auth Issuer
- Break your key somehow. Say, put a typo in it or leave off the
-----BEGIN PRIVATE KEY-----prefix/suffix - Try getting a JWT through /user/login, /jwt/token, or
\Drupal::service('jwt.authentication.jwt')->generateToken()
Proposed resolution
Ensure that JwtAuth::generateToken returns a strongly typed string|bool.
Remaining tasks
- Implement change
- Test?
User interface changes
None.
API changes
Ideally there shouldn't be any, but anything that relies on the return of JwtAuth::generateToken to not be a string or bool will break.
Data model changes
None.
Issue fork jwt-3557317
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
Comment #3
geoffreyr commentedComment #4
geoffreyr commentedComment #5
pwolanin commentedSince we are working on a new major version here, I wonder if it would be clearer/simple to return NULL on failure so the type hint can be
?string. This would be the simplest and would match the return value type hint on the underlying transcoder.Or would it even be better to throw an exception?
Comment #6
pwolanin commentedSwitch to ?string and simpler check for empty return. Needs a test
Comment #7
pwolanin commentedUsed claude to generate a Functional test:
Comment #8
pwolanin commentedComment #10
pwolanin commented