public deserialize ( string $jwt ) : |
||
$jwt | string | |
Результат |
public function deserialize($jwt)
{
$token = new Token();
if (empty($jwt)) {
throw new \InvalidArgumentException('Not a valid JWT string passed for deserialization');
}
list($encodedHeader, $encodedPayload, $encodedSignature) = array_pad(explode('.', $jwt, 3), 3, null);
$decodedHeader = $this->encoding->decode($encodedHeader);
$decodedPayload = $this->encoding->decode($encodedPayload);
$decodedSignature = $this->encoding->decode($encodedSignature);
foreach ($this->parseHeaders($decodedHeader) as $header) {
$token->addHeader($header);
}
foreach ($this->parsePayload($decodedPayload) as $claim) {
$token->addClaim($claim);
}
$token->setSignature($decodedSignature);
return $token;
}
public function testDeserialize() { $jwt = 'a.b.c'; // Configure encoding $this->encoding->expects($this->at(0))->method('decode')->with('a')->will($this->returnValue('{"a":"1"}')); $this->encoding->expects($this->at(1))->method('decode')->with('b')->will($this->returnValue('{"b":"2"}')); $this->encoding->expects($this->at(2))->method('decode')->with('c')->will($this->returnValue('c')); // Configure headers $headerParameter = $this->getMockBuilder('Emarref\\Jwt\\HeaderParameter\\Custom')->getMock(); $headerParameter->expects($this->once())->method('setValue')->with('1'); $headerParameter->expects($this->once())->method('getValue')->will($this->returnValue('1')); $headerParameter->expects($this->exactly(2))->method('getName')->will($this->returnValue('a')); $this->headerParameterFactory->expects($this->once())->method('get')->with('a')->will($this->returnValue($headerParameter)); // Configure claims $claim = $this->getMockBuilder('Emarref\\Jwt\\Claim\\PrivateClaim')->getMock(); $claim->expects($this->once())->method('setValue')->with('2'); $claim->expects($this->once())->method('getValue')->will($this->returnValue('2')); $claim->expects($this->exactly(2))->method('getName')->will($this->returnValue('b')); $this->claimFactory->expects($this->once())->method('get')->with('b')->will($this->returnValue($claim)); // Assert $token = $this->serializer->deserialize($jwt); $this->assertSame('{"a":"1"}', $token->getHeader()->jsonSerialize()); $this->assertSame('{"b":"2"}', $token->getPayload()->jsonSerialize()); $this->assertSame('c', $token->getSignature()); }