stolfam/keycloak-adapter-php

Easy-to-use PHP adapter for Keycloak authentication

2.0.20 2024-10-29 14:16 UTC

This package is auto-updated.

Last update: 2024-10-29 14:17:03 UTC


README

Latest Stable Version Total Downloads License Monthly Downloads

Install

composer require ataccama/keycloak-adapter

Neon config:

parameters:
    keycloak:
        realmId: your_realm
        clientDd: your_client_id
        host: https://your.keycloak.com
        defaultRedirectUri: https://your.default.url
        api:
            username: your_username
            password: your_password
            clientId: your_api_client_id
            clientSecret: your_client_secret
            
services:
    - Ataccama\Adapters\Keycloak(%keycloak%)

Use

Create new class and extend class Ataccama\Auth, then you MUST implement all missing methods with your own logic.

Login URL: $loginUrl = $yourAuthClass->getLoginUrl()

In code use your class like this: $yourAuthClass->authorize($_GET['code'])

Example (Nette Framework):

// waiting for authorization code from Keycloak
if ($yourAuthClass->authorize($this->getParameter('code'))) {
    $this->redirectUrl($yourAuthClass->getRedirectUri());
} else {
    if (!$yourAuthClass->isAuthorized()) {
        $this->redirectUrl($yourAuthClass->getLoginUrl());
    }
}

// check if user is logged in on every page, if not redirect him to Keycloak login page
if (!$yourAuthClass->isAuthorized()) {
    $this->redirectUrl($yourAuthClass->getLoginUrl());
}