From d095551e90b28919e7bddec1d8f65b714774dbea Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Fri, 16 Dec 2016 16:53:05 -0500 Subject: [PATCH] [isomorphic-core] add office365 auth support Summary: Adds support for office 365 Depends on D3532 Test Plan: manual Reviewers: jackie, halla, mark, juan Reviewed By: juan Differential Revision: https://phab.nylas.com/D3533 --- packages/isomorphic-core/src/auth-helpers.js | 91 ++++++++++++-------- 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/packages/isomorphic-core/src/auth-helpers.js b/packages/isomorphic-core/src/auth-helpers.js index 74beada3b..e863bd0cb 100644 --- a/packages/isomorphic-core/src/auth-helpers.js +++ b/packages/isomorphic-core/src/auth-helpers.js @@ -20,10 +20,12 @@ const resolvedGmailSettings = Joi.object().keys({ expiry_date: Joi.number().integer().required(), }).required(); -const exchangeSettings = Joi.object().keys({ - username: Joi.string().required(), +const office365Settings = Joi.object().keys({ + name: Joi.string().required(), + type: Joi.string().valid('office365').required(), + email: Joi.string().required(), password: Joi.string().required(), - eas_server_host: [Joi.string().ip().required(), Joi.string().hostname().required()], + username: Joi.string().required(), }).required(); const USER_ERRORS = { @@ -32,6 +34,52 @@ const USER_ERRORS = { IMAP_RETRY: "We were unable to reach your mail provider. Please try again.", } +function credentialsForProvider({provider, settings, email}) { + if (provider === "gmail") { + const connectionSettings = { + imap_username: email, + imap_host: 'imap.gmail.com', + imap_port: 993, + smtp_username: email, + smtp_host: 'smtp.gmail.com', + smtp_port: 465, + ssl_required: true, + } + const connectionCredentials = { + xoauth2: settings.xoauth2, + expiry_date: settings.expiry_date, + } + return {connectionSettings, connectionCredentials} + } else if (provider === "imap") { + const connectionSettings = _.pick(settings, [ + 'imap_host', 'imap_port', + 'smtp_host', 'smtp_port', + 'ssl_required', + ]); + const connectionCredentials = _.pick(settings, [ + 'imap_username', 'imap_password', + 'smtp_username', 'smtp_password', + ]); + return {connectionSettings, connectionCredentials} + } else if (provider === "office365") { + const connectionSettings = { + imap_host: 'outlook.office365.com', + imap_port: 993, + smtp_host: 'smtp.office365.com', + smtp_port: 465, + ssl_required: true, + } + const connectionCredentials = { + imap_username: email, + imap_password: settings.password, + smtp_username: email, + smpt_password: settings.password, + } + return {connectionSettings, connectionCredentials} + } + throw new Error(`Invalid provider: ${provider}`) +} + module.exports = { imapAuthRouteConfig() { return { @@ -42,8 +90,8 @@ module.exports = { payload: { email: Joi.string().email().required(), name: Joi.string().required(), - provider: Joi.string().valid('imap', 'gmail').required(), - settings: Joi.alternatives().try(imapSmtpSettings, exchangeSettings, resolvedGmailSettings), + provider: Joi.string().valid('imap', 'gmail', 'office365').required(), + settings: Joi.alternatives().try(imapSmtpSettings, office365Settings, resolvedGmailSettings), }, }, } @@ -53,38 +101,9 @@ module.exports = { return (request, reply) => { const dbStub = {}; const connectionChecks = []; - const {settings, email, provider, name} = request.payload; + const {email, provider, name} = request.payload; - let connectionSettings = null; - let connectionCredentials = null; - - if (provider === 'imap') { - connectionSettings = _.pick(settings, [ - 'imap_host', 'imap_port', - 'smtp_host', 'smtp_port', - 'ssl_required', - ]); - connectionCredentials = _.pick(settings, [ - 'imap_username', 'imap_password', - 'smtp_username', 'smtp_password', - ]); - } - - if (provider === 'gmail') { - connectionSettings = { - imap_username: email, - imap_host: 'imap.gmail.com', - imap_port: 993, - smtp_username: email, - smtp_host: 'smtp.gmail.com', - smtp_port: 465, - ssl_required: true, - } - connectionCredentials = { - xoauth2: settings.xoauth2, - expiry_date: settings.expiry_date, - } - } + const {connectionSettings, connectionCredentials} = credentialsForProvider(request.payload) connectionChecks.push(IMAPConnection.connect({ settings: Object.assign({}, connectionSettings, connectionCredentials),