From 5f8cb09d16b9aba617ed9848e4aa15767a111e81 Mon Sep 17 00:00:00 2001 From: renovatebot Date: Fri, 19 Jan 2024 15:36:01 +0100 Subject: [PATCH] Maintenance: Use Net::SMTP authenticator API for XOauth2 support. --- Gemfile | 1 - Gemfile.lock | 5 +---- lib/core_ext/net/smtp/auth_xoauth2.rb | 21 +++++++++++++++++++++ spec/lib/email_helper/probe_spec.rb | 2 +- spec/support/imap.rb | 2 ++ 5 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 lib/core_ext/net/smtp/auth_xoauth2.rb diff --git a/Gemfile b/Gemfile index 6e9791f6f49d..0e339045c2f5 100644 --- a/Gemfile +++ b/Gemfile @@ -113,7 +113,6 @@ gem 'omniauth-weibo-oauth2', git: 'https://github.com/zammad-deps/omniauth-weibo gem 'rack-attack' # channels -gem 'gmail_xoauth' gem 'koala' gem 'telegram-bot-ruby' gem 'twitter' diff --git a/Gemfile.lock b/Gemfile.lock index 512296573782..91d2aaa4026a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -261,8 +261,6 @@ GEM gli (2.21.1) globalid (1.2.1) activesupport (>= 6.1) - gmail_xoauth (0.4.3) - oauth (>= 0.3.6) graphql (2.2.5) racc (~> 1.4) graphql-batch (0.5.3) @@ -360,7 +358,7 @@ GEM net-protocol net-protocol (0.2.2) timeout - net-smtp (0.3.3) + net-smtp (0.4.0.1) net-protocol nio4r (2.5.9) nkf (0.1.3) @@ -749,7 +747,6 @@ DEPENDENCIES execjs factory_bot_rails faker - gmail_xoauth graphql graphql-batch hiredis diff --git a/lib/core_ext/net/smtp/auth_xoauth2.rb b/lib/core_ext/net/smtp/auth_xoauth2.rb new file mode 100644 index 000000000000..0ca254042cbb --- /dev/null +++ b/lib/core_ext/net/smtp/auth_xoauth2.rb @@ -0,0 +1,21 @@ +# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ + +require 'net/smtp' + +class Net::SMTP + class AuthXoauth2 < Net::SMTP::Authenticator + auth_type :xoauth2 + + def auth(user, secret) + token = xoauth2_string(user, secret) + + finish("AUTH XOAUTH2 #{base64_encode(token)}") + end + + private + + def xoauth2_string(user, secret) + "user=#{user}\1auth=Bearer #{secret}\1\1" + end + end +end diff --git a/spec/lib/email_helper/probe_spec.rb b/spec/lib/email_helper/probe_spec.rb index 860d6bc546c2..32591314a08e 100644 --- a/spec/lib/email_helper/probe_spec.rb +++ b/spec/lib/email_helper/probe_spec.rb @@ -146,7 +146,7 @@ let(:message_human) { [ 'This host cannot be reached.', 'There is no route to this host.' ] } before do - allow(Socket).to receive(:tcp).and_raise(Errno::EHOSTUNREACH) + allow(TCPSocket).to receive(:open).and_raise(Errno::EHOSTUNREACH) end include_examples 'probe tests with invalid result' diff --git a/spec/support/imap.rb b/spec/support/imap.rb index dfcc50209b75..8a81b431dda4 100644 --- a/spec/support/imap.rb +++ b/spec/support/imap.rb @@ -1,5 +1,7 @@ # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ +require 'net/imap' + module ImapHelper def imap_delete_old_mails(options) imap = ::Net::IMAP.new(options[:host], port: options[:port], ssl: (options[:ssl] ? { verify_mode: OpenSSL::SSL::VERIFY_NONE } : false))