PHP code example of sindria / oauth2-keycloak

1. Go to this page and download the library: Download sindria/oauth2-keycloak 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/ */

    

sindria / oauth2-keycloak example snippets


composer 

$passport = Passport::init([
    'authServerUrl' => 'http://127.0.0.1:8080/auth',
    'realm'         => 'xxxx',
    'clientId'      => 'backend',
    'clientSecret'  => 'xxxxx',
    'redirectUri'   => 'http://127.0.0.1:8003/auto',
    'periodNoCheck' => 3600,
    'periodCheck'   => 180,
]);

$user = $passport->checkLogin();
$user->getAttr('username');
$user->toArray();

$passport->logout();

$passport->getAccessToken(); // can save in client
$passport->getToken(); // secret
$passport->getAuthorizationUrl();
$passport->getLogoutUrl();

$user = $passport->checkAuth();

// permission
$user->can($resource, $scope);
$user->cannot($resource, $scope);

// list
$user->getClients();
$user->getPermissions();

// role
$user->getRoles();
$user->hasRole($role);
$user->inRoles($roleList);