1. Go to this page and download the library: Download rbdwllr/psr-jwt library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rbdwllr / psr-jwt example snippets
// Will generate a text/html response if JWT authorisation fails.
\PsrJwt\Factory\JwtMiddleware::html('secret', 'tokenKey', 'body');
// Will generate an application/json response if JWT authorisation fails.
\PsrJwt\Factory\JwtMiddleware::json('secret', 'tokenKey', ['body']);
// Can be added to any routes file in Slim, often index.php.
ponse) {
$response->getBody()->write("JSON Web Token is Valid!");
return $response;
})->add(\PsrJwt\Factory\JwtMiddleware::html('Secret123!456$', 'jwt', 'Authorisation Failed'));
use PsrJwt\Helper\Request;
$helper = new Request();
// Will return a ReallySimpleJWT Parsed object.
$helper->getParsedToken($request, $tokenKey);
// Return the token header as an array.
$helper->getTokenHeader($request, $tokenKey);
// Return the token payload as an array.
$helper->getTokenPayload($request, $tokenKey);
use PsrJwt\Handler\Html;
use PsrJwt\JwtAuthMiddleware;
$htmlHandler = new Html($secret, $tokenKey, $body);
$middleware = new JwtAuthMiddleware($htmlHandler);
// Create Middleware with JSON handler.
use PsrJwt\Handler\Json;
use PsrJwt\JwtAuthMiddleware;
// The handler.
$jsonHandler = new Json($secret, $tokenKey, $body);
// The middleware.
$middleware = new JwtAuthMiddleware($jsonHandler);
// An example JWT Authorisation Handler.
use PsrJwt\Auth\Authorise;
use PsrJwt\JwtAuthMiddleware;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Nyholm\Psr7\Response;
class MyHandler extends Authorise implements RequestHandlerInterface
{
public function __construct(string $secret, string $tokenKey)
{
parent::__construct($secret, $tokenKey);
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$auth = $this->authorise($request);
return new Response(
$auth->getCode(),
[],
'The Response Body',
'1.1',
$auth->getMessage()
);
}
}
// Add Handler to Middleware.
$middleware = new JwtAuthMiddleware(new MyHandler('secret', 'token-key'));
// Add Middleware to Slim PHP route.
$app->get('/my/route', function (ServerRequestInterface $request, ResponseInterface $response) {
$response->getBody()->write("OK!");
return $response;
})->add($middleware);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.