Skip to content

Commit

Permalink
Merge pull request #163 from lcobucci/fix-rsa-failures
Browse files Browse the repository at this point in the history
Fix RSA failures and add tests to it
  • Loading branch information
lcobucci authored Feb 12, 2017
2 parents 925435c + 20a1d39 commit 43f30bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Signer/Rsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ public function sign(string $payload, Key $key): string
$this->validateKey($key);

$signature = '';
openssl_sign($payload, $signature, $key, $this->getAlgorithm());

if (!openssl_sign($payload, $signature, $key, $this->getAlgorithm())) {
throw new InvalidArgumentException(
'There was an error while creating the signature: ' . openssl_error_string()
);
}

return $signature;
}
Expand Down
26 changes: 26 additions & 0 deletions test/unit/Signer/RsaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ public function signShouldReturnAValidOpensslSignature(): void
self::assertSame(1, openssl_verify($payload, $signature, $publicKey, OPENSSL_ALGO_SHA256));
}

/**
* @test
*
* @requires extension openssl
*
* @expectedException \InvalidArgumentException
*
* @covers \Lcobucci\JWT\Signer\Rsa::sign
* @covers \Lcobucci\JWT\Signer\Rsa::validateKey
*
* @uses \Lcobucci\JWT\Signer\Key
*/
public function signShouldRaiseAnExceptionWhenKeyIsInvalid(): void
{
$key = <<<KEY
-----BEGIN RSA PRIVATE KEY-----
MGECAQACEQC4MRKSVsq5XnRBrJoX6+rnAgMBAAECECO8SZkgw6Yg66A6SUly/3kC
CQDtPXZtCQWJuwIJAMbBu17GDOrFAggopfhNlFcjkwIIVjb7G+U0/TECCEERyvxP
TWdN
-----END RSA PRIVATE KEY-----
KEY;

$signer = $this->getSigner();
$signer->sign('testing', new Key($key));
}

/**
* @test
*
Expand Down

0 comments on commit 43f30bc

Please sign in to comment.