yangze/oauth2-keycloak

A wrapper for the Keycloak OAuth 2.0 Client Provider

v2.0.9 2021-08-16 03:55 UTC

README

A wrapper for the Keycloak OAuth 2.0 Client Provider, support Authentication and Authorization.

Installation

To install, use composer:

composer require yangze/oauth2-keycloak

Init

$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,
]);

Authentication

Login

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

logout

$passport->logout();

Other Methds

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

Authorization

$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);