Files
odoo_source/addons/fetchmail_gmail/models/fetchmail_server.py
T
qmo-odoo 04e86613c8 [ADD] {google,fetchmail}_gmail: OAuth for gmail servers
Purpose
=======
Less secured apps are no longer supported by google, therefore, we need
to transition to the OAuth2 authentication system.

Specifications
==============
1. User will need to fill their Gmail API credentials in the main
   settings page
2. Then, in the incoming / outgoing mail server form view, they will
   need to tick the Gmail support checkbox
3. A link will be available to be redirected to Gmail and accept the
   permission
4. The user can now copy / paste the authorization code in Odoo, set
   his email as "login" and then send / receive emails with Gmail

Task-2170676

closes odoo/odoo#83424

X-original-commit: ff223afc5300b2421b8ce935bb468f503c938c5d
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2022-01-27 09:12:50 +00:00

45 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models, _
from odoo.exceptions import UserError
class FetchmailServer(models.Model):
_name = 'fetchmail.server'
_inherit = ['fetchmail.server', 'google.gmail.mixin']
@api.constrains('use_google_gmail_service', 'server_type')
def _check_use_google_gmail_service(self):
if any(server.use_google_gmail_service and server.server_type != 'imap' for server in self):
raise UserError(_('Gmail authentication only supports IMAP server type.'))
@api.onchange('use_google_gmail_service')
def _onchange_use_google_gmail_service(self):
"""Set the default configuration for a IMAP Gmail server."""
if self.use_google_gmail_service:
self.server = 'imap.gmail.com'
self.server_type = 'imap'
self.is_ssl = True
self.port = 993
else:
self.server_type = 'pop'
self.is_ssl = False
self.google_gmail_authorization_code = False
self.google_gmail_refresh_token = False
self.google_gmail_access_token = False
self.google_gmail_access_token_expiration = False
def _imap_login(self, connection):
"""Authenticate the IMAP connection.
If the mail server is Gmail, we use the OAuth2 authentication protocol.
"""
self.ensure_one()
if self.use_google_gmail_service:
auth_string = self._generate_oauth2_string(self.user, self.google_gmail_refresh_token)
connection.authenticate('XOAUTH2', lambda x: auth_string)
connection.select('INBOX')
else:
super(FetchmailServer, self)._imap_login(connection)