diff --git a/web/.env b/web/.env deleted file mode 100644 index 675657e..0000000 --- a/web/.env +++ /dev/null @@ -1,9 +0,0 @@ -OIDC_CLIENT_ID=fitness-oidc -OIDC_CLIENT_SECRET=egebuqzZfVnjjBG9JDQ8tjW3TzV9gJurvT7qfB6c2SX2nPBNeaGycDfx4788QzJd -OIDC_REDIRECT_URI=https://fitness.matt-cloud.com/pgapi/auth/oidc/callback -OIDC_AUTH_ENDPOINT=https://auth.matt-cloud.com/oidc/auth -OIDC_TOKEN_ENDPOINT=https://auth.matt-cloud.com/oidc/token -OIDC_USERINFO_ENDPOINT=https://auth.matt-cloud.com/oidc/userinfo -OIDC_LOGOUT_ENDPOINT=https://auth.matt-cloud.com/logout -# Optional: the default redirect after logout -OIDC_POST_LOGOUT_REDIRECT_URI=https://fitness.matt-cloud.com/ \ No newline at end of file diff --git a/web/auth.php b/web/auth.php new file mode 100644 index 0000000..a16cfec --- /dev/null +++ b/web/auth.php @@ -0,0 +1,27 @@ +setRedirectURL($config['redirect_uri']); + +// Tell it which scopes we want +$oidc->addScope($config['scopes']); + +// The OIDC flow we’ll use +$oidc->setResponseTypes([$config['response_type']]); + +// Optional: for local development you can relax TLS validation +// $oidc->setVerifyHost(false); +// $oidc->setVerifyPeer(false); + +$oidc->authenticate(); // This automatically redirects to Authelia \ No newline at end of file diff --git a/web/callback.php b/web/callback.php index bf058e1..7e667a3 100644 --- a/web/callback.php +++ b/web/callback.php @@ -1,40 +1,33 @@ setRedirectURL('https://fitness.matt-cloud.com/pgapi/auth/oidc/callback'); +$oidc->setRedirectURL($config['redirect_uri']); +$oidc->addScope($config['scopes']); +$oidc->setResponseTypes([$config['response_type']]); -try { - // Finish the auth flow – exchanges the code, validates the ID‑token, fetches userinfo - $oidc->authenticate(); +// This will read the `code` query parameter, fetch the tokens +// and validate everything. +$oidc->authenticate(); - // $oidc->idToken is a JWT object; you can cast to array if needed - $idToken = $oidc->idToken; - // Userinfo (includes groups because you requested the `groups` scope) - $userInfo = $oidc->getUserInfo(); // or $oidc->requestUserInfo('email') +// Fetch the OpenID Connect userinfo claims +$claims = $oidc->requestUserInfo(); // → stdClass with the claims - // Store what you need in the session - $_SESSION['user'] = [ - 'id_token' => $oidc->idToken, // JWT string - 'access_token'=> $oidc->accessToken, // opaque or JWT - 'refresh_token'=> $oidc->refreshToken, - 'expires_in' => $oidc->expiresIn, - 'claims' => (array)$idToken, // decoded claims - 'userinfo' => $userInfo, - ]; -} catch (Exception $e) { - http_response_code(500); - echo "Authentication failed: " . $e->getMessage(); - exit; -} +// Store what we need in the PHP session +$_SESSION['authenticated'] = true; +$_SESSION['name'] = $claims->name ?? $claims->given_name ?? $claims->family_name ?? ''; +$_SESSION['email'] = $claims->email ?? ''; +$_SESSION['groups'] = $claims->groups ?? []; -// Redirect to the site’s landing page -header('Location: /'); +// Redirect back to the home page +header('Location: ../'); exit; \ No newline at end of file diff --git a/web/composer.json b/web/composer.json index c08ba4a..ddab54b 100644 --- a/web/composer.json +++ b/web/composer.json @@ -1,8 +1,5 @@ { "require": { - "jumbojett/openid-connect-php": "^2.4", - "vlucas/phpdotenv": "^5.4", - "guzzlehttp/guzzle": "^7.0", - "firebase/php-jwt": "^7.0" + "jumbojett/openid-connect-php": "^1.0" } } \ No newline at end of file diff --git a/web/config.php b/web/config.php index 7495633..940909e 100644 --- a/web/config.php +++ b/web/config.php @@ -1,23 +1,19 @@ 'https://auth.matt-cloud.com', // <-- change if you run locally -session_start(); + // Client (your application) credentials + 'client_id' => 'fitness-oidc', + 'client_secret' => 'egebuqzZfVnjjBG9JDQ8tjW3TzV9gJurvT7qfB6c2SX2nPBNeaGycDfx4788QzJd', -/** - * Load environment variables. If you don't use phpdotenv, - * copy the values from .env directly here. - */ -$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); -$dotenv->load(); + // The callback URL **must** match exactly what is in Authelia + 'redirect_uri' => 'https://fitness.matt-cloud.com/pgapi/auth/oidc/callback', -/** - * Basic OIDC configuration – keep all secrets out of source‑control. - */ -define('OIDC_CLIENT_ID', getenv('OIDC_CLIENT_ID')); -define('OIDC_CLIENT_SECRET', getenv('OIDC_CLIENT_SECRET')); -define('OIDC_REDIRECT_URI', getenv('OIDC_REDIRECT_URI')); -define('OIDC_AUTH_ENDPOINT', getenv('OIDC_AUTH_ENDPOINT')); -define('OIDC_TOKEN_ENDPOINT', getenv('OIDC_TOKEN_ENDPOINT')); -define('OIDC_USERINFO_ENDPOINT', getenv('OIDC_USERINFO_ENDPOINT')); -define('OIDC_LOGOUT_ENDPOINT', getenv('OIDC_LOGOUT_ENDPOINT')); -define('OIDC_POST_LOGOUT_REDIRECT_URI', getenv('OIDC_POST_LOGOUT_REDIRECT_URI')); \ No newline at end of file + // Scopes we want – Authelia already provides “openid”, “profile”, “email”, “groups” + 'scopes' => ['openid', 'profile', 'email', 'groups'], + + // Response type & grant type + 'response_type' => 'code', +]; \ No newline at end of file diff --git a/web/index.php b/web/index.php index fb74276..d48fae8 100644 --- a/web/index.php +++ b/web/index.php @@ -1,19 +1,23 @@ - - -
Email: = htmlspecialchars($_SESSION['user']['userinfo']['email'] ?? '') ?>
-Groups: = htmlspecialchars(implode(', ', $_SESSION['user']['userinfo']['groups'] ?? [])) ?>
-JWT claims:
= htmlspecialchars(print_r($_SESSION['user']['claims'], true)) ?>+$config = require __DIR__ . '/../config.php'; - - - - \ No newline at end of file +if (!isset($_SESSION['authenticated'])) { + // User not logged in – show a login button + echo '