PHPackages                             mainick/keycloak-client-bundle - 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. mainick/keycloak-client-bundle

ActiveSymfony-bundle[Authentication &amp; Authorization](/categories/authentication)

mainick/keycloak-client-bundle
==============================

Keycloak client bundle for Symfony, designed to simplify Keycloak integration into your application and provide additional functionality for token management and user information access

4.0.3(2mo ago)4526.0k↑31.3%14[4 issues](https://github.com/mainick/KeycloakClientBundle/issues)MITPHPPHP &gt;=8.4CI passing

Since Oct 3Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/mainick/KeycloakClientBundle)[ Packagist](https://packagist.org/packages/mainick/keycloak-client-bundle)[ RSS](/packages/mainick-keycloak-client-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (19)Versions (36)Used By (0)

KeycloakClientBundle
====================

[](#keycloakclientbundle)

[![Latest Version](https://camo.githubusercontent.com/376a5f20804a46b388d35026bc6a6c8685402537c80c4e9e9d855893e627780f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6d61696e69636b2f4b6579636c6f616b436c69656e7442756e646c652e7376673f7374796c653d666c61742d737175617265)](https://github.com/mainick/KeycloakClientBundle/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/02e1b48a38c4b8e6af7ff56f49d32feb66937b130051658cc239b5b7e6c5bb84/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61696e69636b2f6b6579636c6f616b2d636c69656e742d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mainick/keycloak-client-bundle)

The `KeycloakClientBundle` bundle is a wrapper for the `stevenmaguire/oauth2-keycloak` package, designed to simplify Keycloak integration into your application in Symfony and provide additional functionality for token management and user information access. It also includes a listener to verify the token on every request.

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

[](#configuration)

Before installing this package, you need to configure it manually. You can do this by creating a `mainick_keycloak_client.yaml` file in the `config/packages` directory of your project and adding the following configuration:

```
# config/packages/mainick_keycloak_client.yaml

mainick_keycloak_client:
  keycloak:
    verify_ssl: '%env(bool:IAM_VERIFY_SSL)%'
    base_url: '%env(IAM_BASE_URL)%'
    realm: '%env(IAM_REALM)%'
    client_id: '%env(IAM_CLIENT_ID)%'
    client_secret: '%env(IAM_CLIENT_SECRET)%'
    redirect_uri: '%env(IAM_REDIRECT_URI)%'
    encryption_algorithm: '%env(IAM_ENCRYPTION_ALGORITHM)%'
    encryption_key: '%env(IAM_ENCRYPTION_KEY)%'
    encryption_key_path: '%env(IAM_ENCRYPTION_KEY_PATH)%'
    version: '%env(IAM_VERSION)%'
    # Optional: Whitelist of allowed domains for JWKS endpoint (security feature)
    # If not specified, only the domain from base_url is allowed
    allowed_jwks_domains:
      - 'keycloak.example.com'
      - '*.auth.example.com'  # Supports wildcard subdomains
```

Additionally, it's recommended to add the following environment variables to your project's environment file (e.g., `.env` or `.env.local`) with the appropriate values for your configuration:

```
###> mainick/keycloak-client-bundle ###
IAM_VERIFY_SSL=true # Verify SSL certificate
IAM_BASE_URL=''  # Keycloak server URL
IAM_REALM='' # Keycloak realm name
IAM_CLIENT_ID='' # Keycloak client id
IAM_CLIENT_SECRET='' # Keycloak client secret
IAM_REDIRECT_URI='' # Keycloak redirect uri
IAM_ENCRYPTION_ALGORITHM='' # RS256, HS256, JWKS, etc.
IAM_ENCRYPTION_KEY='' # public key
IAM_ENCRYPTION_KEY_PATH='' # public key path
IAM_VERSION='' # Keycloak version
###< mainick/keycloak-client-bundle ###
```

Make sure to replace the placeholder values with your actual configuration values. Once you have configured the package and environment variables, you can proceed with the installation.

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

[](#installation)

You can install this package using [Composer](http://getcomposer.org/):

```
composer require mainick/keycloak-client-bundle

```

Then, enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:

```
// config/bundles.php

return [
    // ...
    Mainick\KeycloakClientBundle\MainickKeycloakClientBundle::class => ['all' => true],
];
```

By configuring the package before installation, you ensure that it will be ready to use once installed.

Usage
-----

[](#usage)

### Get the Keycloak client

[](#get-the-keycloak-client)

You can get the Keycloak client by injecting the `Mainick\KeycloakClientBundle\Interface\IamClientInterface`interface in your controller or service.

To use it, you need to add the following configuration to your `config/services.yaml` file:

```
services:
    Mainick\KeycloakClientBundle\Interface\IamClientInterface:
        alias: Mainick\KeycloakClientBundle\Provider\KeycloakClient
```

Then, you can use it in your controller or service:

```
