public function testSign() { $expectedSignature = 'foobar'; // Configure payload $headerParameters = $this->getMockBuilder('Emarref\\Jwt\\Token\\PropertyList')->getMock(); $headerParameters->expects($this->once())->method('jsonSerialize'); $this->encoder->expects($this->at(0))->method('encode'); $header = $this->getMockBuilder('Emarref\\Jwt\\Token\\Header')->getMock(); $header->expects($this->once())->method('getParameters')->will($this->returnValue($headerParameters)); // Configure payload $claims = $this->getMockBuilder('Emarref\\Jwt\\Token\\PropertyList')->getMock(); $claims->expects($this->once())->method('jsonSerialize'); $payload = $this->getMockBuilder('Emarref\\Jwt\\Token\\Payload')->getMock(); $payload->expects($this->once())->method('getClaims')->will($this->returnValue($claims)); $this->encoder->expects($this->at(1))->method('encode'); // Configure token $token = $this->getMockBuilder('Emarref\\Jwt\\Token')->getMock(); $token->expects($this->once())->method('getHeader')->will($this->returnValue($header)); $token->expects($this->once())->method('getPayload')->will($this->returnValue($payload)); $this->encryption->expects($this->once())->method('getAlgorithmName')->will($this->returnValue('alg')); $this->encryption->expects($this->once())->method('encrypt')->will($this->returnValue($expectedSignature)); $token->expects($this->once())->method('addHeader')->with(new Algorithm('alg')); $token->expects($this->once())->method('setSignature')->with($expectedSignature); $this->signer->sign($token); }
/** * @param Token $token * @throws InvalidSignatureException */ public function verify(Token $token) { /** @var HeaderParameter\Algorithm $algorithmParameter */ $algorithmParameter = $token->getHeader()->findParameterByName(HeaderParameter\Algorithm::NAME); if (null === $algorithmParameter) { throw new \RuntimeException('Algorithm parameter not found in token header.'); } if ($algorithmParameter->getValue() !== $this->encryption->getAlgorithmName()) { throw new \RuntimeException(sprintf('Cannot use "%s" algorithm to decrypt token encrypted with algorithm "%s".', $this->encryption->getAlgorithmName(), $algorithmParameter->getValue())); } if (!$this->encryption->verify($this->signer->getUnsignedValue($token), $token->getSignature())) { throw new InvalidSignatureException(); } }
/** * @param Token $token * @param Encryption\EncryptionInterface $encryption */ public function sign(Token $token, Encryption\EncryptionInterface $encryption) { $signer = new Signature\Jws($encryption, $this->encoder); $signer->sign($token); }