antarccub / laravel-jwt-microservice
Base project for create microservices based on Laravel framework and JWT auth
dev-master
2018-03-10 12:48 UTC
Requires
- php: >=7.0.0
- fideloper/proxy: ~3.3
- guzzlehttp/guzzle: ^6.3
- laravel/framework: 5.5.*
- laravel/tinker: ~1.0
- lcobucci/jwt: ^3.2
- tymon/jwt-auth: ^0.5.12
Requires (Dev)
- filp/whoops: ~2.0
- fzaninotto/faker: ~1.4
- mockery/mockery: ~1.0
- phpseclib/phpseclib: ^2.0
- phpunit/phpunit: ~6.0
- symfony/thanks: ^1.0
This package is not auto-updated.
Last update: 2025-03-10 18:27:36 UTC
README
This is an example project to use Laravel framework as microservice with JWT auth.
NOTE: The purpose of this, is only speed-up coding to implement private Laravel microservices with external Identity Provider.
Getting user from request
class SendNotificationsController extends Controller
{
public function send(Request $request){
$user = $request->user(); // Get AuthUser instance from request
$user_id = $user->id; // Get user id
$user_email = $user->id; // Get user email
}
}
AuthUser Class
class AuthUser
{
private $token;
public $email;
public $id;
/*
* Here you can add your custom claims
*/
public function __construct()
{
$this->token = (new Parser())->parse(JWTAuth::getToken()->get());
$this->id = self::getClaim("sub");
$this->email = self::getClaim("email");
}
private function getClaim($name){
return $this->token->getClaim($name);
}
}
JWT Signatures
By default, the project uses RS256 as algorythm to verify tokens and retrives the public key from external service.
You can change the url in jwt.php config file:
'token-provider' => [
'pkey-url' => env('ISSUER_PKEY_URL')
]
Docker
Project includes Dockerfile and docker-compose.yml to run locally using Docker.
Contributing
Feel free to change all you want!