diff --git a/gmail/appsscript.json b/gmail/appsscript.json index 0f1164ef0..c612959fd 100644 --- a/gmail/appsscript.json +++ b/gmail/appsscript.json @@ -34,6 +34,7 @@ "https://*.odoo.com/web/login", "https://*.odoo.com/mail_plugin/auth", "https://*.odoo.com/mail_plugin/auth/access_token", + "https://*.odoo.com/mail_plugin/auth/check_version", "https://odoo.com/mail_plugin/get_translations", "https://odoo.com/mail_plugin/partner/get", "https://odoo.com/mail_plugin/log_mail_content", @@ -49,6 +50,7 @@ "https://odoo.com/web/login", "https://odoo.com/mail_plugin/auth", "https://odoo.com/mail_plugin/auth/access_token", + "https://odoo.com/mail_plugin/auth/check_version", "https://iap-services.odoo.com/iap/mail_extension/enrich" ] } diff --git a/gmail/src/const.ts b/gmail/src/const.ts index a59c7976e..219a28535 100644 --- a/gmail/src/const.ts +++ b/gmail/src/const.ts @@ -23,6 +23,7 @@ export const ODOO_AUTH_URLS: Record = { LOGIN: "/web/login", AUTH_CODE: "/mail_plugin/auth", CODE_VALIDATION: "/mail_plugin/auth/access_token", + CHECK_VERSION: "/mail_plugin/auth/check_version", SCOPE: "outlook", FRIENDLY_NAME: "Gmail", }; diff --git a/gmail/src/services/odoo_auth.ts b/gmail/src/services/odoo_auth.ts index fd8bfda47..ca7b4ca1d 100644 --- a/gmail/src/services/odoo_auth.ts +++ b/gmail/src/services/odoo_auth.ts @@ -132,33 +132,27 @@ export const resetAccessToken = () => { }; /** - * Make an HTTP request to "/mail_plugin/auth/access_token" (cors="*") on the Odoo - * database to verify that the server is reachable and that the mail plugin module is - * installed. + * Make an HTTP request to the Odoo database to verify that the server + * is reachable and that the mail plugin module is installed. * - * Returns True if the Odoo database is reachable and if the "mail_plugin" module - * is installed, false otherwise. + * Returns the version of the addin that is supported if it's reachable, null otherwise. */ -export const isOdooDatabaseReachable = (odooUrl: string): boolean => { +export const getSupportedAddinVersion = (odooUrl: string): number | null => { if (!odooUrl || !odooUrl.length) { - return false; + return null; } - const response = postJsonRpc( - odooUrl + ODOO_AUTH_URLS.CODE_VALIDATION, - { auth_code: null }, - {}, - { returnRawResponse: true }, - ); + const response = postJsonRpc(odooUrl + ODOO_AUTH_URLS.CHECK_VERSION, {}, {}, { returnRawResponse: true }); if (!response) { - return false; + return null; } const responseCode = response.getResponseCode(); if (responseCode > 299 || responseCode < 200) { - return false; + return null; } - return true; + const textResponse = response.getContentText("UTF-8"); + return parseInt(JSON.parse(textResponse).result); }; diff --git a/gmail/src/views/login.ts b/gmail/src/views/login.ts index 574432c08..ca7e2d437 100644 --- a/gmail/src/views/login.ts +++ b/gmail/src/views/login.ts @@ -2,7 +2,7 @@ import { formatUrl, repeat } from "../utils/format"; import { notify, createKeyValueWidget } from "./helpers"; import { State } from "../models/state"; import { IMAGES_LOGIN } from "./icons"; -import { isOdooDatabaseReachable } from "../services/odoo_auth"; +import { getSupportedAddinVersion } from "../services/odoo_auth"; import { _t, clearTranslationCache } from "../services/translation"; import { setOdooServerUrl } from "src/services/app_properties"; @@ -21,9 +21,15 @@ function onNextLogin(event) { setOdooServerUrl(validatedUrl); - if (!isOdooDatabaseReachable(validatedUrl)) { + const version = getSupportedAddinVersion(validatedUrl); + + if (!version) { + return notify("Could not connect to your database."); + } + + if (version !== 1) { return notify( - "Could not connect to your database. Make sure the module is installed in Odoo (Settings > General Settings > Integrations > Mail Plugins)", + "This addin version required Odoo 19.0 or an older version, please install a newer addin version.", ); } diff --git a/outlook/src/taskpane/api.js b/outlook/src/taskpane/api.js index cb6b9669c..19cc2a81e 100644 --- a/outlook/src/taskpane/api.js +++ b/outlook/src/taskpane/api.js @@ -19,6 +19,7 @@ const api = { loginPage: '/web/login', // Should be the usual Odoo login page. authCodePage: '/mail_plugin/auth', // The page where to allow or deny access. You get an auth code. getAccessToken: '/mail_plugin/auth/access_token', // The address where to post to exchange an auth code for an access token. + getOdooVersion: '/mail_plugin/auth/check_version', addInBaseURL: 'https://' + __DOMAIN__, outlookScope: 'outlook', outlookFriendlyName: 'Outlook', diff --git a/outlook/src/taskpane/components/Login/Login.tsx b/outlook/src/taskpane/components/Login/Login.tsx index ace53c5ac..5caa9f57a 100644 --- a/outlook/src/taskpane/components/Login/Login.tsx +++ b/outlook/src/taskpane/components/Login/Login.tsx @@ -19,6 +19,7 @@ enum AuthenticationRequestError { DatabaseNotReachable, PermissionRefused, AuthenticationCodeExpired, + InvalidOdooVersion, } type LoginState = { @@ -78,7 +79,9 @@ class Login extends React.Component<{}, LoginState> { api.baseURL = sanitizedURL; localStorage.setItem('baseURL', sanitizedURL); - if (!(await this._isOdooDatabaseReachable())) { + const version = await this._getSupportedAddinVersion() + + if (!version) { this.setState({ isCheckingUrl: false, authenticationRequestError: AuthenticationRequestError.DatabaseNotReachable, @@ -86,6 +89,14 @@ class Login extends React.Component<{}, LoginState> { return; } + if (version !== 1) { + this.setState({ + isCheckingUrl: false, + authenticationRequestError: AuthenticationRequestError.InvalidOdooVersion, + }); + return; + } + this.setState({ isCheckingUrl: false }); const authCode = await this._openOdooLoginDialog(); @@ -111,16 +122,13 @@ class Login extends React.Component<{}, LoginState> { }; /** - * Check if the database URL is correct and if the mail plugin is installed - * by requesting the endpoint "/mail_plugin/auth/access_token" (with cors="*" !). - * - * If the URL is not reachable (invalid URL or the Odoo module is not installed) - * return false. + * Make an HTTP request to the Odoo database to verify that the server + * is reachable and that the mail plugin module is installed. */ - _isOdooDatabaseReachable = async () => { + _getSupportedAddinVersion = async () => { const request = sendHttpRequest( HttpVerb.POST, - api.baseURL + api.getAccessToken, + api.baseURL + api.getOdooVersion, ContentType.Json, null, {}, @@ -128,10 +136,10 @@ class Login extends React.Component<{}, LoginState> { ); try { - await request.promise; - return true; + const response = (await request.promise) + return JSON.parse(response).result; } catch { - return false; + return null; } }; @@ -289,6 +297,8 @@ class Login extends React.Component<{}, LoginState> { [AuthenticationRequestError.PermissionRefused]: 'Permission to access your database needs to be granted. ', [AuthenticationRequestError.AuthenticationCodeExpired]: 'Your authentication code is invalid or has expired. ', + [AuthenticationRequestError.InvalidOdooVersion]: + 'This addin version required Odoo 19.0 or an older version, please install a newer addin version.', }; const errorStr = ERROR_MESSAGES[this.state.authenticationRequestError];