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:
johnnyq
2026-12-04 12:31:41 -05:00
parent b2d8863607
commit 80e6b724bb
6 changed files with 154 additions and 55 deletions

View File

@@ -62,7 +62,8 @@ if (isset($_POST['edit_mail_smtp_settings']) || isset($_POST['edit_mail_imap_set
* while Microsoft isn't picked), and the refresh token. No ID forgets the app:
* its secret and token go too. A changed app or token makes what ITFlow last
* signed in with useless, so that is fetched afresh when next needed.
* Connect Microsoft 365 saves the same way, then goes to Microsoft to sign in.
* Connect Microsoft 365 saves the same way, then goes to Microsoft to sign in,
* asking for the permissions The app has says it was given (functions/mail_graph.php).
*/
if (isset($_POST['edit_mail_oauth_settings']) || isset($_POST['oauth_connect_microsoft_mail'])) {
@@ -157,10 +158,14 @@ if (isset($_POST['edit_mail_oauth_settings']) || isset($_POST['oauth_connect_mic
$state = sha1(uniqid((string) mt_rand(), true));
}
// Graph's permissions or Exchange's, as the app was given - the callback redeems the sign-in for the same
$way = ($_POST['oauth_connect_way'] ?? '') === 'exchange' ? 'exchange' : 'graph';
$_SESSION['mail_oauth_state'] = $state;
$_SESSION['mail_oauth_state_expires_at'] = time() + 600;
$_SESSION['mail_oauth_way'] = $way;
$scope = 'offline_access openid profile https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/SMTP.Send';
$scope = mailMicrosoftConnectScope($way);
$authorize_url = MICROSOFT_OAUTH_BASE_URL . rawurlencode($config_mail_oauth_tenant_id) . '/oauth2/v2.0/authorize?'
. http_build_query([
@@ -170,10 +175,11 @@ if (isset($_POST['edit_mail_oauth_settings']) || isset($_POST['oauth_connect_mic
'response_mode' => 'query',
'scope' => $scope,
'state' => $state,
'prompt' => 'consent',
// Which account to sign in as - not the consent screen, which a mailbox that may not approve apps can't get past even when an admin has
'prompt' => 'select_account',
], '', '&', PHP_QUERY_RFC3986);
logAudit("Settings", "Edit", "$session_name started connecting Microsoft 365 for mail");
logAudit("Settings", "Edit", "$session_name started connecting Microsoft 365 for mail" . ($way === 'graph' ? ' through Microsoft Graph' : ' by SMTP and IMAP'));
redirect($authorize_url);
}
@@ -473,17 +479,22 @@ if (isset($_POST['test_oauth_token_refresh'])) {
redirect($mail_tab_redirect);
}
$token_url = 'https://oauth2.googleapis.com/token';
// Microsoft 365: through Graph for what the app has its permission for, SMTP and IMAP only for the rest (functions/mail_graph.php)
if ($provider === 'microsoft_oauth') {
$token_url = MICROSOFT_OAUTH_BASE_URL . rawurlencode($oauth_tenant_id) . "/oauth2/v2.0/token";
$check = mailMicrosoftCheck($config_smtp_provider === 'microsoft_oauth', $config_imap_provider === 'microsoft_oauth');
logAudit("Settings", "Edit", "$session_name tested OAuth token refresh for Microsoft 365 mail settings");
flashAlert(($check['ok'] ? 'OAuth token refresh successful for Microsoft 365. ' : '<strong>OAuth token test failed:</strong> ') . escapeHtml($check['message']), $check['ok'] ? 'success' : 'error');
redirect($mail_tab_redirect);
}
$token_url = 'https://oauth2.googleapis.com/token';
$post_fields = http_build_query([
'client_id' => $oauth_client_id,
'client_secret' => $oauth_client_secret,
'refresh_token' => $oauth_refresh_token,
'grant_type' => 'refresh_token',
] + ($provider === 'microsoft_oauth' ? ['scope' => MAIL_MICROSOFT_EXCHANGE_SCOPE] : []));
]);
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -520,21 +531,8 @@ if (isset($_POST['test_oauth_token_refresh'])) {
mysqli_query($mysqli, "UPDATE settings SET " . mailSecretSql('config_mail_oauth_access_token', $new_access_token) . ", config_mail_oauth_access_token_expires_at = '$new_expires_at_esc'$refresh_sql WHERE company_id = 1");
$provider_label = $provider === 'microsoft_oauth' ? 'Microsoft 365' : 'Google Workspace';
logAudit("Settings", "Edit", "$session_name tested OAuth token refresh for $provider_label mail settings");
logAudit("Settings", "Edit", "$session_name tested OAuth token refresh for Google Workspace mail settings");
// And what Microsoft Graph can do for the mailbox now - asked afresh (functions/mail_graph.php)
$graph_said = '';
if ($provider === 'microsoft_oauth') {
try {
$graph_send = mailGraphToken('Mail.Send', true) !== null;
$graph_read = mailGraphToken('Mail.ReadWrite') !== null;
$graph_said = ' ' . mailGraphSays($config_smtp_provider === 'microsoft_oauth', $config_imap_provider === 'microsoft_oauth', $graph_send, $graph_read);
} catch (\Throwable $e) {
$graph_said = ' ' . escapeHtml($e->getMessage());
}
}
flashAlert("OAuth token refresh successful for $provider_label. Access token expires at $new_expires_at." . $graph_said);
flashAlert("OAuth token refresh successful for Google Workspace. Access token expires at $new_expires_at.");
redirect($mail_tab_redirect);
}