Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Services/JWSProvider/LcobucciJWSProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,19 @@ private function getSignerForAlgorithm($signatureAlgorithm): Signer

private function getSignedToken(Builder $jws): string
{
$key = InMemory::plainText($this->keyLoader->loadKey(KeyLoaderInterface::TYPE_PRIVATE), $this->signer instanceof Hmac ? '' : (string) $this->keyLoader->getPassphrase());

$token = $jws->getToken($this->signer, $key);
$passphrase = $this->signer instanceof Hmac ? '' : (string) $this->keyLoader->getPassphrase();
$privateKey = $this->keyLoader->loadKey(KeyLoaderInterface::TYPE_PRIVATE);

try {
$key = InMemory::plainText($privateKey, $passphrase);
$token = $jws->getToken($this->signer, $key);
} catch (\Throwable $e) {
throw new \InvalidArgumentException(
'Unable to sign the JWT token. Please verify that your passphrase is correct.',
0,
$e
);
}

return $token->toString();
}
Expand Down
24 changes: 23 additions & 1 deletion Tests/Services/JWSProvider/LcobucciJWSProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,26 @@ public function testUnSignedToken()

$create = $jwsProvider->create($payload);
}
}

public function testCreateWithInvalidPassphraseThrowsInvalidArgumentException()
{
$keyLoaderMock = $this->getKeyLoaderMock();
$keyLoaderMock
->expects($this->once())
->method('loadKey')
->with('private')
->willReturn(self::$privateKey);
$keyLoaderMock
->expects($this->once())
->method('getPassphrase')
->willReturn('wrong-passphrase');

$jwsProvider = new self::$providerClass($keyLoaderMock, 'RS256', 3600, 0);
$payload = ['username' => 'chalasr', 'iat' => time()];

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to sign the JWT token. Please verify that your passphrase is correct.');

$jwsProvider->create($payload);
}
}