altelma / php-jwt
Creating and Verifying JWT Signatures in PHP using HS256 and RS256.
v1.1
2019-02-21 09:58 UTC
This package is auto-updated.
Last update: 2024-10-12 19:17:43 UTC
README
Simple JWT for Larvel
Simple library creating and verify JWT Signatures in PHP using HS256 and RS256.
Setup
- Run
$ composer require altelma/php-jwt
Laravel
- (Only for Laravel 5.5 or minor) Add provider to config/app.php
'providers' => [ Altelma\JWT\JWTServiceProvider::class, ],
- Run
$ php artisan vendor:publish
to publish the configuration fileconfig/jwt.php
and insert:- private_key
- public_key
Lumen
- Add provider to
bootstrap/app.php
$app->register(Altelma\JWT\JWTServiceProvider::class);
-
Copy
vendor/altelma/php-jwt/config/jwt.php
toconfig/jwt.php
and insert:- private_key
- public_key
-
Add config to
bootstrap/app.php
$app->configure('jwt');
- Allow call package via Facade, uncomment
$app->withFacades(); if (!class_exists('JWT')) { class_alias('Altelma\JWT\JWTFacade', 'JWT'); }
Basic usage
The following example is for generate JWT
<?php namespace App\Http\Controllers; use GuzzleHttp\Client; class JwtController extends Controller { private function getJWT() { $client = new Client(); $response = $client->post('your_auth_url', [ 'headers' => [ 'Content-Type' => 'application/json', ], 'json' => [ 'email' => 'admin@example.com', 'password' => 'password' ] ]); $response = json_decode($response->getBody(), true); return $response['accesstoken']; } public function verifyJwt() { $jwtToken = 'YOUR_JWT_TOKEN'; $verifyToken = \JWT::verify('sha256', $jwtToken); return ['success' => $verifyToken]; } public function genJwt() { $header = [ "alg" => "RS256", "typ" => "JWT" ]; $payload = [ "sub" => "465465464646", "name" => "John Doe", "admin" => true ]; return ['success' => true, 'access_token' => \JWT::generate('sha256', $header, $payload)]; } }
Bug report
This package is not perfect now, but it can be improve together. If you found any bug or have any suggestion. Send that to me or create new issue. Thank you to use it.