sindria/oauth2-keycloak

A wrapper for the Keycloak OAuth 2.0 Client Provider

1.0.0 2023-04-29 01:29 UTC

This package is not auto-updated.

Last update: 2024-04-28 05:18:41 UTC


README

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

Installation

To install, use composer:

composer require sindria/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);