Download the PHP package wg-hyve/keycloak-laravel-guard without Composer
On this page you can find all versions of the php package wg-hyve/keycloak-laravel-guard. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wg-hyve/keycloak-laravel-guard
More information about wg-hyve/keycloak-laravel-guard
Files in wg-hyve/keycloak-laravel-guard
Package keycloak-laravel-guard
Short Description Keycloak Guard for Laravel
License MIT
Homepage https://github.com/wg-hyve/keycloak-laravel-guard
Informations about the package keycloak-laravel-guard
Keycloak Laravel Implementation
This Keycloak lib does not load users from database. Keycloak user UUIDs are usually stored in the sub claim.
Installation
Configuration
Add your keycloak to your guards and use it in your routes as middleware. That's it for a normal usage.
config/auth.php
routes/api.php
Extended usage
The KeycloakGuard comes with some methods to keep it easy.
Example usage in requests
Other Methods
getTokenFromRequest
Get raw Token from request
token(): ?stdClass
Get encoded Token from request
hasRole(array|string $roles): bool
Proofs if the role or one of the roles is in your client or global roles
scopes(): array
Delivers all scopes
hasScope(string|array $scope): bool
Proofs if the scope or one of the scopes is in your JWT
getRoles(): array
Delivers all roles from your client. Client is delivered in azp claim.
roles(bool $useGlobal = true): array
Delivers all roles (global & client)
User
Saving users in a local database is disabled per default. Execute following steps to enable it.
Make sure you read the instructions for the environment variables in your .env
.
Configuration
Enable KEYCLOAK_PROVIDE_LOCAL_USER
in your environment.
The guard will save user objects from JWTs.
Migrations
Publish und execute migrations.
Make adjustments and migrate.
User Model
Update your user model in config/auth.php
. You can also extend it and add your own user model.
You are free to use your user model to extend and define custom relations. Make sure it is compatible with the migration above.
Usage
Receive the User instance with user()
Environment Variables
Local Public key of your Keycloak instance. You can find it in https://your-keycloak.dev.com/auth/realms/your_realm/
Should be azp
or client_id
depending on your Keycloak configuration
true or false, auto check if role given with is in your token
role to auto check
URL to load public key. Usually https://your-keycloak.dev.com/auth/realms/your_realm/
Cache duration for downloaded public key of your realm.
You want to set the time offset in seconds if you get Cannot handle token prior to (time) error
. Use this if your app and Keycloak timings differ.
Enable local user processing. The guard will try to store the user in a local database. Default is false.
The user UUID in your JWT. Default is sub claim.
The email claim in your JWT. Default is email.
The firstname claim in your JWT. Default is given_name.
The lastname claim in your JWT. Default is family_name.
Testing
Commands
Docker
Test in docker with coverage after you extended to lib. Don't install xdebug locally 🐌
Create a new source JSON file
Source files are stored in tests/Data
Generated tokens are stored in tests/Data/tokens
All versions of keycloak-laravel-guard with dependencies
laravel/framework Version ^10.10|^11.0
firebase/php-jwt Version ^6.10
sinema/json-api-error-laravel Version ^0.4.0