-
-
Notifications
You must be signed in to change notification settings - Fork 614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add JWTAuthenticationResponse #177
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
*/ | ||
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); | ||
} | ||
} |
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 = []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set data is completely ignored. public function __construct($token, $data = null, $status = 200, array $headers = array())
{
$this->token = $token;
parent::__construct($data, $status, $headers);
}
public function setData($data) {
parent::setData(['token' => $this->token] + $data);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow... I was gone to far, written too late in the night... This will be widely sufficient. |
||
{ | ||
parent::setData(['token' => $this->token] + (array) $data); | ||
} | ||
} |
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); | ||
} | ||
} |
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())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it expected that the JSON body keep the initial message when the message is modified ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @GromNaN