Files
Fociz/auth/oauth_microsoft.php
T

44 lines
1.5 KiB
PHP

<?php
/**
* Startpunt voor Microsoft 365 OAuth 2.0 Flow
*/
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/integrations/microsoft.php';
$user = require_auth();
$db = get_db_connection();
// Haal eventueel opgeslagen client_id en tenant_id op uit database
$stmt = $db->prepare("SELECT settings FROM integrations WHERE user_id = ? AND provider = 'OUTLOOK'");
$stmt->execute([$user['id']]);
$row = $stmt->fetch();
$savedSettings = json_decode($row['settings'] ?? '{}', true) ?: [];
$clientId = !empty($_GET['client_id'])
? trim($_GET['client_id'])
: (!empty($savedSettings['client_id']) ? $savedSettings['client_id'] : fociz_env('MICROSOFT_CLIENT_ID', ''));
$tenantId = !empty($_GET['tenant_id'])
? trim($_GET['tenant_id'])
: (!empty($savedSettings['tenant_id']) ? $savedSettings['tenant_id'] : fociz_env('MICROSOFT_TENANT_ID', 'common'));
if (empty($clientId)) {
header('Location: /integrations.php?error=' . urlencode('Vul eerst de Application (Client) ID en het Client Secret in bij de Outlook instellingen om te koppelen met Microsoft 365.'));
exit;
}
// Bepaal de dynamische of geconfigureerde redirect URI
$redirectUri = rtrim(APP_URL, '/') . '/auth/callback_microsoft.php';
// Genereer unieke CSRF state
$state = bin2hex(random_bytes(16));
$_SESSION['ms_oauth_state'] = $state;
$_SESSION['ms_oauth_user_id'] = $user['id'];
// Stuur gebruiker door naar Microsoft login
$authUrl = microsoft_get_auth_url($clientId, $redirectUri, $tenantId, $state);
header("Location: {$authUrl}");
exit;