From 2893107c7e2f15303cf1b5084960bbb81979ef41 Mon Sep 17 00:00:00 2001 From: christer kahasha <62720246+christer77@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:26:52 +0100 Subject: [PATCH] [FIX]Error Ocurred when attempting Microsoft's email oauth2 setup --- .env.example | 9 +++++++++ config/oauth2.php | 10 ++++++++++ modules/core/functions.php | 1 + modules/imap/functions.php | 3 +++ modules/imap/hm-imap.php | 3 ++- modules/nux/services.php | 4 ++-- modules/sievefilters/modules.php | 1 - tests/phpunit/modules/core/functions.php | 2 +- 8 files changed, 28 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index bdcd2c3341..fb2d6700f9 100644 --- a/.env.example +++ b/.env.example @@ -172,6 +172,15 @@ OUTLOOK_AUTH_URI=https://login.live.com/oauth20_authorize.srf OUTLOOK_TOKEN_URI=https://login.live.com/oauth20_token.srf OUTLOOK_REFRESH_URI=https://login.live.com/oauth20_token.srf + +#office365 +OFFICE365_CLIENT_ID= +OFFICE365_CLIENT_SECRET= +OFFICE365_CLIENT_URI=http://localhost/?page=home +OFFICE365_AUTH_URI=https://login.live.com/oauth20_authorize.srf +OFFICE365_TOKEN_URI=https://login.live.com/oauth20_token.srf +OFFICE365_REFRESH_URI=https://login.live.com/oauth20_token.srf + #ldap.php LDAP_SERVER=localhost LDAP_ENABLE_TLS=true diff --git a/config/oauth2.php b/config/oauth2.php index 35c3912803..21f5a812cc 100644 --- a/config/oauth2.php +++ b/config/oauth2.php @@ -39,4 +39,14 @@ 'token_uri' => env('OUTLOOK_TOKEN_URI', 'https://login.live.com/oauth20_token.srf'), 'refresh_uri' => env('OUTLOOK_REFRESH_URI', 'https://login.live.com/oauth20_token.srf') ], + + //[office365] + 'office365' => [ + 'client_id' => env('OFFICE365_CLIENT_ID', ''), + 'client_secret' => env('OFFICE365_CLIENT_SECRET', ''), + 'client_uri' => env('OFFICE365_CLIENT_URI', 'http://localhost/?page=home'), + 'auth_uri' => env('OFFICE365_AUTH_URI', 'https://login.live.com/oauth20_authorize.srf'), + 'token_uri' => env('OFFICE365_TOKEN_URI', 'https://login.live.com/oauth20_token.srf'), + 'refresh_uri' => env('OFFICE365_REFRESH_URI', 'https://login.live.com/oauth20_token.srf') + ], ]; diff --git a/modules/core/functions.php b/modules/core/functions.php index bea403918b..d4bc8f80ba 100644 --- a/modules/core/functions.php +++ b/modules/core/functions.php @@ -255,6 +255,7 @@ function get_oauth2_data($config) { return [ 'gmail' => $config->get('gmail',[]), 'outlook' => $config->get('outlook',[]), + 'office365' => $config->get('office365',[]), ]; }} diff --git a/modules/imap/functions.php b/modules/imap/functions.php index 24d6048a18..fd5935eb47 100644 --- a/modules/imap/functions.php +++ b/modules/imap/functions.php @@ -853,6 +853,9 @@ function imap_refresh_oauth2_token($server, $config) { elseif ($server['server'] == 'imap-mail.outlook.com') { $details = $oauth2_data['outlook']; } + elseif ($server['server'] == 'imap-mail.office365.com') { + $details = $oauth2_data['office365']; + } if (!empty($details)) { $oauth2 = new Hm_Oauth2($details['client_id'], $details['client_secret'], $details['client_uri']); $result = $oauth2->refresh_token($details['refresh_uri'], $server['refresh_token']); diff --git a/modules/imap/hm-imap.php b/modules/imap/hm-imap.php index fbb8dbaa89..017e383f76 100644 --- a/modules/imap/hm-imap.php +++ b/modules/imap/hm-imap.php @@ -204,6 +204,7 @@ public function connect($config) { $timeout = 10; $this->handle = Hm_Functions::stream_socket_client($this->server, $this->port, $errorno, $errorstr, $timeout, STREAM_CLIENT_CONNECT, $ctx); + if (is_resource($this->handle)) { $this->debug[] = 'Successfully opened port to the IMAP server'; $this->state = 'connected'; @@ -244,6 +245,7 @@ public function disconnect() { * @return bool true on successful login */ public function authenticate($username, $password) { + $this->get_capability(); if (!$this->tls) { $this->starttls(); @@ -320,7 +322,6 @@ public function authenticate($username, $password) { } return $authed; } - /** * attempt starttls diff --git a/modules/nux/services.php b/modules/nux/services.php index 39c97d0d91..c17ca633db 100644 --- a/modules/nux/services.php +++ b/modules/nux/services.php @@ -62,9 +62,9 @@ 'name' => 'Office365', 'scope' => 'wl.imap', 'smtp' => array( - 'server' => 'outlook.office365.com', + 'server' => 'smtp.office365.com', 'port' => 587, - 'tls'=> 'yes' + 'tls'=> false ) )); diff --git a/modules/sievefilters/modules.php b/modules/sievefilters/modules.php index 8949e994d6..16454fa509 100644 --- a/modules/sievefilters/modules.php +++ b/modules/sievefilters/modules.php @@ -1292,7 +1292,6 @@ function sieve_enabled_callback($val) { return $val; } class Hm_Output_enable_sieve_filter_setting extends Hm_Output_Module { protected function output() { $settings = $this->get('user_settings'); - // exit(var_dump($settings['enable_sieve_filter'])); if ((array_key_exists('enable_sieve_filter', $settings) && $settings['enable_sieve_filter']) || DEFAULT_ENABLE_SIEVE_FILTER) { $checked = ' checked="checked"'; $reset = ''; diff --git a/tests/phpunit/modules/core/functions.php b/tests/phpunit/modules/core/functions.php index 818cf4018e..a358f3f1f4 100644 --- a/tests/phpunit/modules/core/functions.php +++ b/tests/phpunit/modules/core/functions.php @@ -84,7 +84,7 @@ public function test_is_email_address() { */ public function test_get_oauth2_data() { $mock_config = new Hm_Mock_Config(); - $this->assertEquals(array('gmail' => [],'outlook' => []), (get_oauth2_data($mock_config))); + $this->assertEquals(array('gmail' => [],'outlook' => [],'office365' => []), (get_oauth2_data($mock_config))); } /** * @preserveGlobalState disabled