Skip to content

Commit

Permalink
Update doc
Browse files Browse the repository at this point in the history
Scrutinizer Fixes

Fix segmentation fault (infinite loop)

BTW fix deprecation warning if funct test config
  • Loading branch information
chalasr committed Jun 3, 2016
1 parent 9069a5f commit 82ce213
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
21 changes: 10 additions & 11 deletions Resources/doc/2-data-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $even
return;
}

// $data['token'] contains the JWT

$data['data'] = array(
'roles' => $user->getRoles(),
);
Expand Down Expand Up @@ -199,7 +197,7 @@ public function onJwtEncoded(JWTEncodedEvent $event)

#### Events::AUTHENTICATION_FAILURE - customize the failure response

By default, the response in case of failed authentication is just a json containing a "Bad credentials" message and a 401 status code, but you can set a custom response.
By default, the response in case of failed authentication is just a json containing a failure message and a 401 status code, but you can set a custom response.

``` yaml
# services.yml
Expand All @@ -214,6 +212,9 @@ Example 7: set a custom response on authentication failure
``` php
// Acme\Bundle\ApiBundle\EventListener\AuthenticationFailureListener.php

use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;

/**
* @param AuthenticationFailureEvent $event
*/
Expand All @@ -224,7 +225,7 @@ public function onAuthenticationFailureResponse(AuthenticationFailureEvent $even
'message' => 'Bad credentials, please verify that your username/password are correctly set',
];

$response = new JsonResponse($data, 401);
$response = new JWTAuthenticationFailureResponse($data);

$event->setResponse($response);
}
Expand All @@ -243,21 +244,19 @@ services:
- { name: kernel.event_listener, event: lexik_jwt_authentication.on_jwt_invalid, method: onJWTInvalid }
```
Example 8: set a custom response message on invalid token
Example 8: set a custom response message and status code on invalid token
``` php
// Acme\Bundle\ApiBundle\EventListener\JWTInvalidListener.php

use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;

/**
* @param JWTInvalidEvent $event
*/
public function onJWTInvalid(JWTInvalidEvent $event)
{
$data = [
'status' => '403 Forbidden',
'message' => 'Your token is invalid, please login again to get a new one',
];

$response = new JsonResponse($data, 403);
$response = new JWTAuthenticationFailureResponse('Your token is invalid, please login again to get a new one', 403);

$event->setResponse($response);
}
Expand Down
8 changes: 4 additions & 4 deletions Response/JWTAuthenticationFailureResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ final class JWTAuthenticationFailureResponse extends JsonResponse
/**
* @param string $message A failure message passed in the response body
*/
public function __construct($message = 'Bad credentials')
public function __construct($message = 'Bad credentials', $statusCode = JsonResponse::HTTP_UNAUTHORIZED)
{
$this->message = $message;

parent::__construct(null, self::HTTP_UNAUTHORIZED, ['WWW-Authenticate' => 'Bearer']);
parent::__construct(null, $statusCode, ['WWW-Authenticate' => 'Bearer']);

$this->setData([
'code' => $this->statusCode,
'message' => $this->message,
'code' => $statusCode,
'message' => $message,
]);
}

Expand Down
30 changes: 13 additions & 17 deletions Response/JWTAuthenticationSuccessResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ final class JWTAuthenticationSuccessResponse extends JsonResponse
private $token;

/**
* @param string $token Json Web Token
* @param array $data Extra data passed to the response body.
* @param array $headers HTTP headers
* The response extra data
*
* @var array
*/
private $extraData;

/**
* @param string $token Json Web Token
* @param array $extraData Extra data passed to the response body.
*/
public function __construct($token, array $extraData = [])
{
Expand All @@ -34,7 +40,7 @@ public function __construct($token, array $extraData = [])

parent::__construct();

$this->setBody();
$this->setData();
}

/**
Expand Down Expand Up @@ -66,23 +72,13 @@ public function getExtraData()
}

/**
* Prevents unexpected response content.
* {@inheritdoc}
*
* @internal
* @param array $data Unused parameter for risks limitation
*
* {@inheritdoc}
* @internal
*/
public function setData($data = [])
{
return $this->setBody();
}

/**
* Creates the response body.
*
* @return JWTAuthenticationSuccessResponse
*/
private function setBody()
{
parent::setData(['token' => $this->token] + $this->extraData);
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ framework:
resource: %kernel.root_dir%/config/routing.yml

lexik_jwt_authentication:
private_key_path: %kernel.root_dir%/var/private.pem
public_key_path: %kernel.root_dir%/var/public.pem
private_key_path: '%kernel.root_dir%/var/private.pem'
public_key_path: '%kernel.root_dir%/var/public.pem'
pass_phrase: testing

security:
Expand Down

0 comments on commit 82ce213

Please sign in to comment.