PHPackages                             mohsen-mhm/laravel-keycloak-oauth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. mohsen-mhm/laravel-keycloak-oauth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

mohsen-mhm/laravel-keycloak-oauth
=================================

Laravel package for Keycloak OAuth2 authentication with JWT role extraction and user synchronization

v1.0.2(7mo ago)019MITPHPPHP ^8.1

Since Sep 28Pushed 7mo agoCompare

[ Source](https://github.com/Mohsen-mhm/laravel-keycloak-oauth)[ Packagist](https://packagist.org/packages/mohsen-mhm/laravel-keycloak-oauth)[ RSS](/packages/mohsen-mhm-laravel-keycloak-oauth/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (8)Versions (4)Used By (0)

Laravel Keycloak OAuth
======================

[](#laravel-keycloak-oauth)

A Laravel package for Keycloak OAuth2 authentication with JWT role extraction and user synchronization.

Features
--------

[](#features)

- 🔐 Keycloak OAuth2 integration using Laravel Socialite
- 🔍 JWT token parsing for role extraction
- 👤 Automatic user creation and synchronization
- 🛡️ Role-based authorization with Laravel Gates
- 🔄 Token refresh functionality
- 📋 User permission management
- ✅ Health check endpoints
- 🚀 Easy to integrate and configure

Requirements
------------

[](#requirements)

- PHP 8.1+
- Laravel 9.0+ (supports Laravel 9, 10, 11, and 12)
- Laravel Passport
- Laravel Socialite
- SocialiteProviders Keycloak

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require mohsen-mhm/laravel-keycloak-oauth
```

The package will automatically install the required dependencies including SocialiteProviders Keycloak.

Install Laravel Passport if not already installed:

```
# For Laravel 11+
composer require laravel/passport:^12.0
php artisan passport:install

# For Laravel 9-10
composer require laravel/passport:^11.0
php artisan passport:install
```

Publish the configuration file:

```
php artisan vendor:publish --tag=keycloak-oauth-config
```

Publish and run the migrations:

```
php artisan vendor:publish --tag=keycloak-oauth-migrations
php artisan migrate
```

Configuration
-------------

[](#configuration)

### Environment Variables

[](#environment-variables)

Add the following environment variables to your `.env` file:

```
KEYCLOAK_CLIENT_ID=your-client-id
KEYCLOAK_CLIENT_SECRET=your-client-secret
KEYCLOAK_REDIRECT_URI=http://localhost:8000/api/auth/keycloak/callback
KEYCLOAK_BASE_URL=http://localhost:8080
KEYCLOAK_REALM=your-realm
FRONTEND_URL=http://localhost:3000
```

### User Model Setup

[](#user-model-setup)

Add the Keycloak authentication trait to your User model:

```
