-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CS Fixes [WIP] Use the JWTResponse in authentication handlers Add authentication failure/success Response classes Fix tests
- Loading branch information
Showing
10 changed files
with
244 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Response; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
||
/** | ||
* JWTAuthenticationFailureResponse. | ||
* | ||
* Response sent on failed JWT authentication (can be replaced by a custom Response). | ||
* | ||
* @internal | ||
* | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
final class JWTAuthenticationFailureResponse extends JsonResponse | ||
{ | ||
/** | ||
* The response message. | ||
* | ||
* @var string | ||
*/ | ||
private $message; | ||
|
||
/** | ||
* @param string $message A failure message passed in the response body | ||
*/ | ||
public function __construct($message = 'Bad credentials') | ||
{ | ||
$this->message = $message; | ||
|
||
parent::__construct(null, self::HTTP_UNAUTHORIZED, ['WWW-Authenticate' => 'Bearer']); | ||
|
||
$this->setData([ | ||
'code' => $this->statusCode, | ||
'message' => $this->message, | ||
]); | ||
} | ||
|
||
/** | ||
* Sets the failure message. | ||
* | ||
* @param string $message | ||
* | ||
* @return JWTAuthenticationFailureResponse | ||
*/ | ||
public function setMessage($message) | ||
{ | ||
$this->message = $message; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the failure message. | ||
* | ||
* @return string | ||
*/ | ||
public function getMessage() | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Response; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
||
/** | ||
* Response sent on successful JWT authentication. | ||
* | ||
* @internal | ||
* | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
final class JWTAuthenticationSuccessResponse extends JsonResponse | ||
{ | ||
/** | ||
* The Json Web Token. | ||
* | ||
* Immutable property. | ||
* | ||
* @var string | ||
*/ | ||
private $token; | ||
|
||
/** | ||
* @param string $token Json Web Token | ||
* @param array $data Extra data passed to the response body. | ||
* @param array $headers HTTP headers | ||
*/ | ||
public function __construct($token, array $extraData = []) | ||
{ | ||
$this->token = $token; | ||
$this->extraData = $extraData; | ||
|
||
parent::__construct(); | ||
|
||
$this->setBody(); | ||
} | ||
|
||
/** | ||
* Gets the Json Web Token. | ||
* | ||
* @return string | ||
*/ | ||
public function getToken() | ||
{ | ||
return $this->token; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setExtraData(array $extraData = []) | ||
{ | ||
$this->extraData = $extraData; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getExtraData() | ||
{ | ||
return $this->extraData; | ||
} | ||
|
||
/** | ||
* Prevents unexpected response content. | ||
* | ||
* @internal | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function setData($data = []) | ||
{ | ||
return $this->setBody(); | ||
} | ||
|
||
/** | ||
* Creates the response body. | ||
* | ||
* @return JWTAuthenticationSuccessResponse | ||
*/ | ||
private function setBody() | ||
{ | ||
parent::setData(['token' => $this->token] + $this->extraData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Response; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse; | ||
|
||
/** | ||
* Tests the JWTAuthenticationFailureResponse | ||
* | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
final class JWTAuthenticationFailureResponseTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testResponse() | ||
{ | ||
$expected = [ | ||
'code' => 401, | ||
'message' => 'message', | ||
]; | ||
|
||
$response = new JWTAuthenticationFailureResponse($expected['message']); | ||
|
||
$this->assertSame($expected['message'], $response->getMessage()); | ||
$this->assertSame($expected['code'], $response->getStatusCode()); | ||
$this->assertSame('Bearer', $response->headers->get('WWW-Authenticate')); | ||
$this->assertSame(json_encode($expected), $response->getContent()); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Response; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationSuccessResponse; | ||
|
||
/** | ||
* Tests the JWTAuthenticationSuccessResponse. | ||
* | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
final class JWTAuthenticationSuccessResponseTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testResponse() | ||
{ | ||
$extraData = [ | ||
'username' => 'foobar', | ||
'email' => '[email protected]' | ||
]; | ||
$expected = ['token' => 'jwt'] + $extraData; | ||
|
||
$response = new JWTAuthenticationSuccessResponse($expected['token'], $extraData); | ||
|
||
$this->assertSame($expected['token'], $response->getToken()); | ||
$this->assertSame(200, $response->getStatusCode()); | ||
$this->assertSame($extraData, $response->getExtraData()); | ||
|
||
$this->assertSame(json_encode($expected), $response->getContent()); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* @depends testResponse | ||
*/ | ||
public function testReplaceData(JWTAuthenticationSuccessResponse $response) | ||
{ | ||
$replacementData = ['foo' => 'bar']; | ||
$response->setData($replacementData); | ||
|
||
// Test that the previous method call has no effect on the original body | ||
$this->assertNotEquals(json_encode($replacementData), $response->getContent()); | ||
$this->assertSame( | ||
json_encode(['token' => $response->getToken()] + $response->getExtraData()), | ||
$response->getContent() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters