feat: Support additional authentication mechanisms for SMTP (#4431)

* Support additional authentication mechanisms for SMTP
This commit is contained in:
Aswin Dev P.S 2022-04-11 15:43:05 +05:30 committed by GitHub
parent 8622740161
commit 9b5eb98c59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 3 deletions

View file

@ -29,8 +29,12 @@ module Api::V1::InboxesHelper
smtp = Net::SMTP.new(channel_data[:smtp_address], channel_data[:smtp_port])
set_smtp_encryption(channel_data, smtp)
check_smtp_connection(channel_data, smtp)
end
smtp.start(channel_data[:smtp_domain], channel_data[:smtp_email], channel_data[:smtp_password], :login)
def check_smtp_connection(channel_data, smtp)
smtp.start(channel_data[:smtp_domain], channel_data[:smtp_email], channel_data[:smtp_password],
channel_data[:smtp_authentication]&.to_sym || :login)
smtp.finish unless smtp&.nil?
end

View file

@ -529,7 +529,8 @@
"ENCRYPTION": "Encryption",
"SSL_TLS": "SSL/TLS",
"START_TLS": "STARTTLS",
"OPEN_SSL_VERIFY_MODE": "Open SSL Verify Mode"
"OPEN_SSL_VERIFY_MODE": "Open SSL Verify Mode",
"AUTH_MECHANISM": "Authentication"
},
"NOTE": "Note: "
}

View file

@ -69,6 +69,13 @@
:options="openSSLVerifyModes"
:action="handleSSLModeChange"
/>
<single-select-dropdown
class="medium-9 columns"
:label="$t('INBOX_MGMT.SMTP.AUTH_MECHANISM')"
:selected="authMechanism"
:options="authMechanisms"
:action="handleAuthMechanismChange"
/>
</div>
<woot-submit-button
:button-text="$t('INBOX_MGMT.SMTP.UPDATE')"
@ -112,6 +119,7 @@ export default {
ssl: false,
starttls: true,
openSSLVerifyMode: 'none',
authMechanism: 'login',
encryptionProtocols: [
{ id: 'ssl', title: 'SSL/TLS', checked: false },
{ id: 'starttls', title: 'STARTTLS', checked: true },
@ -122,6 +130,15 @@ export default {
{ key: 3, value: 'client_once' },
{ key: 4, value: 'fail_if_no_peer_cert' },
],
authMechanisms: [
{ key: 1, value: 'plain' },
{ key: 2, value: 'login' },
{ key: 3, value: 'cram-md5' },
{ key: 4, value: 'xoauth' },
{ key: 5, value: 'xoauth2' },
{ key: 6, value: 'ntlm' },
{ key: 7, value: 'gssapi' },
],
};
},
validations: {
@ -157,6 +174,7 @@ export default {
smtp_enable_starttls_auto,
smtp_enable_ssl_tls,
smtp_openssl_verify_mode,
smtp_authentication,
} = this.inbox;
this.isSMTPEnabled = smtp_enabled;
this.address = smtp_address;
@ -167,6 +185,7 @@ export default {
this.starttls = smtp_enable_starttls_auto;
this.ssl = smtp_enable_ssl_tls;
this.openSSLVerifyMode = smtp_openssl_verify_mode;
this.authMechanism = smtp_authentication;
this.encryptionProtocols = [
{ id: 'ssl', title: 'SSL/TLS', checked: smtp_enable_ssl_tls },
@ -189,6 +208,9 @@ export default {
handleSSLModeChange(mode) {
this.openSSLVerifyMode = mode;
},
handleAuthMechanismChange(mode) {
this.authMechanism = mode;
},
async updateInbox() {
try {
const payload = {
@ -204,6 +226,7 @@ export default {
smtp_enable_ssl_tls: this.ssl,
smtp_enable_starttls_auto: this.starttls,
smtp_openssl_verify_mode: this.openSSLVerifyMode,
smtp_authentication: this.authMechanism,
},
};
await this.$store.dispatch('inboxes/updateInboxSMTP', payload);

View file

@ -39,7 +39,7 @@ class Channel::Email < ApplicationRecord
self.table_name = 'channel_email'
EDITABLE_ATTRS = [:email, :imap_enabled, :imap_email, :imap_password, :imap_address, :imap_port, :imap_enable_ssl, :imap_inbox_synced_at,
:smtp_enabled, :smtp_email, :smtp_password, :smtp_address, :smtp_port, :smtp_domain, :smtp_enable_starttls_auto,
:smtp_enable_ssl_tls, :smtp_openssl_verify_mode].freeze
:smtp_enable_ssl_tls, :smtp_openssl_verify_mode, :smtp_authentication].freeze
validates :email, uniqueness: true
validates :forward_to_email, uniqueness: true

View file

@ -70,6 +70,7 @@ if resource.email?
json.smtp_enable_ssl_tls resource.channel.try(:smtp_enable_ssl_tls)
json.smtp_enable_starttls_auto resource.channel.try(:smtp_enable_starttls_auto)
json.smtp_openssl_verify_mode resource.channel.try(:smtp_openssl_verify_mode)
json.smtp_authentication resource.channel.try(:smtp_authentication)
end
## API Channel Attributes

View file

@ -540,6 +540,34 @@ RSpec.describe 'Inboxes API', type: :request do
expect(email_channel.reload.smtp_enable_ssl_tls).to be true
expect(email_channel.reload.smtp_openssl_verify_mode).to eq('none')
end
it 'updates smtp configuration with authentication mechanism' do
smtp_connection = double
allow(smtp_connection).to receive(:start).and_return(true)
allow(smtp_connection).to receive(:finish).and_return(true)
allow(smtp_connection).to receive(:respond_to?).and_return(true)
allow(smtp_connection).to receive(:enable_starttls_auto).and_return(true)
allow(Net::SMTP).to receive(:new).and_return(smtp_connection)
patch "/api/v1/accounts/#{account.id}/inboxes/#{email_inbox.id}",
headers: admin.create_new_auth_token,
params: {
channel: {
smtp_enabled: true,
smtp_address: 'smtp.gmail.com',
smtp_port: 587,
smtp_email: 'smtptest@gmail.com',
smtp_authentication: 'plain'
}
},
as: :json
expect(response).to have_http_status(:success)
expect(email_channel.reload.smtp_enabled).to be true
expect(email_channel.reload.smtp_address).to eq('smtp.gmail.com')
expect(email_channel.reload.smtp_port).to eq(587)
expect(email_channel.reload.smtp_authentication).to eq('plain')
end
end
end