From d415e7792ef84f1007dd2cbda2e3a24642450354 Mon Sep 17 00:00:00 2001 From: Halla Moore Date: Fri, 17 Feb 2017 18:38:50 -0800 Subject: [PATCH] [isomorphic-core] Ensure smtp config exists Summary: We previously weren't saving the smtp settings for cloud gmail accounts, and even though we fixed that, we still need to be able to handle the accounts that were authed before that fix went out. This diff changes `smtpConfig()` to always call `credentialsForProvider` instead of depending on what was saved in the database. Test Plan: manual Reviewers: juan, evan Reviewed By: evan Differential Revision: https://phab.nylas.com/D3980 --- packages/isomorphic-core/src/auth-helpers.js | 1 + .../isomorphic-core/src/models/account.js | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/isomorphic-core/src/auth-helpers.js b/packages/isomorphic-core/src/auth-helpers.js index 27fc6c4ba..628a0766e 100644 --- a/packages/isomorphic-core/src/auth-helpers.js +++ b/packages/isomorphic-core/src/auth-helpers.js @@ -93,6 +93,7 @@ function credentialsForProvider({provider, settings, email}) { module.exports = { SUPPORTED_PROVIDERS, + credentialsForProvider, imapAuthRouteConfig() { return { description: 'Authenticates a new account.', diff --git a/packages/isomorphic-core/src/models/account.js b/packages/isomorphic-core/src/models/account.js index c992fbad0..f51e71301 100644 --- a/packages/isomorphic-core/src/models/account.js +++ b/packages/isomorphic-core/src/models/account.js @@ -1,7 +1,7 @@ const atob = require('atob') const crypto = require('crypto'); const {JSONColumn, JSONArrayColumn} = require('../database-types'); -const {SUPPORTED_PROVIDERS} = require('../auth-helpers'); +const {SUPPORTED_PROVIDERS, credentialsForProvider} = require('../auth-helpers'); const {DB_ENCRYPTION_ALGORITHM, DB_ENCRYPTION_PASSWORD} = process.env; @@ -117,10 +117,15 @@ module.exports = (sequelize, Sequelize) => { }, smtpConfig() { - const {smtp_host, smtp_port, ssl_required} = this.connectionSettings; + const {connectionSettings, connectionCredentials} = credentialsForProvider({ + provider: this.provider, + settings: this.decryptedCredentials(), + email: this.emailAddress, + }); + const {smtp_host, smtp_port, smtp_username, ssl_required} = connectionSettings; let config = {} - if (this.connectionSettings.smtp_custom_config) { - config = this.connectionSettings.smtp_custom_config + if (connectionSettings.smtp_custom_config) { + config = connectionSettings.smtp_custom_config } else { config = { host: smtp_host, @@ -129,15 +134,14 @@ module.exports = (sequelize, Sequelize) => { } } if (this.provider === 'gmail') { - const {xoauth2} = this.decryptedCredentials(); + const {xoauth2} = connectionCredentials; if (!xoauth2) { throw new Error("Missing XOAuth2 Token") } - const {imap_username} = this.connectionSettings; const token = this.bearerToken(xoauth2); - config.auth = { user: imap_username, xoauth2: token } + config.auth = { user: smtp_username, xoauth2: token } } else if (SUPPORTED_PROVIDERS.has(this.provider)) { - const {smtp_username, smtp_password} = this.decryptedCredentials(); + const {smtp_password} = connectionCredentials config.auth = { user: smtp_username, pass: smtp_password} } else { throw new Error(`${this.provider} not yet supported`)