PHP code example of mainick / keycloak-client-bundle
1. Go to this page and download the library: Download mainick/keycloak-client-bundle 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/ */
declare(strict_types=1);
namespace App\Service;
use Mainick\KeycloakClientBundle\Interface\IamClientInterface;
class IamService
{
public function __construct(
private IamClientInterface $iamClient
) {
}
}
// authenticate the user with username and password
$accessToken = $this->iamClient->authenticate($username, $password);
// authenticate the user with authorization code
$accessToken = $this->iamClient->authenticateCodeGrant($authorizationCode);
// verify and introspect the token
$userRepresentation = $this->iamClient->verifyToken($accessToken);
echo $userRepresentation->id; // id
echo $userRepresentation->username; // username
echo $userRepresentation->email; // email
echo $userRepresentation->firstName; // first name
echo $userRepresentation->lastName; // last name
echo $userRepresentation->name; // full name
echo $userRepresentation->groups; // all groups assigned to the user
echo $userRepresentation->realmRoles; // realm roles assigned to the user
echo $userRepresentation->clientRoles; // client roles assigned to the user
echo $userRepresentation->applicationRoles; // specific client roles assigned to the user
echo $userRepresentation->attributes; // additional user attributes
// refresh the token
$accessToken = $this->iamClient->refreshToken($accessToken);
// get user info
$userInfo = $this->iamClient->userInfo($accessToken);
echo $userInfo->id; // id
echo $userInfo->username; // username
echo $userInfo->email; // email
echo $userInfo->firstName; // first name
echo $userInfo->lastName; // last name
echo $userInfo->name; // full name
echo $userInfo->groups; // all groups assigned to the user
echo $userInfo->realmRoles; // realm roles assigned to the user
echo $userInfo->clientRoles; // client roles assigned to the user
echo $userInfo->applicationRoles; // specific client roles assigned to the user
echo $userInfo->attributes; // additional user attributes
// has role
$hasRole = $this->iamClient->hasRole($accessToken, $roleName);
// has any role
$hasAnyRole = $this->iamClient->hasAnyRole($accessToken, $roleNames);
// has all roles
$hasAllRoles = $this->iamClient->hasAllRoles($accessToken, $roleNames);
// has group
$hasGroup = $this->iamClient->hasGroup($accessToken, $groupName);
// has any group
$hasAnyGroup = $this->iamClient->hasAnyGroup($accessToken, $groupNames);
// has all groups
$hasAllGroups = $this->iamClient->hasAllGroups($accessToken, $groupNames);
// has scope
$hasScope = $this->iamClient->hasScope($accessToken, $scopeName);
// has any scope
$hasAnyScope = $this->iamClient->hasAnyScope($accessToken, $scopeNames);
// has all scopes
$hasAllScopes = $this->iamClient->hasAllScopes($accessToken, $scopeNames);
// get the user object from the request
$user = $request->attributes->get('user');
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Mainick\KeycloakClientBundle\Annotation\ExcludeTokenValidationAttribute;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
#[Route("/path/to/excluded/route", name: "app.excluded_route", methods: ["GET"])]
#[ExcludeTokenValidationAttribute]
public function excludedRouteAction(): Response
{
// This route is excluded from token validation.
// ...
}
}
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Mainick\KeycloakClientBundle\Annotation\ExcludeTokenValidationAttribute;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
#[Route("/logout", name: "app.logout", methods: ["GET"])]
public function logout(): RedirectResponse
{
return $this->redirectToRoute('mainick_keycloak_security_auth_logout');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.