Skip to content

Improve error message when incorrect passphrase is provided for priva… - #1317

Open
mikoajp wants to merge 1 commit into
lexik:3.xfrom
mikoajp:fix/improve-passphrase-error-message-v2
Open

mikoajp wants to merge 1 commit into
lexik:3.xfrom
mikoajp:fix/improve-passphrase-error-message-v2

Conversation

@mikoajp

@mikoajp mikoajp commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Problem

When an incorrect passphrase is provided for the private key, the error message is unclear:

"An error occurred while trying to encode the JWT token. Please verify your configuration (private key/passphrase)"

The root cause is not obvious — lcobucci throws a RuntimeException with raw OpenSSL internals like bad decrypt which don't help the developer identify the passphrase as the issue.

Fixes #1262

Solution

Wrap the signing operation in LcobucciJWSProvider::getSignedToken() in a try/catch. When lcobucci throws during InMemory::plainText() or getToken(), rethrow as \InvalidArgumentException with a clear message pointing to the passphrase.

The loadKey() call is intentionally left outside the try/catch so key-loading errors (wrong file, unsupported format) propagate with their original messages unchanged.

Changes

  • Services/JWSProvider/LcobucciJWSProvider.php — wrap signing in try/catch
  • Tests/Services/JWSProvider/LcobucciJWSProviderTest.php — add test for invalid passphrase scenario

…te key

When an incorrect passphrase is provided, lcobucci throws a generic
RuntimeException with OpenSSL internals. Wrap the signing operation
in a try/catch to rethrow with a clear, actionable message.

The loadKey() call is intentionally left outside the try/catch so
key-loading errors propagate unchanged.

Fixes lexik#1262
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.

Improve Error Message When Incorrect Passphrase is Provided for Private Key

1 participant