feat: add Google Email fetch and OAuth token refresh service (#9603)
This PR adds the following changes 1. Add `Imap::GoogleFetchEmailService` and `Google::RefreshOauthTokenService`. The `Google::RefreshOauthTokenService` uses `OmniAuth::Strategies::GoogleOauth2` which is already added as a packge 2. Update `Inboxes::FetchImapEmailsJob` to handle Google inboxes 3. Add SMTP settings for Google in `ConversationReplyMailerHelper` to allow sending emails ## Preview #### Incoming emails  #### Outgoing email  --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
62
app/services/base_refresh_oauth_token_service.rb
Normal file
62
app/services/base_refresh_oauth_token_service.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
class BaseRefreshOauthTokenService
|
||||
pattr_initialize [:channel!]
|
||||
|
||||
# Additional references: https://gitlab.com/gitlab-org/ruby/gems/gitlab-mail_room/-/blob/master/lib/mail_room/microsoft_graph/connection.rb
|
||||
def access_token
|
||||
return provider_config[:access_token] unless access_token_expired?
|
||||
|
||||
refreshed_tokens = refresh_tokens
|
||||
refreshed_tokens[:access_token]
|
||||
end
|
||||
|
||||
def access_token_expired?
|
||||
expiry = provider_config[:expires_on]
|
||||
|
||||
return true if expiry.blank?
|
||||
|
||||
# Adding a 5 minute window to expiry check to avoid any race
|
||||
# conditions during the fetch operation. This would assure that the
|
||||
# tokens are updated when we fetch the emails.
|
||||
Time.current.utc >= DateTime.parse(expiry) - 5.minutes
|
||||
end
|
||||
|
||||
# Refresh the access tokens using the refresh token
|
||||
# Refer: https://github.com/microsoftgraph/msgraph-sample-rubyrailsapp/tree/b4a6869fe4a438cde42b161196484a929f1bee46
|
||||
def refresh_tokens
|
||||
oauth_strategy = build_oauth_strategy
|
||||
token_service = build_token_service(oauth_strategy)
|
||||
|
||||
new_tokens = token_service.refresh!.to_hash.slice(:access_token, :refresh_token, :expires_at)
|
||||
|
||||
update_channel_provider_config(new_tokens)
|
||||
channel.reload.provider_config
|
||||
end
|
||||
|
||||
def update_channel_provider_config(new_tokens)
|
||||
channel.provider_config = {
|
||||
access_token: new_tokens[:access_token],
|
||||
refresh_token: new_tokens[:refresh_token],
|
||||
expires_on: Time.at(new_tokens[:expires_at]).utc.to_s
|
||||
}
|
||||
channel.save!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_oauth_strategy
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def provider_config
|
||||
@provider_config ||= channel.provider_config.with_indifferent_access
|
||||
end
|
||||
|
||||
# Builds the token service using OAuth2
|
||||
def build_token_service(oauth_strategy)
|
||||
OAuth2::AccessToken.new(
|
||||
oauth_strategy.client,
|
||||
provider_config[:access_token],
|
||||
refresh_token: provider_config[:refresh_token]
|
||||
)
|
||||
end
|
||||
end
|
||||
12
app/services/google/refresh_oauth_token_service.rb
Normal file
12
app/services/google/refresh_oauth_token_service.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
# Refer: https://learn.microsoft.com/en-us/entra/identity-platform/configurable-token-lifetimes
|
||||
class Google::RefreshOauthTokenService < BaseRefreshOauthTokenService
|
||||
private
|
||||
|
||||
# Builds the OAuth strategy for Microsoft Graph
|
||||
def build_oauth_strategy
|
||||
app_id = GlobalConfigService.load('GOOGLE_OAUTH_CLIENT_ID', nil)
|
||||
app_secret = GlobalConfigService.load('GOOGLE_OAUTH_CLIENT_SECRET', nil)
|
||||
|
||||
OmniAuth::Strategies::GoogleOauth2.new(nil, app_id, app_secret)
|
||||
end
|
||||
end
|
||||
17
app/services/imap/google_fetch_email_service.rb
Normal file
17
app/services/imap/google_fetch_email_service.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
class Imap::GoogleFetchEmailService < Imap::BaseFetchEmailService
|
||||
def fetch_emails
|
||||
return if channel.provider_config['access_token'].blank?
|
||||
|
||||
fetch_mail_for_channel
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def authentication_type
|
||||
'XOAUTH2'
|
||||
end
|
||||
|
||||
def imap_password
|
||||
Google::RefreshOauthTokenService.new(channel: channel).access_token
|
||||
end
|
||||
end
|
||||
@@ -1,64 +1,9 @@
|
||||
# Refer: https://learn.microsoft.com/en-us/entra/identity-platform/configurable-token-lifetimes
|
||||
class Microsoft::RefreshOauthTokenService
|
||||
pattr_initialize [:channel!]
|
||||
|
||||
# Additional references: https://gitlab.com/gitlab-org/ruby/gems/gitlab-mail_room/-/blob/master/lib/mail_room/microsoft_graph/connection.rb
|
||||
def access_token
|
||||
return provider_config[:access_token] unless access_token_expired?
|
||||
|
||||
refreshed_tokens = refresh_tokens
|
||||
refreshed_tokens[:access_token]
|
||||
end
|
||||
|
||||
def access_token_expired?
|
||||
expiry = provider_config[:expires_on]
|
||||
|
||||
return true if expiry.blank?
|
||||
|
||||
# Adding a 5 minute window to expiry check to avoid any race
|
||||
# conditions during the fetch operation. This would assure that the
|
||||
# tokens are updated when we fetch the emails.
|
||||
Time.current.utc >= DateTime.parse(expiry) - 5.minutes
|
||||
end
|
||||
|
||||
# Refresh the access tokens using the refresh token
|
||||
# Refer: https://github.com/microsoftgraph/msgraph-sample-rubyrailsapp/tree/b4a6869fe4a438cde42b161196484a929f1bee46
|
||||
def refresh_tokens
|
||||
oauth_strategy = build_oauth_strategy
|
||||
token_service = build_token_service(oauth_strategy)
|
||||
|
||||
new_tokens = token_service.refresh!.to_hash.slice(:access_token, :refresh_token, :expires_at)
|
||||
|
||||
update_channel_provider_config(new_tokens)
|
||||
channel.reload.provider_config
|
||||
end
|
||||
|
||||
def update_channel_provider_config(new_tokens)
|
||||
channel.provider_config = {
|
||||
access_token: new_tokens[:access_token],
|
||||
refresh_token: new_tokens[:refresh_token],
|
||||
expires_on: Time.at(new_tokens[:expires_at]).utc.to_s
|
||||
}
|
||||
channel.save!
|
||||
end
|
||||
|
||||
class Microsoft::RefreshOauthTokenService < BaseRefreshOauthTokenService
|
||||
private
|
||||
|
||||
def provider_config
|
||||
@provider_config ||= channel.provider_config.with_indifferent_access
|
||||
end
|
||||
|
||||
# Builds the OAuth strategy for Microsoft Graph
|
||||
def build_oauth_strategy
|
||||
::MicrosoftGraphAuth.new(nil, GlobalConfigService.load('AZURE_APP_ID', ''), GlobalConfigService.load('AZURE_APP_SECRET', ''))
|
||||
end
|
||||
|
||||
# Builds the token service using OAuth2
|
||||
def build_token_service(oauth_strategy)
|
||||
OAuth2::AccessToken.new(
|
||||
oauth_strategy.client,
|
||||
provider_config[:access_token],
|
||||
refresh_token: provider_config[:refresh_token]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user