mirror of
https://github.com/itflow-org/itflow
synced 2026-12-05 10:39:00 +00:00
Microsoft 365 mail connects with the permissions the app has: The app has on the OAuth tab picks Microsoft Graph (graph.microsoft.com/.default) or Exchange's IMAP and SMTP scopes, Connect asks for the account instead of forcing the consent screen, the callback redeems for the same and keeps only an Exchange token as the SMTP/IMAP sign-in, and Connect's return and Test OAuth Token Refresh check Graph first and SMTP/IMAP only for what Graph lacks; 26.12.6 changelog
This commit is contained in:
@@ -18,8 +18,10 @@ $error_description = escapeSql($_GET['error_description'] ?? '');
|
||||
|
||||
$session_state = $_SESSION['mail_oauth_state'] ?? '';
|
||||
$session_state_expires = intval($_SESSION['mail_oauth_state_expires_at'] ?? 0);
|
||||
// Graph's permissions or Exchange's - what Connect asked Microsoft for (functions/mail_graph.php)
|
||||
$way = ($_SESSION['mail_oauth_way'] ?? '') === 'graph' ? 'graph' : 'exchange';
|
||||
|
||||
unset($_SESSION['mail_oauth_state'], $_SESSION['mail_oauth_state_expires_at']);
|
||||
unset($_SESSION['mail_oauth_state'], $_SESSION['mail_oauth_state_expires_at'], $_SESSION['mail_oauth_way']);
|
||||
|
||||
if (!empty($error)) {
|
||||
$msg = "Microsoft OAuth authorization failed: $error";
|
||||
@@ -48,56 +50,44 @@ if (defined('BASE_URL') && !empty(BASE_URL)) {
|
||||
}
|
||||
|
||||
$redirect_uri = $base_url . '/admin/oauth_microsoft_mail_callback.php';
|
||||
$token_url = 'https://login.microsoftonline.com/' . rawurlencode($config_mail_oauth_tenant_id) . '/oauth2/v2.0/token';
|
||||
$scope = 'offline_access openid profile https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/SMTP.Send';
|
||||
|
||||
$ch = curl_init($token_url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
||||
// Redeemed for what was asked for - a sign-in to one API at a time
|
||||
$response = mailGraphHttp('POST', MAIL_MICROSOFT_LOGIN_URL . rawurlencode($config_mail_oauth_tenant_id) . '/oauth2/v2.0/token', [], [
|
||||
'client_id' => $config_mail_oauth_client_id,
|
||||
'client_secret' => $config_mail_oauth_client_secret,
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code,
|
||||
'redirect_uri' => $redirect_uri,
|
||||
'scope' => $scope,
|
||||
], '', '&'));
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
|
||||
'scope' => mailMicrosoftConnectScope($way),
|
||||
], 20);
|
||||
|
||||
$raw_body = curl_exec($ch);
|
||||
$curl_err = curl_error($ch);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($raw_body === false || $http_code < 200 || $http_code >= 300) {
|
||||
$reason = !empty($curl_err) ? $curl_err : "HTTP $http_code";
|
||||
flashAlert("Microsoft OAuth token exchange failed: $reason", 'error');
|
||||
redirect($settings_mail_path);
|
||||
}
|
||||
|
||||
$json = json_decode($raw_body, true);
|
||||
if (!is_array($json) || empty($json['refresh_token']) || empty($json['access_token'])) {
|
||||
flashAlert("Microsoft OAuth token exchange failed: refresh token or access token missing.", 'error');
|
||||
$json = $response['json'];
|
||||
if (empty($json['refresh_token']) || empty($json['access_token'])) {
|
||||
flashAlert(escapeHtml($response['status'] >= 200 && $response['status'] < 300
|
||||
? 'Microsoft OAuth token exchange failed: refresh token or access token missing.'
|
||||
: mailGraphError($response, 'Microsoft OAuth token exchange')), 'error');
|
||||
redirect($settings_mail_path);
|
||||
}
|
||||
|
||||
$refresh_token = (string) $json['refresh_token'];
|
||||
$access_token = (string) $json['access_token'];
|
||||
$expires_at = date('Y-m-d H:i:s', time() + intval($json['expires_in'] ?? 3600));
|
||||
|
||||
$expires_at_esc = mysqli_real_escape_string($mysqli, $expires_at);
|
||||
// An SMTP and IMAP sign-in is kept as what they sign in with - a Graph one isn't theirs, so they get their own when needed
|
||||
$access_token = $way === 'exchange' ? (string) $json['access_token'] : '';
|
||||
$expires_at_sql = $way === 'exchange' ? "'" . date('Y-m-d H:i:s', time() + intval($json['expires_in'] ?? 3600)) . "'" : 'NULL';
|
||||
|
||||
// Only the token - Sending and Receiving stay as they were set; connecting for one doesn't turn on the other. Sealed (functions/mail_secret.php)
|
||||
mysqli_query($mysqli, "UPDATE settings SET
|
||||
" . mailSecretSql('config_mail_oauth_refresh_token', $refresh_token) . ",
|
||||
" . mailSecretSql('config_mail_oauth_access_token', $access_token) . ",
|
||||
config_mail_oauth_access_token_expires_at = '$expires_at_esc'
|
||||
config_mail_oauth_access_token_expires_at = $expires_at_sql
|
||||
WHERE company_id = 1
|
||||
");
|
||||
|
||||
// What Microsoft Graph can do is asked afresh with the new sign-in (functions/mail_graph.php)
|
||||
// What Microsoft Graph can do is asked afresh with the new sign-in, and SMTP and IMAP for the rest (functions/mail_graph.php)
|
||||
mailGraphForget();
|
||||
$check = mailMicrosoftCheck($config_smtp_provider === 'microsoft_oauth', $config_imap_provider === 'microsoft_oauth', false);
|
||||
|
||||
logAudit("Settings", "Edit", "$session_name connected Microsoft 365 for mail");
|
||||
flashAlert("Microsoft 365 connected - ITFlow can now use the mailbox");
|
||||
logAudit("Settings", "Edit", "$session_name connected Microsoft 365 for mail" . ($way === 'graph' ? ' through Microsoft Graph' : ' by SMTP and IMAP'));
|
||||
flashAlert($check['ok']
|
||||
? 'Microsoft 365 connected. ' . escapeHtml($check['message'])
|
||||
: '<strong>Microsoft 365 is connected, but email can\'t go yet:</strong> ' . escapeHtml($check['message']), $check['ok'] ? 'success' : 'warning');
|
||||
redirect($settings_mail_path);
|
||||
|
||||
Reference in New Issue
Block a user