This is Laravel package to use with laravel-jwt-idp (Github: https://github.com/ZanichelliEditore/laravel-jwt-idp).
composer require zanichelli/idp-extensions
Note:
you should use tag instead of branch-name (e.g. "zanichelli/idp-extensions:V1.0.0" or "zanichelli/idp-extensions:dev-{branch-name}" )
Add this lines at bottom of your .env file:
IDP_BASE_URL=https://idp.zanichelli.it
IDP_COOKIE_NAME=token
If you need to use your own login form (instead of the IDP one), please add this line too:
IDP_LOGIN_URL=https://idp.zanichelli.it/v4/login
Edit config/auth.php
as follow:
- In
'defaults'
array change value of'guard'
from'web'
to'z-session'
There are 2 migration from this package, Grants table and Sessions Table.
php artisan vendor:publish
and select the "zanichelli/idp-extension" provider
There are 3 migrations from this package:
- Grants table
- Sessions Table
- Grants table key changes (Change role_id and department_id to role_name and department_name).
php artisan vendor:publish
Using the command below will only apply the changes about role_id and department_id
php artisan vendor:publish --tag=grants-by-name-instead-of-id
Use
php artisan vendor:publish --tag=grants-by-name-instead-of-id --force
if you need to overwrite grants table changes migration.
In Kernel.php file add "idp" in your routeMiddleware
'idp' => \Zanichelli\IdpExtension\Http\Middleware\IdpMiddleware::class,
Kernel.php file is no more. Register your middleware in 'bootstrap/app.php'
$middleware->alias([
'idp' => \Zanichelli\IdpExtension\Http\Middleware\IdpMiddleware::class
]);
The default behaviour also retrieves the user's permissions (with_permissions
) and remove token from query params (without_token_url
)
You can specify different configuration like this:
Avoid to remove token from url
Route::group(['middleware'=>'idp:with_permissions,with_token_url'],function(){
Route::get('/', function(){
return view('home');
});
});
Avoid to retrieve permission
Route::group(['middleware'=>'idp:without_permissions'],function(){
Route::get('/', function(){
return view('home');
});
});
Avoid to remove token from url and retrieve permission
Route::group(['middleware'=>'idp:without_permissions,with_token_url'],function(){
Route::get('/', function(){
return view('home');
});
});
Add to your route file (tipically web.php
) the new middleware idp
; code smells like this:
Route::group(['middleware'=>'idp'],function(){
Route::get('/', function(){
return view('home');
});
});
Alternatively, two middlewares read the cookie and, if found, retrieves the user's data and adds it to the request
IdpApiMiddleware
retrieves user's data from v1 user api call
'idp' => \Zanichelli\IdpExtension\Http\Middleware\IdpApiMiddleware::class,
IdpApiJWKSMiddleware
retrieves user's data from jwt token
'idp' => \Zanichelli\IdpExtension\Http\Middleware\IdpApiJWKSMiddleware::class,
In order to edit retrive permissions or add extra parameter to user object you can extend default class IDP Middleware.
Class must implement following methods:
-
retrievePermissions
: this method take userId and roles array as input, here role-based permissions must be retrieved to output an array of strings with permissions; -
addExtraParametersToUser
: this method allow you to add extra parameters to the user object given as input.
After class creation, add in kernel.php
file the new middleware class in '$routeMiddleware'
array:
'idp' => \App\Http\Middleware\IdpMiddleware::class,
Create a logout route inside web.php
file using a logout method inside the controller.
Implement the code as follow:
Route::group(['middleware'=>'idp'],function(){
Route::get('logout', 'LoginController@logout');
});
Then define logout
:
use use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
...
public function logout()
{
return Auth::logout();
}
}
With this integration you could use some Laravel's feature that allows to handle users and their authentication.
Auth
is authtentication class that Laravel ships for this purpose and allow access to following methods:
Auth::check()
: returnstrue
if a user is authenticated,false
otherwiseAuth::guest()
: returnstrue
if a user is guest,false
otherwiseAuth::user()
: returns aZUser
class instance,null
otherwiseAuth::id()
: returnsuserId
if authtenticated,null
otherwiseAuth::hasUser()
: returnstrue
if there's a ZUser in our current session,false
otherwiseAuth::setUser($ZUser)
: sets aZuser
in sessionAuth::attempt($credentials, $remember)
: try to login with IDP without using the login form, if success returnstrue
, otherwisefalse
Auth::logout()
: logout a user, returnredirect