PHPackages                             saldanhaascs/laravel-keycloak-guard - 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. saldanhaascs/laravel-keycloak-guard

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

saldanhaascs/laravel-keycloak-guard
===================================

🔑 Simple Keycloak Guard for Laravel &gt;=8

2.0.0(5y ago)015MITPHP

Since Jul 20Pushed 5y ago1 watchersCompare

[ Source](https://github.com/SaldanhaASCS/laravel-keycloak-guard)[ Packagist](https://packagist.org/packages/saldanhaascs/laravel-keycloak-guard)[ Docs](https://github.com/saldanhaascs/laravel-keycloak-guard)[ RSS](/packages/saldanhaascs-laravel-keycloak-guard/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (3)Dependencies (3)Versions (15)Used By (0)

 [![](bird.png)](bird.png)

Simple Keycloak Guard for Laravel 8 / Lumen
===========================================

[](#simple-keycloak-guard-for-laravel-8--lumen)

This package helps you authenticate users on a Laravel API based on JWT tokens generated from **Keycloak Server**.

Requirements
============

[](#requirements)

✔️ I`m building an API with Laravel &gt;=8.

✔️ I will not use Laravel Passport for authentication, because Keycloak Server will do the job.

✔️ The frontend is a separated project.

✔️ The frontend users authenticate **directly on Keycloak Server** to obtain a JWT token. This process have nothing to do with the Laravel API.

✔️ The frontend keep the JWT token from Keycloak Server.

✔️ The frontend make requests to the Laravel API, with that token.

💔 If your app does not match requirements, probably you are looking for

The flow
========

[](#the-flow)

 [![](flow.png)](flow.png)

1. The frontend user authenticates on Keycloak Server
2. The frontend user obtains a JWT token.
3. In another moment, the frontend user makes a request to some protected endpoint on a Laravel API, with that token.
4. The Laravel API (through `Keycloak Guard`) handle it.

    - Verify token signature.
    - Verify token structure.
    - Verify token expiration time.
    - Verify if my API allows `resource access` from token.
5. If everything is ok, find the user on database and authenticate it on my API.
6. Return response

Install
=======

[](#install)

Require the package

```
composer require saldanhaascs/laravel-keycloak-guard

```

Publish the config file

```
php artisan vendor:publish  --provider="KeycloakGuard\KeycloakGuardServiceProvider"

```

### Lumen

[](#lumen)

Register the provider in your boostrap app file `boostrap/app.php`

Add the following line in the "Register Service Providers" section at the bottom of the file.

```
$app->register(\KeycloakGuard\KeycloakGuardServiceProvider::class);
```

For facades, uncomment `$app->withFacades();` in your boostrap app file `boostrap/app.php`

Configuration
=============

[](#configuration)

Keycloak Guard
--------------

[](#keycloak-guard)

The Keycloak Guard configuration can be handled from Laravel `.env` file. ⚠️ Be sure all strings **are trimmed.**

```
