Skip to content

Commit

Permalink
Adding a way to configure token headers (closes #53).
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Nov 15, 2015
1 parent 8819b1a commit 78c7d4f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ protected function setRegisteredClaim($name, $value, $replicate)
return $this;
}

/**
* Configures a header item
*
* @param string $name
* @param mixed $value
*
* @return Builder
*
* @throws BadMethodCallException When data has been already signed
*/
public function setHeader($name, $value)
{
if ($this->signature) {
throw new BadMethodCallException('You must unsign before make changes');
}

$this->headers[(string) $name] = $this->claimFactory->create($name, $value);

return $this;
}

/**
* Configures a claim item
*
Expand Down
2 changes: 2 additions & 0 deletions test/functional/EcdsaTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ public function builderCanGenerateAToken()
->setAudience('http://client.abc.com')
->setIssuer('http://api.abc.com')
->set('user', $user)
->setHeader('jki', '1234')
->sign($this->signer, static::$ecdsaKeys['private'])
->getToken();

$this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
$this->assertEquals('1234', $token->getHeader('jki'));
$this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
$this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
$this->assertEquals($user, $token->getClaim('user'));
Expand Down
2 changes: 2 additions & 0 deletions test/functional/HmacTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ public function builderCanGenerateAToken()
->setAudience('http://client.abc.com')
->setIssuer('http://api.abc.com')
->set('user', $user)
->setHeader('jki', '1234')
->sign($this->signer, 'testing')
->getToken();

$this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
$this->assertEquals('1234', $token->getHeader('jki'));
$this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
$this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
$this->assertEquals($user, $token->getClaim('user'));
Expand Down
2 changes: 2 additions & 0 deletions test/functional/RsaTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ public function builderCanGenerateAToken()
->setAudience('http://client.abc.com')
->setIssuer('http://api.abc.com')
->set('user', $user)
->setHeader('jki', '1234')
->sign($this->signer, static::$rsaKeys['private'])
->getToken();

$this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
$this->assertEquals('1234', $token->getHeader('jki'));
$this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
$this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
$this->assertEquals($user, $token->getClaim('user'));
Expand Down
59 changes: 59 additions & 0 deletions test/unit/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,39 @@ public function setMustKeepAFluentInterface()
$this->assertSame($builder, $builder->set('userId', 2));
}

/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
*
* @covers Lcobucci\JWT\Builder::setHeader
*/
public function setHeaderMustConfigureTheGivenClaim()
{
$builder = $this->createBuilder();
$builder->setHeader('userId', 2);

$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'userId' => $this->defaultClaim],
'headers',
$builder
);
}

/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
*
* @covers Lcobucci\JWT\Builder::setHeader
*/
public function setHeaderMustKeepAFluentInterface()
{
$builder = $this->createBuilder();

$this->assertSame($builder, $builder->setHeader('userId', 2));
}

/**
* @test
*
Expand Down Expand Up @@ -598,6 +631,32 @@ public function setMustRaiseExceptionWhenTokenHasBeenSigned()
$builder->set('test', 123);
}

/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::sign
* @uses Lcobucci\JWT\Builder::getToken
* @uses Lcobucci\JWT\Token
*
* @covers Lcobucci\JWT\Builder::setHeader
*
* @expectedException BadMethodCallException
*/
public function setHeaderMustRaiseExceptionWhenTokenHasBeenSigned()
{
$signer = $this->getMock(Signer::class);
$signature = $this->getMock(Signature::class, [], [], '', false);

$signer->expects($this->any())
->method('sign')
->willReturn($signature);

$builder = $this->createBuilder();
$builder->sign($signer, 'test');
$builder->setHeader('test', 123);
}

/**
* @test
*
Expand Down

0 comments on commit 78c7d4f

Please sign in to comment.