https://github.com/drago-ex/keycloak
:key: Simple Keycloak adapter for easy integration.
https://github.com/drago-ex/keycloak
keycloak nette
Last synced: 4 months ago
JSON representation
:key: Simple Keycloak adapter for easy integration.
- Host: GitHub
- URL: https://github.com/drago-ex/keycloak
- Owner: drago-ex
- License: mit
- Created: 2022-07-25T09:28:59.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2025-12-30T08:03:00.000Z (6 months ago)
- Last Synced: 2026-01-09T02:36:51.895Z (6 months ago)
- Topics: keycloak, nette
- Language: PHP
- Homepage:
- Size: 89.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
## Drago Keycloak
Simple Keycloak adapter for easy integration.
[](https://raw.githubusercontent.com/drago-ex/keycloak/master/license.md)
[](https://badge.fury.io/ph/drago-ex%2Fkeycloak)
[](https://github.com/drago-ex/keycloak/actions/workflows/tests.yml)
[](https://github.com/drago-ex/keycloak/actions/workflows/coding-style.yml)
[](https://www.codefactor.io/repository/github/drago-ex/keycloak)
[](https://coveralls.io/github/drago-ex/keycloak?branch=master)
## Technology
- PHP 8.3 or higher
- composer
## Installation
```
composer require drago-ex/keycloak
```
## Extension registration in `config.neon`
```neon
extensions:
keycloak: Drago\Keycloak\DI\KeycloakExtension
keycloak:
# https://github.com/stevenmaguire/oauth2-keycloak
authServerUrl: keycloak-server-url
realm: keycloak-realm
clientId: keycloak-client-id
clientSecret: keycloak-client-secret
redirectUri: https://example.com/callback-url
# optional
# version: 21.0.1
# encryptionAlgorithm: 'RS256'
# encryptionKeyPath: '../key.pem'
# encryptionKey: 'contents_of_key_or_certificate'
# https://github.com/guzzle/guzzle
# guzzleHttp:
```
## Usage in Presenter
```php
use Drago\Keycloak\KeycloakAdapter;
public function __construct(
private Keycloak $keycloak,
private KeycloakSessions $keycloakSessions,
) {
parent::__construct();
}
// Simple login
protected function startup(): void
{
parent::startup();
if (!$this->getUser()->isLoggedIn()) {
$keycloakUser = $this->keycloakSessions->getItems()->resourceOwner;
$this->getUser()->login($keycloakUser->getName(), $keycloakUser->getId());
$this->redirect('redirect');
}
}
// Custom authentication with Keycloak attributes and backlink
protected function startup(): void
{
parent::startup();
if (!$this->getUser()->isLoggedIn()) {
$keycloakUser = $this->keycloakSessions->getItems()->resourceOwner;
try {
if ($keycloakUser) {
$user = $this->getUser();
// Custom authenticator
$user->setAuthenticator($this->authRepository);
// User login
$user->login($keycloakUser->getName(), $keycloakUser->getId());
// Backlink handling
$this->restoreRequest($this->backlink);
$this->redirect(':Backend:Admin:');
}
} catch (AuthenticationException $e) {
if ($e->getCode() === 1) {
$this->template->userLoginError = true;
$this->getUserLogout();
$redirect = $this->keycloak->getLogoutUrl();
header('refresh:6; url=' . $redirect);
}
}
}
}
// User logout
private function getUserLogout(): void
{
$this->getUser()->logout();
$this->keycloakSessions->remove();
}
```
### Error message in `@layout.latte`
```latte
{_'The user does not have the required attributes set in keycloak.'}
...
```
### Items from Keycloak
```php
// Get state, accessToken, and resource owner
$this->keycloakSessions->getItems();
```
## User Logout Method
```php
$this->keycloakSessions->remove();
$this->redirectUrl($this->keycloak->getLogoutUrl());
```