Ejemplo n.º 1
0
def _process_imap_exception(exc):
    if 'Lookup failed' in exc.message:
        # Gmail is disabled for this apps account
        return ImapSupportDisabledError('gmail_disabled_for_domain')
    elif 'IMAP access is disabled for your domain.' in exc.message:
        # IMAP is disabled for this domain
        return ImapSupportDisabledError('imap_disabled_for_domain')
    elif exc.message.startswith('[AUTHENTICATIONFAILED] Invalid credentials '
                                '(Failure)'):
        return ImapSupportDisabledError('authentication_failed')
    else:
        # Unknown IMAPClient error
        return exc
Ejemplo n.º 2
0
 def _authenticate_IMAP_connection(self, account, conn):
     """
     Overrides the same method in OAuthAuthHandler so that
     we can choose a token w/ the appropriate scope.
     """
     host, port = account.imap_endpoint
     try:
         token = g_token_manager.get_token_for_email(account)
         conn.oauth2_login(account.email_address, token)
     except IMAPClient.Error as exc:
         log.error('Error during IMAP XOAUTH2 login',
                   account_id=account.id,
                   email=account.email_address,
                   host=host,
                   port=port,
                   error=exc)
         if not _auth_is_invalid(exc):
             raise
         # If we got an AUTHENTICATIONFAILED response, force a token refresh
         # and try again. If IMAP auth still fails, it's likely that IMAP
         # access is disabled, so propagate that errror.
         token = g_token_manager.get_token_for_email(account,
                                                     force_refresh=True)
         try:
             conn.oauth2_login(account.email_address, token)
         except IMAPClient.Error as exc:
             if not _auth_is_invalid(exc):
                 raise
             raise ImapSupportDisabledError()
Ejemplo n.º 3
0
    def authenticate_imap_connection(self, account, conn):
        token = token_manager.get_token(account)
        try:
            conn.oauth2_login(account.email_address, token)
        except IMAPClient.Error as exc:
            exc = _process_imap_exception(exc)

            # Raise all IMAP disabled errors except authentication_failed
            # error, which we handle differently.
            if (isinstance(exc, ImapSupportDisabledError)
                    and exc.reason != "authentication_failed"):
                raise exc

            log.error(
                "Error during IMAP XOAUTH2 login",
                account_id=account.id,
                error=exc,
            )
            if not isinstance(exc, ImapSupportDisabledError):
                raise  # Unknown IMAPClient error, reraise

            # If we got an AUTHENTICATIONFAILED response, force a token refresh
            # and try again. If IMAP auth still fails, it's likely that IMAP
            # access is disabled, so propagate that errror.
            token = token_manager.get_token(account, force_refresh=True)
            try:
                conn.oauth2_login(account.email_address, token)
            except IMAPClient.Error as exc:
                exc = _process_imap_exception(exc)
                if (not isinstance(exc, ImapSupportDisabledError)
                        or exc.reason != "authentication_failed"):
                    raise exc
                else:
                    # Instead of authentication_failed, report imap disabled
                    raise ImapSupportDisabledError("imap_disabled_for_account")
Ejemplo n.º 4
0
def _process_imap_exception(exc):
    if "Lookup failed" in exc.message:
        # Gmail is disabled for this apps account
        return ImapSupportDisabledError("gmail_disabled_for_domain")
    elif "IMAP access is disabled for your domain." in exc.message:
        # IMAP is disabled for this domain
        return ImapSupportDisabledError("imap_disabled_for_domain")
    elif exc.message.startswith(
            "[AUTHENTICATIONFAILED] Invalid credentials (Failure)"):
        # Google
        return ImapSupportDisabledError("authentication_failed")
    elif exc.message.startswith("AUTHENTICATE failed."):
        # Microsoft
        return ImapSupportDisabledError("authentication_failed")
    else:
        # Unknown IMAPClient error
        return exc
Ejemplo n.º 5
0
    def _authenticate_IMAP_connection(self, account, conn):
        """
        Overrides the same method in OAuthAuthHandler so that
        we can choose a token w/ the appropriate scope.

        """
        host, port = account.imap_endpoint
        try:
            token = g_token_manager.get_token_for_email(account)
            conn.oauth2_login(account.email_address, token)
        except IMAPClient.Error as exc:
            exc = _process_imap_exception(exc)
            # Raise all imap disabled errors except authentication_failed
            # error, swhich we handle differently
            if isinstance(exc, ImapSupportDisabledError) and \
                    exc.reason != 'authentication_failed':
                raise exc

            log.error('Error during IMAP XOAUTH2 login',
                      account_id=account.id,
                      email=account.email_address,
                      host=host,
                      port=port,
                      error=exc)
            if not isinstance(exc, ImapSupportDisabledError):
                raise  # Unknown IMAPClient error, reraise

            # If we got an AUTHENTICATIONFAILED response, force a token refresh
            # and try again. If IMAP auth still fails, it's likely that IMAP
            # access is disabled, so propagate that errror.
            token = g_token_manager.get_token_for_email(account,
                                                        force_refresh=True)
            try:
                conn.oauth2_login(account.email_address, token)
            except IMAPClient.Error as exc:
                exc = _process_imap_exception(exc)
                if not isinstance(exc, ImapSupportDisabledError) or \
                        exc.reason != 'authentication_failed':
                    raise exc
                else:
                    # Instead of authentication_failed, report imap disabled
                    raise ImapSupportDisabledError('imap_disabled_for_account')
Ejemplo n.º 6
0
def raise_imap_disabled_error(*args):
    raise ImapSupportDisabledError()
Ejemplo n.º 7
0
 def raise_exc(*args, **kwargs):
     raise ImapSupportDisabledError()