Browse Source

Maintenance: Use Net::SMTP authenticator API for XOauth2 support.

renovatebot 1 year ago
parent
commit
5f8cb09d16
5 changed files with 25 additions and 6 deletions
  1. 0 1
      Gemfile
  2. 1 4
      Gemfile.lock
  3. 21 0
      lib/core_ext/net/smtp/auth_xoauth2.rb
  4. 1 1
      spec/lib/email_helper/probe_spec.rb
  5. 2 0
      spec/support/imap.rb

+ 0 - 1
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'

+ 1 - 4
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

+ 21 - 0
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

+ 1 - 1
spec/lib/email_helper/probe_spec.rb

@@ -146,7 +146,7 @@ RSpec.describe EmailHelper::Probe, integration: true, required_envs: %w[MAIL_SER
       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'

+ 2 - 0
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))