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

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

shiftby/oauth2-keycloak
=======================

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

2.2.2(5y ago)0355MITPHP

Since Aug 31Pushed 5y agoCompare

[ Source](https://github.com/shiftby/oauth2-keycloak)[ Packagist](https://packagist.org/packages/shiftby/oauth2-keycloak)[ RSS](/packages/shiftby-oauth2-keycloak/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (5)Versions (10)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/stevenmaguire/oauth2-keycloak/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/ce4ebd1d6c2ed15fd2b2fda8648a470808f47c5e13b90d9bdbd224894b3ddc05/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f73746576656e6d6167756972652f6f61757468322d6b6579636c6f616b2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/stevenmaguire/oauth2-keycloak)[![Coverage Status](https://camo.githubusercontent.com/14af7cc266493a2f95dc2dbb622b7480a9412c0a28a229081f658ad4ba2d2b00/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f73746576656e6d6167756972652f6f61757468322d6b6579636c6f616b2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/stevenmaguire/oauth2-keycloak/code-structure)[![Quality Score](https://camo.githubusercontent.com/d532e76bb039df331f2dfc48e37c818871f61c48f16810d9de4ef6edcb57bc7f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f73746576656e6d6167756972652f6f61757468322d6b6579636c6f616b2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/stevenmaguire/oauth2-keycloak)[![Total Downloads](https://camo.githubusercontent.com/062f79eea38f4e48211849a428d4e4ea40f9491fee85e29011b0b81d8a8793c5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746576656e6d6167756972652f6f61757468322d6b6579636c6f616b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stevenmaguire/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 stevenmaguire/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/stevenmaguire/oauth2-keycloak/blob/master/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

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

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 65.6% 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 ~290 days

Recently: every ~379 days

Total

8

Last Release

1875d ago

Major Versions

0.2.0 → 1.x-dev2016-12-09

1.x-dev → 2.0.02017-01-26

### Community

Maintainers

![](https://www.gravatar.com/avatar/958238939e5913bb414d77b1e7b2ac1696712af71d15f14df2e47e5cc8beae17?d=identicon)[shiftby](/maintainers/shiftby)

---

Top Contributors

[![stevenmaguire](https://avatars.githubusercontent.com/u/1851973?v=4)](https://github.com/stevenmaguire "stevenmaguire (21 commits)")[![raehalme](https://avatars.githubusercontent.com/u/3288306?v=4)](https://github.com/raehalme "raehalme (6 commits)")[![mstefan21](https://avatars.githubusercontent.com/u/22791905?v=4)](https://github.com/mstefan21 "mstefan21 (2 commits)")[![bastnic](https://avatars.githubusercontent.com/u/84887?v=4)](https://github.com/bastnic "bastnic (1 commits)")[![jgdevweb](https://avatars.githubusercontent.com/u/32622953?v=4)](https://github.com/jgdevweb "jgdevweb (1 commits)")[![MaximDovk](https://avatars.githubusercontent.com/u/15961558?v=4)](https://github.com/MaximDovk "MaximDovk (1 commits)")

---

Tags

clientoauthoauth2authorizationauthorisationkeycloak

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/shiftby-oauth2-keycloak/health.svg)](https://phpackages.com/packages/shiftby-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)
