PHPackages                             sdaoudi/oauth2-keycloak - 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. sdaoudi/oauth2-keycloak

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

sdaoudi/oauth2-keycloak
=======================

Keycloak OAuth 2.0 Client Provider for The PHP League OAuth2-Client

3.1.1(2y ago)035.0k↓17.3%4[2 PRs](https://github.com/sdaoudi/oauth2-keycloak/pulls)MITPHP

Since Aug 31Pushed 2y agoCompare

[ Source](https://github.com/sdaoudi/oauth2-keycloak)[ Packagist](https://packagist.org/packages/sdaoudi/oauth2-keycloak)[ RSS](/packages/sdaoudi-oauth2-keycloak/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (14)Used By (0)

Keycloak Provider for OAuth 2.0 Client
======================================

[](#keycloak-provider-for-oauth-20-client)

[![Latest Version](https://camo.githubusercontent.com/0a1f1c46e2b14ec523ab545aff0b20ebe85a8f14865a6bb18af8e54f0dcb2075/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73746576656e6d6167756972652f6f61757468322d6b6579636c6f616b2e7376673f7374796c653d666c61742d737175617265)](https://github.com/sdaoudi/oauth2-keycloak/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/bebd795b498d3646be33699a6f7e7af16fdeea3f1baede1453e42bc998a2657e/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7364616f7564692f6f61757468322d6b6579636c6f616b2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/sdaoudi/oauth2-keycloak)[![Coverage Status](https://camo.githubusercontent.com/106870f95b0c5aba73e067fecf90bebb9536b7436362a1dddf14266335ad0ddd/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f7364616f7564692f6f61757468322d6b6579636c6f616b2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/sdaoudi/oauth2-keycloak/code-structure)[![Quality Score](https://camo.githubusercontent.com/8438dcfe67ba5295d26a63d49e5f778b726757779642698dee9b3f811f3798ba/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7364616f7564692f6f61757468322d6b6579636c6f616b2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/sdaoudi/oauth2-keycloak)[![Total Downloads](https://camo.githubusercontent.com/63b8769f7d5339ecfb06bc39e71b38d2e205b631bcd4dbf45d7959cb87503440/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7364616f7564692f6f61757468322d6b6579636c6f616b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sdaoudi/oauth2-keycloak)

This package provides Keycloak OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).

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

[](#installation)

To install, use composer:

```
composer require sdaoudi/oauth2-keycloak

```

Usage
-----

[](#usage)

Usage is the same as The League's OAuth client, using `\Stevenmaguire\OAuth2\Client\Provider\Keycloak` as the provider.

Use `authServerUrl` to specify the Keycloak server URL. You can lookup the correct value from the Keycloak client installer JSON under `auth-server-url`, eg. `http://localhost:8080/auth`.

Use `realm` to specify the Keycloak realm name. You can lookup the correct value from the Keycloak client installer JSON under `resource`, eg. `master`.

### Authorization Code Flow

[](#authorization-code-flow)

```
$provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
    'authServerUrl'         => '{keycloak-server-url}',
    'realm'                 => '{keycloak-realm}',
    'clientId'              => '{keycloak-client-id}',
    'clientSecret'          => '{keycloak-client-secret}',
    'redirectUri'           => 'https://example.com/callback-url',
    'encryptionAlgorithm'   => 'RS256',                             // optional
    'encryptionKeyPath'     => '../key.pem'                         // optional
    'encryptionKey'         => 'contents_of_key_or_certificate'     // optional
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state, make sure HTTP sessions are enabled.');

} else {

    // Try to get an access token (using the authorization coe grant)
    try {
        $token = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);
    } catch (Exception $e) {
        exit('Failed to get access token: '.$e->getMessage());
    }

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the user's details
        $user = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $user->getName());

    } catch (Exception $e) {
        exit('Failed to get resource owner: '.$e->getMessage());
    }

    // Use this to interact with an API on the users behalf
    echo $token->getToken();
}
```

### Refreshing a Token

[](#refreshing-a-token)

```
$provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
    'authServerUrl'     => '{keycloak-server-url}',
    'realm'             => '{keycloak-realm}',
    'clientId'          => '{keycloak-client-id}',
    'clientSecret'      => '{keycloak-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
]);

$token = $provider->getAccessToken('refresh_token', ['refresh_token' => $token->getRefreshToken()]);
```

### Handling encryption

[](#handling-encryption)

If you've configured your Keycloak instance to use encryption, there are some advanced options available to you.

#### Configure the provider to use the same encryption algorithm

[](#configure-the-provider-to-use-the-same-encryption-algorithm)

```
$provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
    // ...
    'encryptionAlgorithm'   => 'RS256',
]);
```

or

```
$provider->setEncryptionAlgorithm('RS256');
```

#### Configure the provider to use the expected decryption public key or certificate

[](#configure-the-provider-to-use-the-expected-decryption-public-key-or-certificate)

##### By key value

[](#by-key-value)

```
$key = "-----BEGIN PUBLIC KEY-----\n....\n-----END PUBLIC KEY-----";
// or
// $key = "-----BEGIN CERTIFICATE-----\n....\n-----END CERTIFICATE-----";

$provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
    // ...
    'encryptionKey'   => $key,
]);
```

or

```
$provider->setEncryptionKey($key);
```

##### By key path

[](#by-key-path)

```
$keyPath = '../key.pem';

$provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
    // ...
    'encryptionKeyPath'   => $keyPath,
]);
```

or

```
$provider->setEncryptionKeyPath($keyPath);
```

Testing
-------

[](#testing)

```
$ ./vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/sdaoudi/oauth2-keycloak/blob/master/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Steven Maguire](https://github.com/stevenmaguire)
- [All Contributors](https://github.com/sdaoudi/oauth2-keycloak/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/sdaoudi/oauth2-keycloak/blob/master/LICENSE) for more information.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 57.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~336 days

Recently: every ~356 days

Total

10

Last Release

882d ago

Major Versions

0.2.0 → 1.x-dev2016-12-09

1.x-dev → 2.0.02017-01-26

2.2.0 → 3.0.02021-04-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/4258e4bcc4df372565f3aff30556471739017fa40b49cf4b6e800d40dc9831ba?d=identicon)[sdaoudi](/maintainers/sdaoudi)

---

Top Contributors

[![stevenmaguire](https://avatars.githubusercontent.com/u/1851973?v=4)](https://github.com/stevenmaguire "stevenmaguire (20 commits)")[![sdaoudi](https://avatars.githubusercontent.com/u/4227015?v=4)](https://github.com/sdaoudi "sdaoudi (9 commits)")[![raehalme](https://avatars.githubusercontent.com/u/3288306?v=4)](https://github.com/raehalme "raehalme (6 commits)")

---

Tags

clientoauthoauth2authorizationauthorisationkeycloak

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/sdaoudi-oauth2-keycloak/health.svg)

```
[![Health](https://phpackages.com/badges/sdaoudi-oauth2-keycloak/health.svg)](https://phpackages.com/packages/sdaoudi-oauth2-keycloak)
```

###  Alternatives

[stevenmaguire/oauth2-keycloak

Keycloak OAuth 2.0 Client Provider for The PHP League OAuth2-Client

2275.9M27](/packages/stevenmaguire-oauth2-keycloak)[patrickbussmann/oauth2-apple

Sign in with Apple OAuth 2.0 Client Provider for The PHP League OAuth2-Client

1132.5M6](/packages/patrickbussmann-oauth2-apple)[thenetworg/oauth2-azure

Azure Active Directory OAuth 2.0 Client Provider for The PHP League OAuth2-Client

2509.6M48](/packages/thenetworg-oauth2-azure)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
