/** * Returns the signature from given data * * @param array $header * @param string $data * * @return Signature */ protected function parseSignature(array $header, $data) { if ($data == '' || !isset($header['alg']) || $header['alg'] == 'none') { return null; } $hash = $this->decoder->base64UrlDecode($data); return new Signature($hash); }
public function setUp() { $this->publicKeyString = file_get_contents(__DIR__ . '/samples/public.pem'); $this->tokenString = rtrim(file_get_contents(__DIR__ . '/samples/token.txt'), '\\r\\n'); $this->tokenHeaders = ["typ" => "JWT", "alg" => "RS256"]; $this->tokenClaims = ["uid" => "1", "nick" => "foo", "email" => "*****@*****.**", "iss" => "http://culudb-jwt-provider.dev", "iat" => "1461829061", "exp" => "1461829061", "nbf" => "1461829061"]; $this->tokenClaimsAsValueObjects = ["uid" => new Basic('uid', '1'), "nick" => new Basic('nick', 'foo'), "email" => new Basic('email', '*****@*****.**'), "iss" => new EqualsTo('iss', 'http://culudb-jwt-provider.dev'), "iat" => new LesserOrEqualsTo('iat', '1461829061'), "exp" => new GreaterOrEqualsTo('exp', '1461829061'), "nbf" => new LesserOrEqualsTo('nbf', '1461829061')]; $this->payload = explode('.', $this->tokenString); $decoder = new Decoder(); $hash = $decoder->base64UrlDecode($this->payload[2]); $this->signature = new Signature($hash); $this->token = new Jwt($this->tokenHeaders, $this->tokenClaimsAsValueObjects, $this->signature, $this->payload); $this->parser = new Parser(); $this->validationData = new ValidationData(); $this->validationData->setIssuer("http://culudb-jwt-provider.dev"); $this->signer = new Sha256(); $this->publicKey = new Key($this->publicKeyString); $this->requiredCLaims = ['uid', 'nick', 'email']; $this->decoderService = new JwtDecoderService($this->parser, $this->validationData, $this->signer, $this->publicKey, $this->requiredCLaims); }
/** * @test * * @uses Lcobucci\JWT\Parser::__construct * @uses Lcobucci\JWT\Token::__construct * @uses Lcobucci\JWT\Signature::__construct * * @covers Lcobucci\JWT\Parser::parse * @covers Lcobucci\JWT\Parser::splitJwt * @covers Lcobucci\JWT\Parser::parseHeader * @covers Lcobucci\JWT\Parser::parseClaims * @covers Lcobucci\JWT\Parser::parseSignature */ public function parseMustReturnASignedTokenWhenSignatureIsInformed() { $this->decoder->expects($this->at(1))->method('jsonDecode')->willReturn(['typ' => 'JWT', 'alg' => 'HS256']); $this->decoder->expects($this->at(3))->method('jsonDecode')->willReturn(['aud' => 'test']); $this->decoder->expects($this->at(4))->method('base64UrlDecode')->willReturn('aaa'); $parser = $this->createParser(); $token = $parser->parse('a.a.a'); $this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'HS256'], 'headers', $token); $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token); $this->assertAttributeEquals(new Signature('aaa'), 'signature', $token); }