Files
snapcad-studio/callback.php
T
Enes Yakıştır ee28672e23
Build and Push Docker Images / build (push) Canceled after 0s
Desktop Release / Build linux desktop (push) Canceled after 0s
Desktop Release / Build windows desktop (push) Canceled after 0s
Desktop Release / Attach packages to release (push) Canceled after 0s
Desktop Release / Build macos desktop (push) Canceled after 0s
CI / Typecheck and unit tests (push) Canceled after 0s
release: SnapCAD Studio 1.0.10 (desktop menuler, macOS kisayollari, YakNet SSO)
2026-11-12 10:12:35 +03:00

122 lines
4.4 KiB
PHP

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
$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;
};
if (isset($_GET['error'])) {
$err = (string)($_GET['error_description'] ?? $_GET['error']);
$redirectCallbackError($err);
}
$code = isset($_GET['code']) ? (string)$_GET['code'] : '';
$state = isset($_GET['state']) ? (string)$_GET['state'] : '';
if ($code === '') {
$redirectCallbackError('Yetkilendirme kodu (code) alınamadı.');
}
// 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)) {
$redirectCallbackError('Geçersiz OAuth state anahtarı. Lütfen tekrar deneyin.');
}
if ($issuedAt > 0 && (time() - $issuedAt) > 600) {
$redirectCallbackError('Giriş isteğinin süresi doldu. Lütfen tekrar deneyin.');
}
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) {
$redirectCallbackError('Kullanıcı kimliği doğrulanamadı.');
}
// Oturum sabitleme saldırısını engellemek için giriş sonrası kimliği yenile.
if (session_status() === PHP_SESSION_ACTIVE) {
session_regenerate_id(true);
}
$userRecord = [
'id' => $userId,
'name' => $userName,
'email' => $userEmail,
'avatar' => $userAvatar,
'auth_source' => 'yaknet_y_auth',
'synced_at' => date('Y-m-d H:i:s'),
];
$_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;
}
header('Location: /?cloud_login=1');
exit;
} catch (IdentityProviderException $e) {
error_log('[SnapCAD OAuth] Sağlayıcı hatası: ' . $e->getMessage());
$redirectCallbackError('YakNet kimlik doğrulaması başarısız oldu. Lütfen tekrar deneyin.');
} catch (Throwable $e) {
error_log('[SnapCAD OAuth] Giriş hatası: ' . $e->getMessage());
$redirectCallbackError('Giriş sırasında bir hata oluştu. Lütfen tekrar deneyin.');
}