https://github.com/drago-ex/keycloak
:key: Simple Keycloak adapter for easy integration.
https://github.com/drago-ex/keycloak
keycloak nette
Last synced: 2 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 3 years ago)
- Default Branch: master
- Last Pushed: 2025-01-27T12:41:04.000Z (5 months ago)
- Last Synced: 2025-03-19T19:49:33.768Z (3 months ago)
- Topics: keycloak, nette
- Language: PHP
- Homepage:
- Size: 80.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
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\KeycloakExtensionkeycloak:
# 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());
```