Sozlash
<?php
// config.php
session_start();
const GXID_ISSUER = 'https://myid.goxost.net';
const GXID_CLIENT_ID = 'gxid_01JD3W8V6Q2N9F4K7T5R0YHXBM';
const GXID_REDIRECT_URI = 'https://example.com/callback.php';
function gxid_secret(): string
{
return getenv('GXID_CLIENT_SECRET'); // never commit the secret
}
function b64url(string $bytes): string
{
return rtrim(strtr(base64_encode($bytes), '+/', '-_'), '=');
}
Kirishni boshlash
<?php
// login.php
require __DIR__ . '/config.php';
$_SESSION['gxid'] = [
'state' => b64url(random_bytes(32)),
'nonce' => b64url(random_bytes(32)),
'verifier' => b64url(random_bytes(48)),
];
header('Location: ' . GXID_ISSUER . '/oauth/authorize?' . http_build_query([
'response_type' => 'code',
'client_id' => GXID_CLIENT_ID,
'redirect_uri' => GXID_REDIRECT_URI,
'scope' => 'openid profile email',
'state' => $_SESSION['gxid']['state'],
'nonce' => $_SESSION['gxid']['nonce'],
'code_challenge' => b64url(hash('sha256', $_SESSION['gxid']['verifier'], true)),
'code_challenge_method' => 'S256',
], '', '&', PHP_QUERY_RFC3986));
exit;
Callbackni qayta ishlash
<?php
// callback.php
require __DIR__ . '/config.php';
$flow = $_SESSION['gxid'] ?? null;
unset($_SESSION['gxid']);
if (! $flow || ! hash_equals($flow['state'], (string) ($_GET['state'] ?? ''))) {
http_response_code(400);
exit('Invalid state');
}
if (isset($_GET['error'])) {
exit('Sign-in was cancelled.');
}
$ch = curl_init(GXID_ISSUER . '/oauth/token');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_USERPWD => rawurlencode(GXID_CLIENT_ID) . ':' . rawurlencode(gxid_secret()), // client_secret_basic
CURLOPT_POSTFIELDS => http_build_query([
'grant_type' => 'authorization_code',
'code' => (string) $_GET['code'],
'redirect_uri' => GXID_REDIRECT_URI,
'code_verifier' => $flow['verifier'],
]),
]);
$tokens = json_decode((string) curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($status !== 200) {
exit('Sign-in failed: ' . htmlspecialchars($tokens['error'] ?? 'unknown'));
}
// The ID token came straight from the token endpoint over HTTPS,
// so OpenID Connect allows skipping the signature; the claims must still be checked.
$claims = json_decode(base64_decode(strtr(explode('.', $tokens['id_token'])[1], '-_', '+/')), true);
$valid = $claims['iss'] === GXID_ISSUER
&& in_array(GXID_CLIENT_ID, (array) $claims['aud'], true)
&& $claims['exp'] > time() - 60
&& hash_equals($flow['nonce'], (string) ($claims['nonce'] ?? ''));
if (! $valid) {
http_response_code(400);
exit('Invalid ID token');
}
session_regenerate_id(true);
$_SESSION['user'] = [
'sub' => $claims['sub'], // link your user by this
'email' => $claims['email'] ?? null,
'name' => $claims['name'] ?? null,
];
header('Location: /');
Imzoni tekshirish
RS256 imzosini ham tekshirish uchun firebase/php-jwt dan foydalaning:
use Firebase\JWT\JWK;
use Firebase\JWT\JWT;
$jwks = json_decode(file_get_contents(GXID_ISSUER . '/oauth/jwks'), true); // cache this
JWT::$leeway = 60;
$claims = (array) JWT::decode($tokens['id_token'], JWK::parseKeySet($jwks, 'RS256'));
// then check iss, aud and nonce exactly as above