From 772e54c6bf12a2ddcce6ad4ee2b03257a71a0f26 Mon Sep 17 00:00:00 2001 From: "Ifedapo .A. Olarewaju" Date: Mon, 6 Jul 2020 14:22:15 +0100 Subject: [PATCH] companion: rename microsoft and google options to onedrive and drive respectively (#2346) * companion: rename microsoft and google provider options to onedrive and drive respectively * companion: throw error when the deprecated option is set --- .../@uppy/companion-client/src/Provider.js | 1 - .../@uppy/companion-client/types/index.d.ts | 1 - packages/@uppy/companion/src/companion.js | 10 +++++++ packages/@uppy/companion/src/config/grant.js | 2 ++ .../companion/src/server/provider/index.js | 26 +++++++------------ .../@uppy/companion/src/standalone/helper.js | 4 +-- .../@uppy/companion/src/standalone/index.js | 2 +- packages/@uppy/google-drive/src/index.js | 1 - packages/@uppy/instagram/src/index.js | 1 - 9 files changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/@uppy/companion-client/src/Provider.js b/packages/@uppy/companion-client/src/Provider.js index 138f6975e3..bf4f28e338 100644 --- a/packages/@uppy/companion-client/src/Provider.js +++ b/packages/@uppy/companion-client/src/Provider.js @@ -12,7 +12,6 @@ module.exports = class Provider extends RequestClient { super(uppy, opts) this.provider = opts.provider this.id = this.provider - this.authProvider = opts.authProvider || this.provider this.name = this.opts.name || _getName(this.id) this.pluginId = this.opts.pluginId this.tokenKey = `companion-${this.pluginId}-auth-token` diff --git a/packages/@uppy/companion-client/types/index.d.ts b/packages/@uppy/companion-client/types/index.d.ts index 886feb4f40..0dc89c02f8 100644 --- a/packages/@uppy/companion-client/types/index.d.ts +++ b/packages/@uppy/companion-client/types/index.d.ts @@ -37,7 +37,6 @@ export interface PublicProviderOptions extends RequestClientOptions { */ export interface ProviderOptions extends PublicProviderOptions { provider: string - authProvider?: string name?: string pluginId: string } diff --git a/packages/@uppy/companion/src/companion.js b/packages/@uppy/companion/src/companion.js index f0e2eb6e4a..ece274b5be 100644 --- a/packages/@uppy/companion/src/companion.js +++ b/packages/@uppy/companion/src/companion.js @@ -335,4 +335,14 @@ const validateConfig = (companionOptions) => { `No access to "${companionOptions.filePath}". Please ensure the directory exists and with read/write permissions.` ) } + + const { providerOptions } = companionOptions + if (providerOptions) { + const deprecatedOptions = { microsoft: 'onedrive', google: 'drive' } + Object.keys(deprecatedOptions).forEach((deprected) => { + if (providerOptions[deprected]) { + throw new Error(`The Provider option "${deprected}" is no longer supported. Please use the option "${deprecatedOptions[deprected]}" instead.`) + } + }) + } } diff --git a/packages/@uppy/companion/src/config/grant.js b/packages/@uppy/companion/src/config/grant.js index a36b0722c3..0607ec0d96 100644 --- a/packages/@uppy/companion/src/config/grant.js +++ b/packages/@uppy/companion/src/config/grant.js @@ -1,6 +1,7 @@ // oauth configuration for provider services that are used. module.exports = () => { return { + // for drive google: { transport: 'session', scope: [ @@ -23,6 +24,7 @@ module.exports = () => { scope: ['email', 'user_photos'], callback: '/facebook/callback' }, + // for onedrive microsoft: { transport: 'session', scope: ['files.read.all', 'offline_access', 'User.Read'], diff --git a/packages/@uppy/companion/src/server/provider/index.js b/packages/@uppy/companion/src/server/provider/index.js index 9f035e5e18..864156b9c9 100644 --- a/packages/@uppy/companion/src/server/provider/index.js +++ b/packages/@uppy/companion/src/server/provider/index.js @@ -100,17 +100,17 @@ module.exports.addProviderOptions = (companionOptions, grantConfig) => { const { oauthDomain } = server const keys = Object.keys(providerOptions).filter((key) => key !== 'server') - keys.forEach((authProvider) => { - if (grantConfig[authProvider]) { + keys.forEach((providerName) => { + const authProvider = providerNameToAuthName(providerName, companionOptions) + if (authProvider && grantConfig[authProvider]) { // explicitly add providerOptions so users don't override other providerOptions. - grantConfig[authProvider].key = providerOptions[authProvider].key - grantConfig[authProvider].secret = providerOptions[authProvider].secret - const { provider, name } = authNameToProvider(authProvider, companionOptions) + grantConfig[authProvider].key = providerOptions[providerName].key + grantConfig[authProvider].secret = providerOptions[providerName].secret + const provider = exports.getDefaultProviders(companionOptions)[providerName] Object.assign(grantConfig[authProvider], provider.getExtraConfig()) // override grant.js redirect uri with companion's custom redirect url if (oauthDomain) { - const providerName = name const redirectPath = `/${providerName}/redirect` const isExternal = !!server.implicitPath const fullRedirectPath = getURLBuilder(companionOptions)(redirectPath, isExternal, true) @@ -131,19 +131,13 @@ module.exports.addProviderOptions = (companionOptions, grantConfig) => { /** * - * @param {string} authProvider + * @param {string} name of the provider * @param {{server: object, providerOptions: object}} options - * @return {{name: string, provider: typeof Provider}} + * @return {string} the authProvider for this provider */ -const authNameToProvider = (authProvider, options) => { +const providerNameToAuthName = (name, options) => { const providers = exports.getDefaultProviders(options) - const providerNames = Object.keys(providers) - for (const name of providerNames) { - const provider = providers[name] - if (provider.authProvider === authProvider) { - return { name, provider } - } - } + return (providers[name] || {}).authProvider } /** diff --git a/packages/@uppy/companion/src/standalone/helper.js b/packages/@uppy/companion/src/standalone/helper.js index 9950051eea..be9879fe0b 100644 --- a/packages/@uppy/companion/src/standalone/helper.js +++ b/packages/@uppy/companion/src/standalone/helper.js @@ -29,7 +29,7 @@ const getConfigFromEnv = () => { return { providerOptions: { - google: { + drive: { key: process.env.COMPANION_GOOGLE_KEY, secret: getSecret('COMPANION_GOOGLE_SECRET') }, @@ -45,7 +45,7 @@ const getConfigFromEnv = () => { key: process.env.COMPANION_FACEBOOK_KEY, secret: getSecret('COMPANION_FACEBOOK_SECRET') }, - microsoft: { + onedrive: { key: process.env.COMPANION_ONEDRIVE_KEY, secret: getSecret('COMPANION_ONEDRIVE_SECRET') }, diff --git a/packages/@uppy/companion/src/standalone/index.js b/packages/@uppy/companion/src/standalone/index.js index 704cff4ce7..386d7ec86f 100644 --- a/packages/@uppy/companion/src/standalone/index.js +++ b/packages/@uppy/companion/src/standalone/index.js @@ -182,7 +182,7 @@ if (process.env.COMPANION_PATH) { // Only set COMPANION_ONEDRIVE_DOMAIN_VALIDATION if you are sure that you are setting the // correct value for COMPANION_ONEDRIVE_KEY (i.e application ID). If there's a slightest possiblilty // that you might have mixed the values for COMPANION_ONEDRIVE_KEY and COMPANION_ONEDRIVE_SECRET, -// please do not set a value for COMPANION_ONEDRIVE_DOMAIN_VALIDATION +// please DO NOT set any value for COMPANION_ONEDRIVE_DOMAIN_VALIDATION if (process.env.COMPANION_ONEDRIVE_DOMAIN_VALIDATION === 'true' && process.env.COMPANION_ONEDRIVE_KEY) { app.get('/.well-known/microsoft-identity-association.json', (req, res) => { const content = JSON.stringify({ diff --git a/packages/@uppy/google-drive/src/index.js b/packages/@uppy/google-drive/src/index.js index 3e4faa5450..86b1f45857 100644 --- a/packages/@uppy/google-drive/src/index.js +++ b/packages/@uppy/google-drive/src/index.js @@ -25,7 +25,6 @@ module.exports = class GoogleDrive extends Plugin { companionUrl: this.opts.companionUrl, companionHeaders: this.opts.companionHeaders || this.opts.serverHeaders, provider: 'drive', - authProvider: 'google', pluginId: this.id }) diff --git a/packages/@uppy/instagram/src/index.js b/packages/@uppy/instagram/src/index.js index 8a21eb0615..ee1e01982f 100644 --- a/packages/@uppy/instagram/src/index.js +++ b/packages/@uppy/instagram/src/index.js @@ -24,7 +24,6 @@ module.exports = class Instagram extends Plugin { companionUrl: this.opts.companionUrl, companionHeaders: this.opts.companionHeaders || this.opts.serverHeaders, provider: 'instagram', - authProvider: 'instagram', pluginId: this.id })