1. Go to this page and download the library: Download ph-ab/keycloak-sso 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/ */
ph-ab / keycloak-sso example snippets
use PhAb\KeycloakSso\Facades\KeycloakSso;
use PhAb\KeycloakSso\Exceptions\InvalidCredentialsException;
use PhAb\KeycloakSso\Exceptions\SsoServiceException;
use PhAb\KeycloakSso\Exceptions\SsoConnectionException;
public function login(Request $request)
{
$request->validate(['username' => 'payload (null on fallback)
'user' => $result->user, // userinfo array (or your fallback's return value)
]);
} catch (InvalidCredentialsException $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()], 401);
} catch (SsoConnectionException $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()], 503);
} catch (SsoServiceException $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()], 502);
}
}
$new = KeycloakSso::refresh($refreshToken); // array of new tokens
$info = KeycloakSso::userInfo($accessToken); // userinfo array
KeycloakSso::logout($refreshToken); // bool
namespace App\Auth;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class KeycloakDbFallback
{
/** Return a truthy value to accept the login, or null to reject. */
public function __invoke(string $username, string $password): ?User
{
$user = User::where('username', $username)->orWhere('email', $username)->first();
return $user && Hash::check($password, $user->password) ? $user : null;
}
}