setRedirectURL($redirectUrl); $oidc->addScope(['openid', 'profile', 'email']); // Complete the OAuth2 flow (exchange code for tokens) $oidc->authenticate(); // Get user information from Keycloak $userInfo = $oidc->requestUserInfo(); // Restore error reporting error_reporting($oldErrorReporting); // Extract email from Keycloak response $email = $userInfo->email ?? null; if (!$email) { throw new \Exception('Keycloak did not return an email address.'); } // Find admin user in Vvveb database using Vvveb API // Only get active admins (status = 1) $adminInfo = Admin::get([ 'email' => $email, 'status' => 1, ]); if (!$adminInfo) { // User not found or not active - redirect to login with error header('Location: /admin/index.php?module=user/login&error=' . urlencode("The email {$email} is not registered as an active admin in Vvveb.")); exit; } // Create Vvveb admin session (same way Admin::login() does it) $this->session->regenerateId(true); // Remove password hash from session data if (isset($adminInfo['password'])) { unset($adminInfo['password']); } // Set admin session (namespace 'admin' is used by Vvveb\System\User\Admin) $this->session->set('admin', $adminInfo); // Check if there's a redirect URL stored in session (from auto-login) $redirectUrl = $this->session->get('keycloak_redirect'); if ($redirectUrl) { // Remove redirect URL from session $this->session->delete('keycloak_redirect'); // Redirect to original page header('Location: ' . $redirectUrl); } else { // Redirect to admin dashboard $adminPath = \Vvveb\adminPath() ?: '/admin/'; header('Location: ' . $adminPath . 'index.php'); } exit; } catch (\Exception $e) { // Restore error reporting error_reporting($oldErrorReporting); // Log error if logging is available if (function_exists('error_log')) { error_log('Keycloak SSO Error: ' . $e->getMessage()); } // Redirect to login page with error header('Location: /admin/index.php?module=user/login&error=' . urlencode('Keycloak authentication failed: ' . $e->getMessage())); exit; } } }