2026-10-25 18:20:21 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
/**
|
|
|
|
|
|
* SnapCAD — YakNet OAuth 2.0 Callback Endpoint (yaknet/y-auth SDK ile)
|
|
|
|
|
|
* Callback URL: https://snap.yakhub.com.tr/callback.php
|
|
|
|
|
|
*/
|
|
|
|
|
|
require_once __DIR__ . '/snap-config.php';
|
|
|
|
|
|
|
|
|
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
|
|
|
|
|
|
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$desktopCtx = $_SESSION['oauth2_desktop'] ?? null;
|
|
|
|
|
|
unset($_SESSION['oauth2_desktop']);
|
|
|
|
|
|
|
|
|
|
|
|
$desktopPort = 0;
|
|
|
|
|
|
$desktopNonce = '';
|
|
|
|
|
|
$desktopDevice = 'SnapCAD Masaüstü';
|
|
|
|
|
|
if (
|
|
|
|
|
|
is_array($desktopCtx)
|
|
|
|
|
|
&& !empty($desktopCtx['port'])
|
|
|
|
|
|
&& !empty($desktopCtx['nonce'])
|
|
|
|
|
|
&& ((int)($desktopCtx['issued_at'] ?? 0) <= 0 || (time() - (int)$desktopCtx['issued_at']) <= 900)
|
|
|
|
|
|
) {
|
|
|
|
|
|
$candidatePort = (int)$desktopCtx['port'];
|
|
|
|
|
|
$candidateNonce = strtolower(trim((string)$desktopCtx['nonce']));
|
|
|
|
|
|
if ($candidatePort >= 1024 && $candidatePort <= 65535 && preg_match('/^[a-f0-9]{32,128}$/', $candidateNonce)) {
|
|
|
|
|
|
$desktopPort = $candidatePort;
|
|
|
|
|
|
$desktopNonce = $candidateNonce;
|
|
|
|
|
|
$desktopDevice = (string)($desktopCtx['device'] ?? 'SnapCAD Masaüstü');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$redirectCallbackError = static function (string $message) use ($desktopPort, $desktopNonce): void {
|
|
|
|
|
|
if ($desktopPort > 0 && $desktopNonce !== '') {
|
|
|
|
|
|
header('Location: http://127.0.0.1:' . $desktopPort . '/oauth-callback?error=' . urlencode($message) . '&nonce=' . urlencode($desktopNonce));
|
|
|
|
|
|
exit;
|
|
|
|
|
|
}
|
|
|
|
|
|
header('Location: /?oauth_error=' . urlencode($message));
|
|
|
|
|
|
exit;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-10-25 18:20:21 +03:00
|
|
|
|
if (isset($_GET['error'])) {
|
|
|
|
|
|
$err = (string)($_GET['error_description'] ?? $_GET['error']);
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$redirectCallbackError($err);
|
2026-10-25 18:20:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$code = isset($_GET['code']) ? (string)$_GET['code'] : '';
|
|
|
|
|
|
$state = isset($_GET['state']) ? (string)$_GET['state'] : '';
|
|
|
|
|
|
|
|
|
|
|
|
if ($code === '') {
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$redirectCallbackError('Yetkilendirme kodu (code) alınamadı.');
|
2026-10-25 18:20:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// State her zaman zorunludur; eksik veya eski state kabul edilmez.
|
|
|
|
|
|
$expectedState = (string)($_SESSION['oauth2state'] ?? '');
|
|
|
|
|
|
$issuedAt = (int)($_SESSION['oauth2state_issued_at'] ?? 0);
|
|
|
|
|
|
unset($_SESSION['oauth2state'], $_SESSION['oauth2state_issued_at']);
|
|
|
|
|
|
|
|
|
|
|
|
if ($expectedState === '' || $state === '' || !hash_equals($expectedState, $state)) {
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$redirectCallbackError('Geçersiz OAuth state anahtarı. Lütfen tekrar deneyin.');
|
2026-10-25 18:20:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($issuedAt > 0 && (time() - $issuedAt) > 600) {
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$redirectCallbackError('Giriş isteğinin süresi doldu. Lütfen tekrar deneyin.');
|
2026-10-25 18:20:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
$provider = snapcad_get_oauth_provider();
|
|
|
|
|
|
|
|
|
|
|
|
/** @var \League\OAuth2\Client\Token\AccessToken $token */
|
|
|
|
|
|
$token = $provider->getAccessToken('authorization_code', [
|
|
|
|
|
|
'code' => $code,
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
/** @var \YakNet\YAuth\Provider\YakNetResourceOwner $owner */
|
|
|
|
|
|
$owner = $provider->getResourceOwner($token);
|
|
|
|
|
|
$raw = $owner->toArray();
|
|
|
|
|
|
|
|
|
|
|
|
$userId = (int)($owner->getId() ?? $raw['id'] ?? 0);
|
|
|
|
|
|
$userName = (string)($owner->getName() ?? $raw['name'] ?? 'YakNet Kullanıcısı');
|
|
|
|
|
|
$userEmail = (string)($owner->getEmail() ?? $raw['email'] ?? '');
|
|
|
|
|
|
$userAvatar = $raw['avatar'] ?? $raw['picture'] ?? null;
|
|
|
|
|
|
|
|
|
|
|
|
if ($userId <= 0) {
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$redirectCallbackError('Kullanıcı kimliği doğrulanamadı.');
|
2026-10-25 18:20:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Oturum sabitleme saldırısını engellemek için giriş sonrası kimliği yenile.
|
|
|
|
|
|
if (session_status() === PHP_SESSION_ACTIVE) {
|
|
|
|
|
|
session_regenerate_id(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$userRecord = [
|
2026-10-25 18:20:21 +03:00
|
|
|
|
'id' => $userId,
|
|
|
|
|
|
'name' => $userName,
|
|
|
|
|
|
'email' => $userEmail,
|
|
|
|
|
|
'avatar' => $userAvatar,
|
|
|
|
|
|
'auth_source' => 'yaknet_y_auth',
|
|
|
|
|
|
'synced_at' => date('Y-m-d H:i:s'),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$_SESSION['yaknet_token'] = $token->getToken();
|
|
|
|
|
|
$_SESSION['yaknet_user'] = $userRecord;
|
|
|
|
|
|
|
|
|
|
|
|
if ($desktopPort > 0 && $desktopNonce !== '') {
|
|
|
|
|
|
$pdo = snapcad_get_pdo();
|
|
|
|
|
|
if (!$pdo) {
|
|
|
|
|
|
$redirectCallbackError('YakNet Bulut veritabanına ulaşılamadı.');
|
|
|
|
|
|
}
|
|
|
|
|
|
$exchangeCode = snapcad_create_desktop_exchange($pdo, $userRecord, $desktopNonce, $desktopDevice);
|
|
|
|
|
|
header('Location: http://127.0.0.1:' . $desktopPort . '/oauth-callback?code=' . urlencode($exchangeCode) . '&nonce=' . urlencode($desktopNonce));
|
|
|
|
|
|
exit;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-25 18:20:21 +03:00
|
|
|
|
header('Location: /?cloud_login=1');
|
|
|
|
|
|
exit;
|
|
|
|
|
|
} catch (IdentityProviderException $e) {
|
|
|
|
|
|
error_log('[SnapCAD OAuth] Sağlayıcı hatası: ' . $e->getMessage());
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$redirectCallbackError('YakNet kimlik doğrulaması başarısız oldu. Lütfen tekrar deneyin.');
|
2026-10-25 18:20:21 +03:00
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
|
error_log('[SnapCAD OAuth] Giriş hatası: ' . $e->getMessage());
|
2026-11-12 10:12:35 +03:00
|
|
|
|
$redirectCallbackError('Giriş sırasında bir hata oluştu. Lütfen tekrar deneyin.');
|
2026-10-25 18:20:21 +03:00
|
|
|
|
}
|