-
-
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.
#177 Add JWTAuthenticationResponse (chalasr)
| Q | A | |---------------|------| | Bug fix? | no | | New feature? | yes | | BC breaks? | no | | Deprecations | yes | | Fixed tickets | n/a | | Tests pass? | yes | This introduces a generic Response class for successful/failed authentication. BTW remove duplicated failure Responses with pretty much the same data (3times). Let me know what do you think. Steps: - [x] Add JWTAuthenticationFailureResponse & JWTAuthenticationSuccessResponses - [x] Add tests - [x] Update documentation
- Loading branch information
Showing
12 changed files
with
227 additions
and
52 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
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,70 @@ | ||
<?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 | ||
*/ | ||
This comment has been minimized.
Sorry, something went wrong. |
||
public function __construct($message = 'Bad credentials', $statusCode = JsonResponse::HTTP_UNAUTHORIZED) | ||
{ | ||
$this->message = $message; | ||
|
||
parent::__construct(null, $statusCode, ['WWW-Authenticate' => 'Bearer']); | ||
} | ||
|
||
/** | ||
* Sets the failure message. | ||
* | ||
* @param string $message | ||
* | ||
* @return JWTAuthenticationFailureResponse | ||
*/ | ||
public function setMessage($message) | ||
{ | ||
$this->message = $message; | ||
|
||
$this->setData(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the failure message. | ||
* | ||
* @return string | ||
*/ | ||
public function getMessage() | ||
{ | ||
return $this->message; | ||
} | ||
|
||
/** | ||
* Sets the response data with the statusCode & message included. | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function setData($data = []) | ||
{ | ||
parent::setData(['code' => $this->statusCode, 'message' => $this->message] + (array) $data); | ||
} | ||
} |
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,45 @@ | ||
<?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. | ||
*/ | ||
public function __construct($token, array $data = null) | ||
{ | ||
$this->token = $token; | ||
|
||
parent::__construct($data); | ||
} | ||
|
||
/** | ||
* Sets the response data with the JWT included. | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function setData($data = []) | ||
{ | ||
parent::setData(['token' => $this->token] + (array) $data); | ||
} | ||
} |
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
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,45 @@ | ||
<?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; | ||
} | ||
|
||
/** | ||
* @depends testResponse | ||
*/ | ||
public function testSetMessage(JWTAuthenticationFailureResponse $response) | ||
{ | ||
$newMessage = 'new message'; | ||
$response->setMessage($newMessage); | ||
|
||
$responseBody = json_decode($response->getContent()); | ||
|
||
$this->assertSame($response->getStatusCode(), $responseBody->code); | ||
$this->assertSame($newMessage, $response->getMessage()); | ||
$this->assertSame($newMessage, $responseBody->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,42 @@ | ||
<?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() | ||
{ | ||
$data = [ | ||
'username' => 'foobar', | ||
'email' => '[email protected]' | ||
]; | ||
$expected = ['token' => 'jwt'] + $data; | ||
$response = new JWTAuthenticationSuccessResponse($expected['token'], $data); | ||
|
||
$this->assertSame(200, $response->getStatusCode()); | ||
$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->assertAttributeSame($replacementData['foo'], 'foo', json_decode($response->getContent())); | ||
$this->assertAttributeNotEmpty('token', json_decode($response->getContent())); | ||
} | ||
} |
Oops, something went wrong.
Missing statusCode phpdoc...