Download the PHP package peppertech/larakeycloak without Composer
On this page you can find all versions of the php package peppertech/larakeycloak. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download peppertech/larakeycloak
More information about peppertech/larakeycloak
Files in peppertech/larakeycloak
Package larakeycloak
Short Description Provide Authentication and Authorization using KeyCloak Socialite Provider
License MIT
Informations about the package larakeycloak
LaraKeycloak
- Overview
- Features
- Keycloak Configurations
- Installation
- Configuration
- Integration
- Testing
Overview
LaraKeycloak provides Authentication using KeyCloak Socialite Provider and RBAC Authorization by checking user roles from Keycloak.
Features
- Provides Authentication using KeyCloak Socialite Provider
- Provides Authorization by RBAC managed by KeyCloak
Keycloak Configurations
Before installing LaraKeycloak, configure your Keycloak Server to add your application as Client.
Creating a Keycloak Client
Add User Roles in Keycloak Client
Create Users and Assign Roles
Create at least a Regular User and an Admin User, for testing Authorization later on.
Installation
Configuration
Environment Variables
Variable | Required | Description | Default Value |
---|---|---|---|
KEYCLOAK_BASE_URL | Yes | Keycloak Server URL. ie. https://[keycloak server]/auth | none |
KEYCLOAK_REALMS | Yes | Keycloak Realm | none |
KEYCLOAK_CLIENT_ID | Yes | Keycloak Client ID | none |
KEYCLOAK_CLIENT_SECRET | Yes | OpenId Connect Client Secret | none |
KEYCLOAK_REDIRECT_URI | Yes | The default page to redirect users after login | /home |
KEYCLOAK_REALM_PUBLIC_KEY | Yes | Keycloak Realm RS256 Public Key | none |
Integration
Published Files
Run the following commands to publish the files to your app.
This will copy the following files:
app/Http/Controllers/LaraKeyController.php
, controller for the/auth/redirect
and '/auth/callback` routes.app/Policies/SampleAdminPolicy.php
, an example Admin Policy to secure certain pages in your application foradmin
roleresources/views/sample_admin_blade.php
, example Admin View with/sample/admin
route.app/Http/Controllers/SampleAdminController.php
, controller for the/sample/admin
route.
Routes
Create the following routes in your app/routes/web.php
Add the following logout
method in your LoginController
Socialite Keycloak Settings
Reference: https://socialiteproviders.com/Keycloak/#installation-basic-usage
-
Add the following block in your
config/services.php
-
In
app/Providers/EventServiceProvider.php
, add the following: - In
config/app.php
add theSocialiteProviders\Manager\ServiceProvider::class
and comment-outLaravel\Socialite\SocialiteServiceProvider::class
if you have added this before.
Auth Middleware
- In
app/Http/Middleware/Authenticate.php
, change theredirectTo
method. This change will make the redirection to Keycloak Login when an unauthenticated user access a protect part of the website.
Authorization
Authorization is provided by roles of user from Keycloak. PepperTech\LaraKeycloak\LaraKeycloak
class has a public method hasRole
that checks if currently logged-in user has that role. hasRole
can be used with Laravel Authorization
Defining Gates
- Define your Gate in
app/Providers/AuthServiceProvider.php
boot
method
Policies
- An example Policy is provided in
app/Policies/SampleAdminPolicy.php
that uses the LaraKeycloakhasRole
method. - An example Admin View Controller is also provided at
app/Htttp/Controllers/SampleAdminController.php
. Inspect how Gates are used here to check the user's authorization in viewing a page.
Testing
- To test if everything is working, navigate to
http://[your domain]/sample/admin
. This should redirct to Keycloak Login Page. - Login with a Keycloak User that has 'admin' role.
- Upon login, you should be able to see the Sample Admin Page.
- Logout and go to
http://[your domain]/sample/admin
again. This time, login with a user that does not have anadmin
role. - Upon login, you should see a 403 Unauthorized Page.
All versions of larakeycloak with dependencies
socialiteproviders/keycloak Version ^4.1
firebase/php-jwt Version ^5.0