diff --git a/app/assets/javascripts/app/controllers/_channel/google.coffee b/app/assets/javascripts/app/controllers/_channel/google.coffee
index 31b69afe5..6207b4aeb 100644
--- a/app/assets/javascripts/app/controllers/_channel/google.coffee
+++ b/app/assets/javascripts/app/controllers/_channel/google.coffee
@@ -37,6 +37,7 @@ class ChannelAccountOverview extends App.ControllerSubContent
events:
'click .js-new': 'new'
'click .js-delete': 'delete'
+ 'click .js-reauthenticate': 'reauthenticate'
'click .js-configApp': 'configApp'
'click .js-disable': 'disable'
'click .js-enable': 'enable'
@@ -158,6 +159,11 @@ class ChannelAccountOverview extends App.ControllerSubContent
container: @el.closest('.content')
)
+ reauthenticate: (e) =>
+ e.preventDefault()
+ id = $(e.target).closest('.action').data('id')
+ window.location.href = "#{@apiPath}/external_credentials/google/link_account?channel_id=#{id}"
+
disable: (e) =>
e.preventDefault()
id = $(e.target).closest('.action').data('id')
diff --git a/app/assets/javascripts/app/controllers/_channel/microsoft365.coffee b/app/assets/javascripts/app/controllers/_channel/microsoft365.coffee
index 4bb97bd69..e92242bee 100644
--- a/app/assets/javascripts/app/controllers/_channel/microsoft365.coffee
+++ b/app/assets/javascripts/app/controllers/_channel/microsoft365.coffee
@@ -37,6 +37,7 @@ class ChannelAccountOverview extends App.ControllerSubContent
events:
'click .js-new': 'new'
'click .js-delete': 'delete'
+ 'click .js-reauthenticate': 'reauthenticate'
'click .js-configApp': 'configApp'
'click .js-disable': 'disable'
'click .js-enable': 'enable'
@@ -149,6 +150,11 @@ class ChannelAccountOverview extends App.ControllerSubContent
container: @el.closest('.content')
)
+ reauthenticate: (e) =>
+ e.preventDefault()
+ id = $(e.target).closest('.action').data('id')
+ window.location.href = "#{@apiPath}/external_credentials/microsoft365/link_account?channel_id=#{id}"
+
disable: (e) =>
e.preventDefault()
id = $(e.target).closest('.action').data('id')
diff --git a/app/assets/javascripts/app/views/google/list.jst.eco b/app/assets/javascripts/app/views/google/list.jst.eco
index 9cbf392aa..6412a06c7 100644
--- a/app/assets/javascripts/app/views/google/list.jst.eco
+++ b/app/assets/javascripts/app/views/google/list.jst.eco
@@ -110,6 +110,7 @@
<%- @T('Enable') %>
<% end %>
<%- @T('Delete') %>
+ <%- @T('Reauthenticate') %>
<% end %>
diff --git a/app/assets/javascripts/app/views/microsoft365/list.jst.eco b/app/assets/javascripts/app/views/microsoft365/list.jst.eco
index 3d3680719..e0b2e9813 100644
--- a/app/assets/javascripts/app/views/microsoft365/list.jst.eco
+++ b/app/assets/javascripts/app/views/microsoft365/list.jst.eco
@@ -110,6 +110,7 @@
<% else: %>
<%- @T('Enable') %>
<% end %>
+ <%- @T('Reauthenticate') %>
<% end %>
diff --git a/app/controllers/external_credentials_controller.rb b/app/controllers/external_credentials_controller.rb
index 8f9eb704b..2bb75a0b4 100644
--- a/app/controllers/external_credentials_controller.rb
+++ b/app/controllers/external_credentials_controller.rb
@@ -34,18 +34,24 @@ class ExternalCredentialsController < ApplicationController
provider = params[:provider].downcase
attributes = ExternalCredential.request_account_to_link(provider)
session[:request_token] = attributes[:request_token]
+ session[:channel_id] = params[:channel_id]
redirect_to attributes[:authorize_url]
end
def callback
provider = params[:provider].downcase
- channel = ExternalCredential.link_account(provider, session[:request_token], params.permit!.to_h)
+ channel = ExternalCredential.link_account(provider, session[:request_token], link_params)
session[:request_token] = nil
+ session[:channel_id] = nil
redirect_to app_url(provider, channel.id)
end
private
+ def link_params
+ params.permit!.to_h.merge(channel_id: session[:channel_id])
+ end
+
def callback_url(provider)
ExternalCredential.callback_url(provider)
end
diff --git a/lib/external_credential/google.rb b/lib/external_credential/google.rb
index 1f2e1f8c2..a610e91a4 100644
--- a/lib/external_credential/google.rb
+++ b/lib/external_credential/google.rb
@@ -41,18 +41,6 @@ class ExternalCredential::Google
user_data = user_info(response[:id_token])
raise Exceptions::UnprocessableEntity, 'Unable to extract user email from id_token!' if user_data[:email].blank?
- migrate_channel = nil
- Channel.where(area: 'Email::Account').find_each do |channel|
- next if channel.options.dig(:inbound, :options, :user) != user_data[:email]
- next if channel.options.dig(:inbound, :options, :host) != 'imap.gmail.com'
- next if channel.options.dig(:outbound, :options, :user) != user_data[:email]
- next if channel.options.dig(:outbound, :options, :host) != 'smtp.gmail.com'
-
- migrate_channel = channel
-
- break
- end
-
channel_options = {
inbound: {
adapter: 'imap',
@@ -82,6 +70,30 @@ class ExternalCredential::Google
),
}
+ if params[:channel_id]
+ existing_channel = Channel.where(area: 'Google::Account').find(params[:channel_id])
+
+ existing_channel.update!(
+ options: channel_options,
+ )
+
+ existing_channel.refresh_xoauth2!
+
+ return existing_channel
+ end
+
+ migrate_channel = nil
+ Channel.where(area: 'Email::Account').find_each do |channel|
+ next if channel.options.dig(:inbound, :options, :user) != user_data[:email]
+ next if channel.options.dig(:inbound, :options, :host) != 'imap.gmail.com'
+ next if channel.options.dig(:outbound, :options, :user) != user_data[:email]
+ next if channel.options.dig(:outbound, :options, :host) != 'smtp.gmail.com'
+
+ migrate_channel = channel
+
+ break
+ end
+
if migrate_channel
channel_options[:inbound][:options][:folder] = migrate_channel.options[:inbound][:options][:folder]
channel_options[:inbound][:options][:keep_on_server] = migrate_channel.options[:inbound][:options][:keep_on_server]
diff --git a/lib/external_credential/microsoft365.rb b/lib/external_credential/microsoft365.rb
index f95d129ca..f7ada482b 100644
--- a/lib/external_credential/microsoft365.rb
+++ b/lib/external_credential/microsoft365.rb
@@ -41,18 +41,6 @@ class ExternalCredential::Microsoft365
user_data = user_info(response[:id_token])
raise Exceptions::UnprocessableEntity, 'Unable to extract user preferred_username from id_token!' if user_data[:preferred_username].blank?
- migrate_channel = nil
- Channel.where(area: 'Email::Account').find_each do |channel|
- next if channel.options.dig(:inbound, :options, :user) != user_data[:email]
- next if channel.options.dig(:inbound, :options, :host) != 'outlook.office365.com'
- next if channel.options.dig(:outbound, :options, :user) != user_data[:email]
- next if channel.options.dig(:outbound, :options, :host) != 'smtp.office365.com'
-
- migrate_channel = channel
-
- break
- end
-
channel_options = {
inbound: {
adapter: 'imap',
@@ -81,6 +69,30 @@ class ExternalCredential::Microsoft365
),
}
+ if params[:channel_id]
+ existing_channel = Channel.where(area: 'Microsoft365::Account').find(params[:channel_id])
+
+ existing_channel.update!(
+ options: channel_options,
+ )
+
+ existing_channel.refresh_xoauth2!
+
+ return existing_channel
+ end
+
+ migrate_channel = nil
+ Channel.where(area: 'Email::Account').find_each do |channel|
+ next if channel.options.dig(:inbound, :options, :user) != user_data[:email]
+ next if channel.options.dig(:inbound, :options, :host) != 'outlook.office365.com'
+ next if channel.options.dig(:outbound, :options, :user) != user_data[:email]
+ next if channel.options.dig(:outbound, :options, :host) != 'smtp.office365.com'
+
+ migrate_channel = channel
+
+ break
+ end
+
if migrate_channel
channel_options[:inbound][:options][:folder] = migrate_channel.options[:inbound][:options][:folder]
channel_options[:inbound][:options][:keep_on_server] = migrate_channel.options[:inbound][:options][:keep_on_server]