PHP code example of admin9 / laravel-oidc-client
1. Go to this page and download the library: Download admin9/laravel-oidc-client 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/ */
admin9 / laravel-oidc-client example snippets
protected $fillable = [
'name',
'email',
'password',
'oidc_sub',
'auth_server_refresh_token',
];
protected $hidden = [
'password',
'remember_token',
'auth_server_refresh_token',
];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'auth_server_refresh_token' => 'encrypted',
];
}
@if (session('oidc_error'))
<div class="alert alert-danger">
Authentication failed: {{ session('oidc_error_description') }}
</div>
@endif
use Admin9\OidcClient\Services\OidcService;
public function logout(Request $request, OidcService $oidcService)
{
$user = $request->user();
$oidcService->revokeAuthServerToken($user);
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
if ($oidcService->isOidcUser($user)) {
return redirect($oidcService->getSsoLogoutUrl());
}
return redirect('/');
}
use Admin9\OidcClient\Contracts\OidcAuthenticator;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class JwtAuthenticator implements OidcAuthenticator
{
public function authenticate(Request $request, mixed $user, array $tokens, array $userInfo): Response
{
$jwt = /* generate your JWT */;
return response()->json(['token' => $jwt]);
}
}
'authenticator' => \App\Services\JwtAuthenticator::class,
// app/Listeners/LogOidcLogin.php
use Admin9\OidcClient\Events\OidcUserAuthenticated;
class LogOidcLogin
{
public function handle(OidcUserAuthenticated $event): void
{
logger()->info('OIDC login', [
'user_id' => $event->user->getKey(),
'new' => $event->isNewUser,
]);
}
}
bash
composer vendor:publish --tag="oidc-client-config"
php artisan vendor:publish --tag="oidc-client-migrations"
php artisan migrate