PHPackages                             guisz1/laravel-keycloak-guard-without-resource\_access - 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. guisz1/laravel-keycloak-guard-without-resource\_access

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

guisz1/laravel-keycloak-guard-without-resource\_access
======================================================

🔑 Simple Keycloak Guard for Laravel

1.4(4y ago)0809MITPHP

Since Jul 20Pushed 4y agoCompare

[ Source](https://github.com/guisz1/laravel-keycloak-guard-without-resource_access)[ Packagist](https://packagist.org/packages/guisz1/laravel-keycloak-guard-without-resource_access)[ Docs](https://github.com/guisz1/laravel-keycloak-guard-without-resource_access)[ RSS](/packages/guisz1-laravel-keycloak-guard-without-resource-access/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (13)Used By (0)

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

 [![](https://camo.githubusercontent.com/c59cbddffe2833843d413d748e720118bf0ff2f1575a1d778f93256648b6a268/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f62736f6e74656e6f72696f2f6c61726176656c2d6b6579636c6f616b2d67756172642e737667)](https://camo.githubusercontent.com/c59cbddffe2833843d413d748e720118bf0ff2f1575a1d778f93256648b6a268/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f62736f6e74656e6f72696f2f6c61726176656c2d6b6579636c6f616b2d67756172642e737667) [![](https://camo.githubusercontent.com/43340c432c1338eb6692ff2acfea404c2fc4c46eeabfabf142b060fdc01aff86/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f62736f6e74656e6f72696f2f6c61726176656c2d6b6579636c6f616b2d67756172642e737667)](https://camo.githubusercontent.com/43340c432c1338eb6692ff2acfea404c2fc4c46eeabfabf142b060fdc01aff86/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f62736f6e74656e6f72696f2f6c61726176656c2d6b6579636c6f616b2d67756172642e737667)

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

[](#simple-keycloak-guard-for-laravel--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.

✔️ 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  or

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)

### Laravel / Lumen

[](#laravel--lumen)

Require the package

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

```

### Lumen only

[](#lumen-only)

Register the provider in your boostrap app file `bootstrap/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 `bootstrap/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.**

Optionally you can publish the config file.

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

```

```
